Skip to content

Commit

Permalink
docs: update notebook autocmds
Browse files Browse the repository at this point in the history
  • Loading branch information
benlubas committed Jan 17, 2024
1 parent 921e6e9 commit e9abde9
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions docs/Notebook-Setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,29 +273,39 @@ We can make importing/exporting outputs seamless with a few autocommands:
-- automatically import output chunks from a jupyter notebook
-- tries to find a kernel that matches the kernel in the jupyter notebook
-- falls back to a kernel that matches the name of the active venv (if any)
vim.api.nvim_create_autocmd("BufWinEnter", {
pattern = { "*.ipynb" },
callback = function(e)
local kernels = vim.fn.MoltenAvailableKernels()

local try_kernel_name = function()
local metadata = vim.json.decode(io.open(e.file, "r"):read("a"))["metadata"]
return metadata.kernelspec.name
end
local ok, kernel_name = pcall(try_kernel_name)

if not ok or not vim.tbl_contains(kernels, kernel_name) then
kernel_name = nil
local venv = os.getenv("VIRTUAL_ENV")
if venv ~= nil then
kernel_name = string.match(venv, "/.+/(.+)")
end
local imb = function(e) -- init molten buffer
local kernels = vim.fn.MoltenAvailableKernels()
local try_kernel_name = function()
local metadata = vim.json.decode(io.open(e.file, "r"):read("a"))["metadata"]
return metadata.kernelspec.name
end
local ok, kernel_name = pcall(try_kernel_name)
if not ok or not vim.tbl_contains(kernels, kernel_name) then
kernel_name = nil
local venv = os.getenv("VIRTUAL_ENV")
if venv ~= nil then
kernel_name = string.match(venv, "/.+/(.+)")
end
end
if kernel_name ~= nil and vim.tbl_contains(kernels, kernel_name) then
vim.cmd(("MoltenInit %s"):format(kernel_name))
end
vim.cmd("MoltenImportOutput")
end

if kernel_name ~= nil and vim.tbl_contains(kernels, kernel_name) then
vim.cmd(("MoltenInit %s"):format(kernel_name))
-- automatically import output chunks from a jupyter notebook
vim.api.nvim_create_autocmd("BufAdd", {
pattern = { "*.ipynb" },
callback = imb,
})

-- we have to do this as well so that we catch files opened like nvim ./hi.ipynb
vim.api.nvim_create_autocmd("BufEnter", {
pattern = { "*.ipynb" },
callback = function(e)
if vim.api.nvim_get_vvar("vim_did_enter") ~= 1 then
imb(e)
end
vim.cmd("MoltenImportOutput")
end,
})
```
Expand Down Expand Up @@ -409,7 +419,7 @@ Compared to Jupyter-lab:
- jank. the UI is definitely worse, and sometimes images will move somewhere weird until
you scroll. Molten is still relatively new, and bugs are still being ironed out.
- setup is a lot of work. I've mentioned ~4~ 5 different plugins that are required to get
this working and all 4 of those plugins have external dependencies.
this working and all 4 of those plugins have external dependencies.

## Honorable Mentions

Expand Down

0 comments on commit e9abde9

Please sign in to comment.