nvim: Fix quarto jupyter kernel setup
Creates a hidden kernel file with the current filename and '.kernel.json' suffix by default (in the file directory). But 'JupyterStart' command can also be given an argument to use as the filename / file path for the kernel file. That way we can also attach to already running kernels for longer running projects.
This commit is contained in:
parent
6edd3143cb
commit
4d98c9ab46
1 changed files with 46 additions and 17 deletions
|
|
@ -9,9 +9,22 @@ local default_buffer_session = function()
|
||||||
return temp_path
|
return temp_path
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- TODO: Puths kernel into local file dir currently,
|
||||||
|
-- could take an argument to put it into temporary dir instead.
|
||||||
|
local kernel_filename = function()
|
||||||
|
local buf = vim.api.nvim_buf_get_name(0)
|
||||||
|
if not buf or buf:match("^$") then
|
||||||
|
vim.fn.tempname()
|
||||||
|
end
|
||||||
|
local path, name = buf:match("(.-)([^\\/]-%.?([^%.\\/]*))$")
|
||||||
|
|
||||||
|
return path .. "." .. name .. ".kernel.json"
|
||||||
|
end
|
||||||
|
|
||||||
-- Start quarto session
|
-- Start quarto session
|
||||||
local startsession = function(file, args)
|
local startsession = function(opts)
|
||||||
file = file or default_buffer_session()
|
local args = opts.fargs
|
||||||
|
local kernel_filen = args[1] or kernel_filename()
|
||||||
|
|
||||||
local path = require("core.util").get_python_venv_bin()
|
local path = require("core.util").get_python_venv_bin()
|
||||||
if not path then
|
if not path then
|
||||||
|
|
@ -19,39 +32,55 @@ local startsession = function(file, args)
|
||||||
end
|
end
|
||||||
vim.g["python3_host_prog"] = path
|
vim.g["python3_host_prog"] = path
|
||||||
|
|
||||||
if vim.fn.executable("jupyter-console") ~= 1 then
|
-- simply attach to existing if exists
|
||||||
|
if vim.fn.filereadable(kernel_filen) == 1 then
|
||||||
|
print(kernel_filen)
|
||||||
|
vim.cmd("MoltenInit " .. kernel_filen)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if args then
|
-- look for session maker
|
||||||
file = args[0]
|
local exec_name = "jupyter-kernel"
|
||||||
|
local exec_path = (require("core.util").get_python_venv_basefolder() or "") .. "/bin/" .. exec_name
|
||||||
|
if vim.fn.filereadable(exec_path) ~= 1 then
|
||||||
|
exec_path = exec_name
|
||||||
|
if vim.fn.executable(exec_path) ~= 1 then
|
||||||
|
return
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- make our own session
|
||||||
local once = false
|
local once = false
|
||||||
vim.fn.jobstart({ "jupyter", "console", "-f", file }, {
|
vim.fn.jobstart({ exec_path, "--KernelManager.connection_file", kernel_filen }, {
|
||||||
on_stdout = function(_)
|
on_stderr = function(_, data)
|
||||||
if not once then
|
if not once then
|
||||||
vim.cmd("MoltenInit " .. file)
|
for _, v in pairs(data) do
|
||||||
end
|
if v:find("connect a client") then
|
||||||
|
vim.cmd("MoltenInit " .. kernel_filen)
|
||||||
once = true
|
once = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end,
|
end,
|
||||||
on_exit = function(_)
|
on_exit = function(_)
|
||||||
vim.notify(string.format("jupyter kernel stopped: %s", file), vim.log.levels.INFO)
|
vim.notify(string.format("jupyter kernel stopped: %s", kernel_filen), vim.log.levels.INFO)
|
||||||
end,
|
end,
|
||||||
stdin = nil,
|
stdin = nil,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
vim.api.nvim_create_user_command("JupyterStart", function()
|
|
||||||
startsession(vim.b["sessionfile"] or default_buffer_session())
|
vim.api.nvim_create_user_command("JupyterStart", function(opts)
|
||||||
end, {})
|
startsession(opts)
|
||||||
|
end, { nargs = "?" })
|
||||||
|
|
||||||
if vim.g.quarto_auto_init_molten_session then
|
if vim.g.quarto_auto_init_molten_session then
|
||||||
vim.api.nvim_create_autocmd({ "InsertEnter", "BufEnter" }, {
|
vim.api.nvim_create_autocmd({ "BufEnter" }, {
|
||||||
callback = function()
|
callback = function()
|
||||||
if vim.b["sessionfile"] == nil then
|
if vim.b["sessionfile"] == nil then
|
||||||
local path = default_buffer_session()
|
local path = kernel_filename()
|
||||||
vim.b["sessionfile"] = path
|
vim.b["sessionfile"] = path
|
||||||
vim.schedule_wrap(function()
|
vim.schedule(function()
|
||||||
startsession(path)
|
startsession({ fargs = { path } })
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue