Molten is a fork of Magma, a plugin for running code interactively with the jupyter kernel. Molten provides an excellent repl-like experience, and an incredible notebook-like experience in neovim.
https://github.com/benlubas/molten-nvim/assets/56943754/17ae81c0-306f-4496-bce8-99286e7f21ed
vim.ui.input
image.nvim
image
providerwezterm
image
providerpynvim
(for the Remote Plugin API)jupyter_client
(for interacting with Jupyter)cairosvg
(for displaying SVG images with transparency)pnglatex
(for displaying TeX formulas)plotly
and kaleido
(for displaying Plotly figures)pyperclip
if you want to use molten_copy_output
nbformat
for importing and exporting output to jupyter notebooks filespillow
for opening images with :MoltenImagePopup
requests
and websocket-client
for connecting to the Jupyter Server API via HTTP and WebSocket with :MoltenInit <Jupyter server URL>
You can run :checkhealth
to see what you have installed.
Note: Optional python packages are only imported when they're used.
Configuration information is located further down in this README.
To setup molten for editing Jupyter Notebook files, see Notebook Setup.
Start by initializing a kernel. This kernel will get a _kernelid which is most commonly just the
name of the kernel. If you try to initialize two kernels with the same name, the second one will be
named kernel_name_n
where n
is the total number of kernels that are already initialized.
You execute code by sending it to a kernel, specified by its _kernelid (this is handled automatically if there is only one option).
When you execute some code, it will create a cell. You can recognize a cell because it will be highlighted when your cursor is in it.
A cell is delimited using two extmarks (see :h api-extended-marks
), so each cell will adjust when
editing text within its boundaries.
When your cursor is in a cell (i.e., you have an active cell), a floating window may be shown
below the cell, reporting output. This is the floating output window. (To see more about whether
a window is shown or not, see :MoltenShowOutput
and g:molten_auto_open_output
). When you cursor
is not in any cell, no cell is active. When your cursor leaves a cell, its floating output window
will close.
Output may also be displayed as virtual text below a cell. Virtual text output will stay there until you re-run the cell or delete the cell.
Overlapping cells are not allowed. If you create an overlapping cell, the old cell will be deleted.
The output window has a header, containing the execution count and execution state (i.e., whether the cell is waiting to be run, running, has finished successfully or has finished with an error). Below the header, output is shown.
Jupyter provides a rich set of outputs. To see what we can currently handle, see Output Chunks.
These user commands are the main interface to the plugin. It is recommended to map most of them to keys, as explained in Keybindings.
Here is a list of the commands and their arguments. Args in []
are optional, args in ""
are
literal.
When the kernel
argument is specified as optional the command behaves in the following way:
Some commands will prompt for a kernel when they require one, but no
kernel is attached to the buffer. This is configurable with the molten_auto_init_behavior
option.
Command | Arguments | Description |
---|---|---|
MoltenInfo |
none | Show information about the state of the plugin, initialization status, available kernels, and running kernels |
MoltenInit |
["shared"] [kernel] |
Initialize a kernel for the current buffer. If shared is passed as the first value, this buffer will use an already running kernel. If no kernel is given, prompts the user. |
MoltenDeinit |
none | De-initialize the current buffer's runtime and molten instance. (called automatically on vim close/buffer unload) |
MoltenGoto |
[n] |
Go to the n th code cell n defaults to 1 (1 indexed) |
MoltenNext |
[n] |
Go to the next code cell, or jump n code cells n defaults to 1. Values wrap. Negative values move backwards |
MoltenPrev |
[n] |
like Next but backwards |
MoltenEvaluateLine |
[kernel] |
Evaluate the current line |
MoltenEvaluateVisual |
[kernel] |
Evaluate the visual selection (cannot be called with a range!) |
MoltenEvaluateOperator |
[kernel] |
Evaluate text selected by the following operator. see Keybindings for useage |
MoltenEvaluateArgument |
[kernel] code |
Evaluate given code in the given kernel |
MoltenReevaluateCell |
none | Re-evaluate the active cell (including new code) with the same kernel that it was originally evaluated with |
MoltenDelete |
none | Delete the active cell (does nothing if there is no active cell) |
MoltenShowOutput |
none | Shows the output window for the active cell |
MoltenHideOutput |
none | Hide currently open output window |
MoltenEnterOutput |
none | Move into the active cell's output window. Opens but does not enter the output if it's not open. must be called with noautocmd (see Keybindings for example) |
MoltenInterrupt |
[kernel] |
Sends a keyboard interrupt to the kernel which stops any currently running code. (does nothing if there's no current output) |
MoltenOpenInBrowser |
none | Open the current output in the browser. Currently this only supports cells with 'text/html' outputs, configured with molten_auto_open_html_in_browser and molten_open_cmd |
MoltenImagePopup |
none | Open an image from the current output with python's Image.show() . This will use your system's default image viewer, this behavior can happen automatically (see: molten_auto_image_popup ) |
MoltenRestart |
[!] [kernel] |
Shuts down a restarts the kernel. Deletes all outputs if used with a bang |
MoltenSave |
[path] [kernel] |
Save the current cells and evaluated outputs into a JSON file. When path is specified, save the file to path , otherwise save to g:molten_save_path . currently only saves one kernel per file |
MoltenLoad |
["shared"] [path] |
Loads cell locations and output from a JSON file generated by MoltenSave . path functions the same as MoltenSave . If shared is specified, the buffer shares an already running kernel. |
MoltenExportOutput |
[!] [path] [kernel] |
Export outputs from the current buffer and kernel to a jupyter notebook (.ipynb ) at the given path. read more |
MoltenImportOutput |
[path] [kernel] |
Import outputs from a jupyter notebook (.ipynb ). read more |
The commands above should be mapped to keys for the best experience.
Pay attention to MoltenEvaluateVisual
and MoltenEnterOutput
, as they need to be run in...odd
ways.
vim.keymap.set("n", "<localleader>mi", ":MoltenInit<CR>",
{ silent = true, desc = "Initialize the plugin" })
vim.keymap.set("n", "<localleader>e", ":MoltenEvaluateOperator<CR>",
{ silent = true, desc = "run operator selection" })
vim.keymap.set("n", "<localleader>rl", ":MoltenEvaluateLine<CR>",
{ silent = true, desc = "evaluate line" })
vim.keymap.set("n", "<localleader>rr", ":MoltenReevaluateCell<CR>",
{ silent = true, desc = "re-evaluate cell" })
vim.keymap.set("v", "<localleader>r", ":<C-u>MoltenEvaluateVisual<CR>gv",
{ silent = true, desc = "evaluate visual selection" })
vim.keymap.set("n", "<localleader>rd", ":MoltenDelete<CR>",
{ silent = true, desc = "molten delete cell" })
vim.keymap.set("n", "<localleader>oh", ":MoltenHideOutput<CR>",
{ silent = true, desc = "hide output" })
vim.keymap.set("n", "<localleader>os", ":noautocmd MoltenEnterOutput<CR>",
{ silent = true, desc = "show/enter output" })
Configuration is done with variables. Below you'll find a table of all the potential configuration variable, their values, and a brief description.
the default value is wrapped in ()
Variable | Values | Description |
---|---|---|
g:molten_auto_image_popup |
true | (false ) |
When true, cells that produce an image output will open the image output automatically with python's Image.show() |
g:molten_auto_init_behavior |
"raise" | ("init" ) |
When set to "raise" commands which would otherwise ask for a kernel when they're run without a running kernel will instead raise an exception. Useful for other plugins that want to use pcall and do their own error handling |
g:molten_auto_open_html_in_browser |
true | (false ) |
Automatically open HTML outputs in a browser. related: molten_open_cmd |
g:molten_auto_open_output |
(true ) | false |
Automatically open the floating output window when your cursor moves into a cell |
g:molten_cover_empty_lines |
true | (false ) |
The output window and virtual text will be shown just below the last line of code in the cell. |
g:molten_cover_lines_starting_with |
({} ) | array of str |
When cover_empty_lines is true, also covers lines starting with these strings |
g:molten_copy_output |
true | (false ) |
Copy evaluation output to clipboard automatically (requires pyperclip ) |
g:molten_enter_output_behavior |
("open_then_enter" ) | "open_and_enter" | "no_open" |
The behavior of MoltenEnterOutput |
g:molten_image_location |
("both" ) | "float" | "virt" | |
Where images will be displayed, either the floating window only, virtual text output only, or both. "virt" requires molten_virt_text_output = true |
g:molten_image_provider |
("none" ) | "image.nvim" | "wezterm" | |
How images are displayed see Images for more details |
g:molten_open_cmd |
(nil ) | Any command |
Defaults to xdg-open on Linux, open on Darwin, and start on Windows. But you can override it to whatever you want. The command is called like: subprocess.run([open_cmd, filepath]) |
g:molten_output_crop_border |
(true ) | false |
'crops' the bottom border of the output window when it would otherwise just sit at the bottom of the screen |
g:molten_output_show_exec_time |
(true ) | false |
Shows the current amount of time since the cell has begun execution |
g:molten_output_show_more |
true | (false ) |
When the window can't display the entire contents of the output buffer, shows the number of extra lines in the window footer (requires nvim 10.0+ and a window border) |
g:molten_output_virt_lines |
true | (false ) |
Pad the main buffer with virtual lines so the floating window doesn't cover anything while it's open |
g:molten_output_win_border |
({ "", "━", "", "" } ) | any value for border in :h nvim_open_win() |
Some border features will not work if you don't specify your border as a table. see border option of :h nvim_open_win() |
g:molten_output_win_cover_gutter |
(true ) | false |
Should the output window cover the gutter (numbers and sign col), or not. If you change this, you probably also want to change molten_output_win_style |
g:molten_output_win_hide_on_leave |
(true ) | false |
After leaving the output window (via :q or switching windows), do not attempt to redraw the output window |
g:molten_output_win_max_height |
(999999 ) | int |
Max height of the output window |
g:molten_output_win_max_width |
(999999 ) | int |
Max width of the output window |
g:molten_output_win_style |
(false ) | "minimal" |
Value passed to the style option in :h nvim_open_win() |
g:molten_save_path |
(stdpath("data").."/molten" ) | any path to a folder |
Where to save/load data with :MoltenSave and :MoltenLoad |
g:molten_split_direction |
("right" ) | "left" | "top" | "bottom" | |
Direction of the terminal split created by wezterm. Only applies if g:molten_image_provider = "wezterm" |
g:molten_split_size |
(40 ) | int |
(0-100) % size of the screen dedicated to the output window. _Only applies if g:molten_image_provider = "wezterm" _ |
g:molten_tick_rate |
(500 ) | int |
How often (in ms) we poll the kernel for updates. Determines how quickly the ui will update, if you want a snappier experience, you can set this to 150 or 200 |
g:molten_use_border_highlights |
true | (false ) |
When true, uses different highlights for output border depending on the state of the cell (running, done, error). see highlights |
g:molten_limit_output_chars |
(1000000 ) | int |
Limit on the number of chars in an output. If you're lagging your editor with too much output text, decrease it |
g:molten_virt_lines_off_by_1 |
true | (false ) |
Allows the output window to cover exactly one line of the regular buffer when output_virt_lines is true, also effects where virt_text_output is displayed. (useful for running code in a markdown file where that covered line will just be ```) |
g:molten_virt_text_output |
true | (false ) |
When true, show output as virtual text below the cell, virtual text stays after leaving the cell. When true, output window doesn't open automatically on run. Effected by virt_lines_off_by_1 |
g:molten_virt_text_max_lines |
(12 ) | int |
Max height of the virtual text |
g:molten_wrap_output |
true | (false ) |
Wrap output text |
[DEBUG] g:molten_show_mimetype_debug |
true | (false ) |
Before any non-iostream output chunk, the mime-type for that output chunk is shown. Meant for debugging/plugin devlopment |
Molten has two image providers, image.nvim
or wezterm
:
image.nvim
requires the image.nvim plugin (and its
dependencies). It renders images in neovim inline with other cell output. This creates a better
experience, but it can be buggy with large numbers of images, and it does not work on Windows.
wezterm
requires the wezterm.nvim plugin (and the wezterm
terminal emulator). It renders images in a wezterm split pane using wezterm's imgcat
program. This
method is significantly less buggy with large numbers of images and works on Windows, but it doesn't
keep images next to the code they came from.
g:molten_auto_open_output = true
g:molten_split_direction
and g:molten_split_size
options.wezterm
image provider does not integrate with tmux. There are issues
with allowing images to passing through tmux to wezterm. If you are using tmux, you will need to
use the image.nvim
image provider.Molten provides a few functions that you can use to see information in your status line. These are listed below:
require('molten.status').initialized() -- "Molten" or "" based on initialization information
require('molten.status').kernels() -- "kernel1 kernel2" list of kernels attached to buffer or ""
require('molten.status').all_kernels() -- same as kernels, but will show all kernels
The way these are used will vary based on status line plugin. So please refer to your status line plugin to figure out how to use them.
You can change highlights like so:
-- see :h nvim_set_hl for what to put in place of ...
-- I would recommend using the `link` option to link the values to colors from your color scheme
vim.api.nvim_set_hl(0, "MoltenOutputBorder", { ... })
Here is a complete list of the highlight groups that Molten uses, and their default values
MoltenOutputBorder
= FloatBorder
: default output window borderMoltenOutputBorderFail
= MoltenOutputBorder
: border of a failed output windowMoltenOutputBorderSuccess
= MoltenOutputBorder
: border of a successfully run output windowMoltenOutputWin
= NormalFloat
: the innards of the output windowMoltenOutputWinNC
= MoltenOutputWin
: a "Non-Current" output windowMoltenOutputFooter
= FloatFooter
: the "x more lines" textMoltenCell
= CursorLine
: applied to code that makes up a cellMoltenVirtualText
= Comment
: output that is rendered as virtual textWe provide some User
autocommands (see :help User
) for further customization. They are:
MoltenInitPre
: runs right before MoltenInit
initialization happens for a bufferMoltenInitPost
: runs right after MoltenInit
initialization happens for a bufferMoltenDeinitPre
: runs right before MoltenDeinit
de-initialization happens for a bufferMoltenDeinitPost
: runs right after MoltenDeinit
de-initialization happens for a bufferMoltenKernelReady
: runs when a kernel is ready for the first time. data
field has the kernel_id
Molten exposes some functionality through vim functions. These are mostly for plugin authors and people who want some custom behavior.
In the Jupyter protocol, most output-related messages provide a dictionary of mime-types which can
be used to display the data. Theoretically, a text/plain
field (i.e., plain text) is always
present, so we (theoretically) always have that fallback.
Here is a list of the currently handled mime-types:
text/plain
: Plain text. Shown as text in the output window's buffer.image/*
: Molten attempts to render any image
mimetype by sending it to image.nvim
. In theory,
this means that Molten can handle any image format that image.nvim
supports, though I've only
tested common formats
:MoltenImagePopup
application/vnd.plotly.v1+json
: A Plotly figure. Rendered into a PNG with
Plotly + Kaleidotext/latex
: A LaTeX formula. Rendered into a PNG with
pnglatextext/html
: via the :MoltenOpenInBrowser
command.This already provides quite a bit of basic functionality, but if you find a use case for a mime-type that isn't currently supported, feel free to open an issue and/or PR!