Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run jupyter notebook when not running #22

Merged
merged 1 commit into from
Jan 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,28 @@ The Jupynium server will receive events from Neovim, keep the copy of the buffer
- 🦊 Firefox (Other browsers are not supported due to their limitation with Selenium)
- ✌️ Neovim >= v0.8
- 🐍 Python >= 3.7
- 📔 Jupyter Notebook 6 (Doesn't support Jupyter Lab)
- 📔 Jupyter Notebook >= 6.2 (Doesn't support Jupyter Lab)

Don't have system python 3.7? You can use [Conda](https://docs.conda.io/en/latest/miniconda.html).

```bash
conda create -n jupynium
conda activate jupynium && conda install -c conda-forge python=3.11
conda create -n jupynium python=3
conda activate jupynium
```

Install with vim-plug:

```vim
Plug 'kiyoon/jupynium.nvim', { 'do': 'pip3 install --user .' }
" Plug 'kiyoon/jupynium.nvim', { 'do': '~/miniconda3/bin/envs/jupynium/bin/pip install .' }
" Plug 'kiyoon/jupynium.nvim', { 'do': '~/miniconda3/envs/jupynium/bin/pip install .' }
Plug 'rcarriga/nvim-notify' " optional
```

Install with packer.nvim:

```lua
use { "kiyoon/jupynium.nvim", run = "pip3 install --user ." }
-- use { "kiyoon/jupynium.nvim", run = "~/miniconda3/bin/envs/jupynium/bin/pip install ." }
-- use { "kiyoon/jupynium.nvim", run = "~/miniconda3/envs/jupynium/bin/pip install ." }
use { "rcarriga/nvim-notify" } -- optional
```

Expand Down Expand Up @@ -82,6 +82,14 @@ require("jupynium").setup({

default_notebook_URL = "localhost:8888",

-- Write jupyter command but without "notebook"
-- When you call :JupyniumStartAndAttachToServer and no notebook is open,
-- then Jupynium will open the server for you using this command. (only when notebook_URL is localhost)
-- It will open at the git directory of the current buffer,
-- and start syncing the relevant file (file.ju.py will be synced with file.ipynb)
jupyter_command = "jupyter",
-- jupyter_command = "~/miniconda3/envs/jupynium/bin/jupyter",

-- Used to remember the last session (password etc.).
-- You may need to change the path.
firefox_profiles_ini_path = vim.fn.isdirectory(vim.fn.expand "~/snap/firefox/common/.mozilla/firefox")
Expand Down Expand Up @@ -175,6 +183,10 @@ For example:
Running `:JupyniumStartAndAttachToServer` will open the notebook.
Type password and once **you need to be on the main page (file browser) for the next steps**.

**New in 0.1.1:**
Jupynium will open Jupyter Notebook server for you if not found.
It will also open the ipynb file in the current directory and ask you if you want to sync from vim or from ipynb.

### Sync current buffer to the Jupynium server

You attached your nvim instance to the server, but it won't automatically start syncing.
Expand Down
10 changes: 9 additions & 1 deletion lua/jupynium/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ local M = {}
M.opts = {}
M.default_opts = {
-- Conda users:
-- python_host = "~/miniconda3/envs/jupynium/bin/python",
-- python_host = "~/miniconda3/envs/jupynium/bin/python"
python_host = vim.g.python3_host_prog or "python3",

default_notebook_URL = "localhost:8888",

-- Write jupyter command but without "notebook"
-- When you call :JupyniumStartAndAttachToServer and no notebook is open,
-- then Jupynium will open the server for you using this command. (only when notebook_URL is localhost)
-- It will open at the git directory of the current buffer,
-- and start syncing the relevant file (file.ju.py will be synced with file.ipynb)
jupyter_command = "jupyter",
-- jupyter_command = "~/miniconda3/envs/jupynium/bin/jupyter",

-- Used to remember the last session (password etc.).
-- You may need to change the path.
firefox_profiles_ini_path = vim.fn.isdirectory(vim.fn.expand "~/snap/firefox/common/.mozilla/firefox")
Expand Down
110 changes: 79 additions & 31 deletions lua/jupynium/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,82 @@ M.server_state = {
is_autoattached = false,
}

local function TableConcat(t1, t2)
for i = 1, #t2 do
t1[#t1 + 1] = t2[i]
end
return t1
end

local function run_process_bg(cmd, args)
args = args or {}
local call_str
if vim.fn.has "win32" == 1 then
call_str = [[call system('PowerShell "Start-Process -FilePath \"]]
.. vim.fn.expand(cmd):gsub("\\", "\\\\")
.. [[\" -ArgumentList \"]]

for _, v in ipairs(args) do
call_str = call_str .. [[ `\"]] .. v:gsub("\\", "\\\\") .. [[`\"]]
end

call_str = call_str .. [[\""')]]
else
call_str = [[call system('"]] .. vim.fn.expand(cmd) .. [["]]

for _, v in ipairs(args) do
call_str = call_str .. [[ "]] .. v:gsub("\\", "\\\\") .. [["]]
end

call_str = call_str .. [[ &')]]
end

vim.cmd(call_str)
end

local function run_process(cmd, args)
args = args or {}
local call_str = [[echo system('"]] .. vim.fn.expand(cmd) .. [["]]

for _, v in ipairs(args) do
call_str = call_str .. [[ "]] .. v:gsub("\\", "\\\\") .. [["]]
end

local output = vim.cmd(call_str)
if output == nil then
return ""
else
return output
end
end

local function call_jupynium_cli(args, bg)
args = args or {}
if bg == nil then
bg = true
end

args = TableConcat({ "-m", "jupynium", "--nvim_listen_addr", vim.v.servername }, args)

local cmd
if type(options.opts.python_host) == "string" then
cmd = options.opts.python_host
elseif type(options.opts.python_host) == "table" then
cmd = options.opts.python_host[1]
args = TableConcat({ unpack(options.opts.python_host, 2) }, args)
else
error "Invalid python_host type."
end

if bg then
run_process_bg(cmd, args)
else
return run_process(cmd, args)
end
end

function M.jupynium_pid()
local pid = vim.fn.trim(vim.fn.system(options.opts.python_host .. " -m jupynium --check_running"))
local pid = vim.fn.trim(call_jupynium_cli({ "--check_running" }, false))
if pid == "" then
return nil
else
Expand Down Expand Up @@ -103,34 +177,6 @@ function M.add_commands()
vim.api.nvim_create_user_command("JupyniumAttachToServer", M.attach_to_server_cmd, { nargs = "?" })
end

local function call_jupynium_cli_bg(args)
local call_str
if vim.fn.has "win32" == 1 then
call_str = [[call system('PowerShell "Start-Process -FilePath \"]]
.. vim.fn.expand(options.opts.python_host):gsub("\\", "\\\\")
.. [[\" -ArgumentList \"-m jupynium --nvim_listen_addr ]]
.. vim.v.servername

for _, v in ipairs(args) do
call_str = call_str .. [[ `\"]] .. v:gsub("\\", "\\\\") .. [[`\"]]
end

call_str = call_str .. [[\""')]]
else
call_str = [[call system('"]]
.. vim.fn.expand(options.opts.python_host)
.. [[" -m jupynium --nvim_listen_addr ]]
.. vim.v.servername

for _, v in ipairs(args) do
call_str = call_str .. [[ "]] .. v:gsub("\\", "\\\\") .. [["]]
end

call_str = call_str .. [[ &')]]
end
vim.cmd(call_str)
end

function M.start_and_attach_to_server_cmd(args)
local notebook_URL = vim.fn.trim(args.args)

Expand All @@ -142,7 +188,9 @@ function M.start_and_attach_to_server_cmd(args)
table.insert(args, "--firefox_profile_name")
table.insert(args, options.opts.firefox_profile_name)
end
call_jupynium_cli_bg(args)
table.insert(args, "--jupyter_command")
table.insert(args, options.opts.jupyter_command)
call_jupynium_cli(args, true)
end

function M.attach_to_server_cmd(args)
Expand All @@ -151,7 +199,7 @@ function M.attach_to_server_cmd(args)
if notebook_URL == "" then
notebook_URL = options.opts.default_notebook_URL
end
call_jupynium_cli_bg { "--attach_only", "--notebook_URL", notebook_URL }
call_jupynium_cli({ "--attach_only", "--notebook_URL", notebook_URL }, true)
end

return M
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ dependencies = [
"persist-queue >= 0.8.0",
"packaging >= 22.0",
"setuptools >= 45.0", # for pkg_resources. Otherwise get LegacyVersion error
"gitpython >= 3.1.24",
]

[project.optional-dependencies]
extra = [
"notebook >= 6.4.5",
]
dev = [
"black >= 22.1.0",
"pre-commit >= 2.21.0",
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ psutil==5.9.4
persist-queue==0.8.0
packaging==23.0
setuptools==66.0
gitpython==3.1.30
Loading