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

feat: Connect to synchronized kernel #77

Merged
merged 6 commits into from
Apr 18, 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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ The Jupynium server will receive events from Neovim, keep the copy of the buffer
- Supported Python installation methods include system-level and [Conda](https://docs.conda.io/en/latest/miniconda.html)
- 📔 Jupyter Notebook >= 6.2
- Jupyter Lab is not supported
-
```sh
# jupyter-console is optional and used for `:JupyniumKernelOpenInTerminal`
pip install notebook jupyter-console
```

### Install Python

Expand Down Expand Up @@ -442,6 +447,7 @@ If you want custom keymaps, add `textobjects = { use_default_keybindings = false
:JupyniumKernelInterrupt
:JupyniumKernelSelect
:JupyniumKernelHover " See value like LSP hover
:JupyniumKernelOpenInTerminal [hostname] " Connect to kernel of synchronized notebook

" Highlight
:JupyniumShortsightedToggle
Expand Down
6 changes: 6 additions & 0 deletions src/jupynium/events_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,12 @@ def process_request_event(nvim_info: NvimInfo, driver, event):
event[3].send(ret_obj)
return True, None

elif event[1] == "kernel_connect_info":
driver.switch_to.window(nvim_info.window_handles[bufnr])
kernel_id = driver.execute_script("return Jupyter.notebook.kernel.id")
event[3].send(kernel_id)
return True, None

if event[3] is not None:
event[3].send("OK")

Expand Down
1 change: 1 addition & 0 deletions src/jupynium/lua/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ vim.api.nvim_create_user_command("JupyniumKernelRestart", "lua Jupynium_kernel_r
vim.api.nvim_create_user_command("JupyniumKernelInterrupt", "lua Jupynium_kernel_interrupt()", {})
vim.api.nvim_create_user_command("JupyniumKernelSelect", "lua Jupynium_kernel_select()", {})
vim.api.nvim_create_user_command("JupyniumKernelHover", "lua Jupynium_kernel_hover()", {})
vim.api.nvim_create_user_command("JupyniumKernelOpenInTerminal", Jupynium_kernel_connect_cmd, { nargs = "?" })
39 changes: 39 additions & 0 deletions src/jupynium/lua/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -688,3 +688,42 @@ function Jupynium_kernel_complete_async(bufnr, code_line, col, callback)

Jupynium_rpcnotify("kernel_complete_async", bufnr, true, code_line, col, callback_id)
end

function Jupynium_get_kernel_connect_shcmd(bufnr, hostname)
if bufnr == nil or bufnr == 0 then
bufnr = vim.api.nvim_get_current_buf()
end

local kernel_id = nil
if Jupynium_syncing_bufs[bufnr] ~= nil then
kernel_id = Jupynium_rpcrequest("kernel_connect_info", bufnr, true)
end
if kernel_id == nil then
kernel_id = ""
end
local jupyter_command = "jupyter"
local ok, options = pcall(require, "jupynium.options")
if ok then
if type(options.opts.jupyter_command) == "string" then
jupyter_command = options.opts.jupyter_command
elseif type(options.opts.jupyter_command) == "table" then
jupyter_command = table.concat(options.opts.jupyter_command, " ")
else
Jupynium_notify.error { "Invalid jupyter_command type." }
end
end
if hostname ~= "" then
jupyter_command = "ssh " .. hostname .. " -t " .. jupyter_command
end
Jupynium_notify.info { "Connecting to kernel " .. kernel_id }
local cmd = jupyter_command .. " console --existing " .. kernel_id
return cmd
end
kiyoon marked this conversation as resolved.
Show resolved Hide resolved

function Jupynium_kernel_connect_cmd(args)
local hostname = args.args
local buf = vim.api.nvim_get_current_buf()
local cmd = Jupynium_get_kernel_connect_shcmd(buf, hostname)
vim.cmd([[split | terminal ]] .. cmd)
vim.cmd [[normal! G]]
end