Package: jupynium
Jupynium: Control Jupyter Notebook on Neovim with ZERO CompromiseIt's just like a markdown live preview, but it's Jupyter Notebook live preview!Jupynium uses Selenium to automate Jupyter Notebook, synchronising everything you type on Neovim.Never leave Neovim. Switch tabs on the browser as you switch files on Neovim.Note that it doesn't sync from Notebook to Neovim so only modify from Neovim.How does it work?The Jupynium server will receive events from Neovim, keep the copy of the buffer and apply that to the Jupyter Notebook by using Selenium browser automation. It interacts only through the front end so it doesn't require installing extensions on the kernel etc., which makes it possible to:Develop locally, run remotely (or vice versa)Use university-hosted Jupyter NotebookUse any other languages / kernels such as R🛠️ InstallationRequirements💻 Linux, macOS and Windows (CMD, PowerShell, WSL2)✌️ Neovim >= v0.8🦊 FirefoxOther browsers are not supported due to their limitation with Selenium (see#49)🦎 Mozilla geckodriverMay already be installed with Firefox. Checkgeckodriver -V🐍 Python >= 3.7Supported Python installation methods include system-level andConda📔 Jupyter Notebook >= 6.2Jupyter Lab is not supported# jupyter-console is optional and used for `:JupyniumKernelOpenInTerminal`pipinstallnotebooknbclassicjupyter-consoleImportant note about Notebook 7 (breaking change!)Jupynium does not support Notebook 7 yet. In the meantime, you can change thedefault_notebook_URL = "localhost:8888/nbclassic"inrequire("jupynium").setup({ ... })to use the classic (Notebook 6) interface with Jupynium. This is the new default setting from now on.Don't forget to upgrade your notebook and install nbclassic (pip install --upgrade notebook nbclassic) when you set this.Install PythonDon't have system Python 3.7? You can useConda:condacreate-njupyniumpython=3condaactivatejupyniumUpgrade pip. This solves many problems:# pip >= 23.0 recommendedpip3install--upgradepipInstall JupyniumClick to see vim-plug and packer installation.With vim-plug:Plug'kiyoon/jupynium.nvim',{'do':'pip3 install --user .'}" Plug 'kiyoon/jupynium.nvim', { 'do': 'conda run --no-capture-output -n jupynium pip install .' }Plug'rcarriga/nvim-notify'" optionalPlug'stevearc/dressing.nvim'" optional, UI for :JupyniumKernelSelectWith packer.nvim:use{"kiyoon/jupynium.nvim",run="pip3 install --user ."}-- use { "kiyoon/jupynium.nvim", run = "conda run --no-capture-output -n jupynium pip install ." }use{"rcarriga/nvim-notify"}-- optionaluse{"stevearc/dressing.nvim"}-- optional, UI for :JupyniumKernelSelectWith 💤lazy.nvim:{"kiyoon/jupynium.nvim",build="pip3 install --user .",-- build = "conda run --no-capture-output -n jupynium pip install .",-- enabled = vim.fn.isdirectory(vim.fn.expand "~/miniconda3/envs/jupynium"),},"rcarriga/nvim-notify",-- optional"stevearc/dressing.nvim",-- optional, UI for :JupyniumKernelSelectConfigure JupyniumThe default configuration values are below and work well for system-level Python users. If you're a Conda user, you need to changepython_hostto execute using thecondacommand instead.Click to see the setup defaultsrequire("jupynium").setup({--- For Conda environment named "jupynium",-- python_host = { "conda", "run", "--no-capture-output", "-n", "jupynium", "python" },python_host=vim.g.python3_host_progor"python3",default_notebook_URL="localhost:8888/nbclassic",-- 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)jupyter_command="jupyter",--- For Conda, maybe use base environment--- then you can `conda install -n base nb_conda_kernels` to switch environment in Jupyter Notebook-- jupyter_command = { "conda", "run", "--no-capture-output", "-n", "base", "jupyter" },-- Used when notebook is launched by using jupyter_command.-- If nil or "", it will open at the git directory of the current buffer,-- but still navigate to the directory of the current buffer. (e.g. localhost:8888/nbclassic/tree/path/to/buffer)notebook_dir=nil,-- Used to remember the last session (password etc.).-- e.g. '~/.mozilla/firefox/profiles.ini'-- or '~/snap/firefox/common/.mozilla/firefox/profiles.ini'firefox_profiles_ini_path=nil,-- nil means the profile with Default=1-- or set to something like 'default-release'firefox_profile_name=nil,-- Open the Jupynium server if it is not already running-- which means that it will open the Selenium browser when you open this file.-- Related command :JupyniumStartAndAttachToServerauto_start_server={enable=false,file_pattern={"*.ju.*"},},-- Attach current nvim to the Jupynium server-- Without this step, you can't use :JupyniumStartSync-- Related command :JupyniumAttachToServerauto_attach_to_server={enable=true,file_pattern={"*.ju.*","*.md"},},-- Automatically open an Untitled.ipynb file on Notebook-- when you open a .ju.py file on nvim.-- Related command :JupyniumStartSyncauto_start_sync={enable=false,file_pattern={"*.ju.*","*.md"},},-- Automatically keep filename.ipynb copy of filename.ju.py-- by downloading from the Jupyter Notebook server.-- WARNING: this will overwrite the file without asking-- Related command :JupyniumDownloadIpynbauto_download_ipynb=true,-- Automatically close tab that is in sync when you close buffer in vim.auto_close_tab=true,-- Always scroll to the current cell.-- Related command :JupyniumScrollToCellautoscroll={enable=true,mode="always",-- "always" or "invisible"cell={top_margin_percent=20,},},scroll={page={step=0.5},cell={top_margin_percent=20,},},-- Files to be detected as a jupynium file.-- Add highlighting, keybindings, commands (e.g. :JupyniumStartAndAttachToServer)-- Modify this if you already have lots of files in Jupytext format, for example.jupynium_file_pattern={"*.ju.*"},use_default_keybindings=true,textobjects={use_default_keybindings=true,},syntax_highlight={enable=true,},-- Dim all cells except the current one-- Related command :JupyniumShortsightedToggleshortsighted=false,-- Configure floating window options-- Related command :JupyniumKernelHoverkernel_hover={floating_win_opts={max_width=84,border="none",},},})-- You can link highlighting groups.-- This is the default (when colour scheme is unknown)-- Try with CursorColumn, Pmenu, Folded etc.vim.cmd[[hi! link JupyniumCodeCellSeparator CursorLinehi! link JupyniumMarkdownCellSeparator CursorLinehi! link JupyniumMarkdownCellContent CursorLinehi! link JupyniumMagicCommand Keyword]]-- Please share your favourite settings on other colour schemes, so I can add defaults.-- Currently, tokyonight is supported.Optionally, configurenvim-cmpto show Jupyter kernel completionlocalcmp=require"cmp"localcompare=cmp.config.comparecmp.setup{sources={{name="jupynium",priority=1000},-- consider higher priority than LSP{name="nvim_lsp",priority=100},-- ...},sorting={priority_weight=1.0,comparators={compare.score,-- Jupyter kernel completion shows prior to LSPcompare.recently_used,compare.locality,-- ...},},}Optionally, configurenvim-ufoto fold cellsThere is an API serving as a folds provider, which will return a table with format{startLine=#num, endLine=#num}.require("jupynium").get_folds()You should use it with a fold plugin likenvim-ufo.See#88for more detail and an example configuration.🏃 Quick StartOpen a*.ju.pyfile.Execute:JupyniumStartAndAttachToServer. This will open Jupyter Notebook on the Firefox browser.If not, clarify optionjupyter_commandor just open the Notebook server by yourself:jupyter notebookExecute:JupyniumStartSync. This will create anUntitled.ipynbfile on the browser.Now you can type# %%in Neovim to create a code cell.You'll see everything you type below that will be synchronised in the browser.Execute cells using the default keybind<space>x.For detailed instructions, see Usage below.🚦 UsageThere are 2 general steps to using Jupynium:Setup a Jupynium fileConnect to the Jupynium serverThe Jupynium server stays alive as long as the browser is alive. So you can see them as the same thing in this doc.
For example:Starting Jupynium server = opening a Selenium browserManually closing the browser = closing the Jupynium serverSetup a Jupynium fileJupynium uses a Jupytext's percent format (see theJupynium file formatsection below). This Jupytext file named.ju.pyis what you will primarily be interacting with, rather than the.ipynbfile directly. The contents of the Jupynium file are synced to the browser notebook where it can be viewed in real-time. If you want to keep a copy of the notebook, it can be downloaded as an.ipynbfile later.First, it's recommended to set a password on your notebook (rather than using tokens):$jupyternotebookpasswordEnter password: 🔒$jupyternotebook# leave notebook openedIf you want to start a new notebookManually create a local Jupynium file called<filename>.ju.pyDone! The rest happens after connecting to the serverIf you want to open an existing notebookThere are currently 2 ways of converting an existing.ipynbfile to a Jupynium file:Option 1: Use an included command line tool:ipynb2jupytext[-h][--stdout][--code_only]file.ipynb[file.ju.py]If you're already familiar with Jupytext, feel free to use it instead.Option 2: This method requires that you have already connected to the Jupynium server:Open your.ipynbfile in the web browser after connecting to the serverIn a new Neovim buffer, run:JupyniumLoadFromIpynbTab. This will convert the contents of the notebook file to Jupynium format.Save your buffer as<filename>.ju.pyWhen using Jupynium for the first time, it's recommended to start a new notebook to make sure everything works before trying to load existing files.Connect to the Jupynium server(This is for local Neovim only. For remote Neovim, seeCommand-Line Usage)In Neovim, with your Jupynium.ju.pyfile open, you can run:JupyniumStartAndAttachToServerto start the notebook server.Sync current buffer to the Jupynium serverYou need to be on the main notebook page (file browser) for the next few steps.Although Neovim is now attached to the server, it won't automatically start syncing.To sync your Neovim Jupynium file to a notebook, run:JupyniumStartSync.You can also::JupyniumStartSync filenameto give a name to the notebook (filename.ipynb) instead ofUntitled.ipynb. This does not open existing files. If a file with that name already exists then the filename argument will just be ignored.To sync a Jupynium file to an existing notebook, manually open the file in the browser, and:JupyniumStartSync 2to sync to the 2nd tab (count from 1).At this point, any changes you make within the Neovim Jupynium file will be reflected live in the browser. Make sure you do not make changes inside the browser itself, as the sync is only one-way (from Neovim to browser).If you want to save a copy of the.ipynbfile, run:JupyniumDownloadIpynb. There is also a configuration option to enable automatic downloading.Sync multiple Jupynium filesYou can sync multiple files at the same time. Simple run:JupyniumStartSyncagain with the new file you want to sync.Use multiple NeovimYou can run:JupyniumStartSyncon a new Neovim instance.If you haveauto_attach_to_server = falseduring setup, you need to run:JupyniumAttachToServerand:JupyniumStartSync.📝 Jupynium file format (.ju.py or .ju.*)The Jupynium file format follows Jupytext's percent format. In order for Jupynium to detect the files, name them as*.ju.pyor specifyjupynium_file_patterninrequire("jupynium").setup().Code cell:Any code below this line (and before the next separator) will be the content of a code cell.# %%Magic commands# %timebecomes%timein notebook.If you want to really comment out magic commands, comment it two times like## %time.Markdown cell:Any code below this line will be markdown cell content.# %% [md]# %% [markdown]In Python, the recommended way is to wrap the whole cell content as a multi-line string.# %% [md]"""# This is a markdown headingThis is markdown content"""In other languages like R, you'll need to comment every line.# %% [md]# # This is a markdown heading# This is markdown contentExplicitly specify the first cell separator to use it like a notebook.If there is one or more cells, it works as a notebook mode.Contents before the first cell are ignored, so use it as a heading (shebang etc.)If there is no cell, it works as a markdown preview mode.It will still open ipynb file but will one have one markdown cell.⌨️ Keybindings<space>x: Execute selected cells<space>c: Clear selected cells<PageUp>,<PageDown>: Scroll notebook<space>js: Scroll to cell (if autoscroll is disabled)<space>K: Hover (inspect a variable)<space>jo: Toggle output scroll (when output is long)If you want custom keymaps, adduse_default_keybindings = falseand followM.default_keybindings()inlua/jupynium/init.lua.Textobjects[j,]j: go to previous / next cell separatorRepeat with;and,if you havenvim-treesitter-textobjects.
Follow their instructions for keybindings for this.<space>jj: go to current cell separatorvaj,vij,vaJ,viJ: select current cella: include separator,i: exclude separatorj: exclude next separator,J: include next separatorIf you want custom keymaps, addtextobjects = { use_default_keybindings = false }and followM.default_keybindings()inlua/jupynium/textobj.lua.📡 Available Vim Commands" Server (only used when Neovim is local. See Command-Line Usage for remote neovim):JupyniumStartAndAttachToServer [notebook_URL]:JupyniumStartAndAttachToServerInTerminal [notebook_URL]" Useful for debugging:JupyniumAttachToServer [notebook_URL]" Sync:JupyniumStartSync [filename / tab_index]:JupyniumStopSync:JupyniumLoadFromIpynbTab tab_index:JupyniumLoadFromIpynbTabAndStartSync tab_index" Notebook (while syncing):JupyniumSaveIpynb:JupyniumDownloadIpynb [filename]:JupyniumAutoDownloadIpynbToggle:JupyniumScrollToCell:JupyniumScrollUp:JupyniumScrollDown:JupyniumAutoscrollToggle:JupyniumExecuteSelectedCells:JupyniumClearSelectedCellsOutputs:JupyniumToggleSelectedCellsOutputsScroll:JupyniumKernelRestart:JupyniumKernelInterrupt:JupyniumKernelSelect:JupyniumKernelHover" See value like LSP hover:JupyniumKernelOpenInTerminal [hostname]" Connect to kernel of synchronized notebook" Highlight:JupyniumShortsightedToggleLua APIThe core API is provided as a global function.--- Execute javascript in the browser. It will switch to the correct tab before executing.---@param bufnr: integer | nil If given, before executing the code it will switch to the tab of this buffer. Requires syncing in advance.---@param code string Javascript code---@return boolean, object: Success, responseJupynium_execute_javascript(bufnr,code)Example: get kernel name and language-- Use bufnr=nil if you don't want to switch tablocalcode=[[return [Jupyter.notebook.kernel.name, Jupyter.kernelselector.kernelspecs];]]localstatus_ok,kernel_name_and_spec=Jupynium_execute_javascript(0,code)ifstatus_okthenlocalkernel_name=kernel_name_and_spec[1]-- "python3"localkernel_spec=kernel_name_and_spec[2]localkernel_language=kernel_spec[kernel_name].spec.language-- "python"localkernel_display_name=kernel_spec[kernel_name].spec.display_name-- "Python 3 (ipykernel)"end👨‍💻️ Command-Line Usage (attach to remote Neovim)You don't need to install the vim plugin to use Jupynium.The plugin is responsible of adding:JupyniumStartAndAttachToServeretc. that just calls the command line program, plus it has textobjects and shortsighted support.Install Jupynium if you haven't already:pip3installjupyniumOpen a python/markdown file with nvim and see:echo v:servername.Run Jupynium like:jupynium--nvim_listen_addr/tmp/your_socket_pathOr, you can run Neovim likenvim--listenlocalhost:18898notebook.ju.pyThen start Jupynium as well as attaching the neovim to it.jupynium--nvim_listen_addrlocalhost:18898Note that you can attach to remote neovim by changinglocalhosttoservername.comor using SSH port forwarding.This will open Firefox with Selenium, defaulting tohttp://localhost:8888/nbclassic.Additionally,You can runjupyniumcommand multiple times to attach more than one Neovim instance.jupynium --notebook_URL localhost:18888to view different notebook.You can just runjupyniumwithout arguments to just leave the server / browser running and wait for nvim to attach.⚠️ CautionThe program is in the alpha stage. If it crashes it's likely that the whole browser turns off without saving!RulesAlways leave the home page accessible. Jupynium interacts with it to open files. Do not close, or move to another website.It's okay to move between directories.It's OK to close the notebook pages. If you do so, it will stop syncing that buffer.Changing tab ordering or making it to a separate window is OK.🤔 FAQ🌽 How do I use different languages / kernels?Instead of*.ju.pyif you make files named*.ju.*(e.g.*.ju.r) you will see all the keybindings and commands.All the procedures should be the same.The notebook content is not in sync with vim. How do I fix it?You probably would have accidentally modified directly from the notebook.If you want to keep the vim content and sync to the notebook, just add one more cell in the notebook and start making changes in vim. It works because Jupynium tries to only update the currently modified cell if the number of cells is the same in both. If it differs, it will fully update the entire content.To keep the notebook content and load that to vim, run:JupyniumLoadFromIpynbTab [tab_index]without making changes on vim.📰 Fun FactsI spent my whole Christmas and New Year holidays (and more) just making this plugin.This is the star history chart with relevant plugins. Thank you for helping it grow!