Magma is a NeoVim plugin for running code interactively with Jupyter.

Overview

Magma

Magma is a NeoVim plugin for running code interactively with Jupyter.

Requirements

  • NeoVim 0.5+
  • Python 3.8+
  • Required Python packages:
    • pynvim (for the Remote Plugin API)
    • jupyter_client (for interacting with Jupyter)
    • ueberzug (for displaying images)
    • Pillow (also for displaying images, should be installed with ueberzug)
    • cairosvg (for displaying SVG images)
    • pnglatex (for displaying TeX formulas)
    • plotly and kaleido (for displaying Plotly figures)

You can do a :checkhealth to see if you are ready to go.

Note: Python packages which are used only for the display of some specific kind of output are only imported when that output actually appears.

Installation

Use your favourite package/plugin manager.

If you use packer.nvim,

use { 'dccsillag/magma-nvim', run = ':UpdateRemotePlugins' }

If you use vim-plug,

Plug 'dccsillag/magma-nvim', { 'do': ':UpdateRemotePlugins' }

Note that you will still need to configure keymappings -- see Keybindings.

Suggested settings

If you want a quickstart, these are the author's suggestions of mappings and options (beware of potential conflicts of these mappings with your own!):

nnoremap  r  :MagmaEvaluateOperator
nnoremap        rr :MagmaEvaluateLine
xnoremap        r  :MagmaEvaluateVisual
nnoremap        rc :MagmaReevaluateCell
nnoremap        rd :MagmaDelete
nnoremap        ro :MagmaShowOutput

let g:magma_automatically_open_output = v:false

Note: Key mappings are not defined by default because of potential conflicts -- the user should decide which keys they want to use (if at all).

Note: The options that are altered here don't have these as their default values in order to provide a simpler (albeit perhaps a bit more inconvenient) UI for someone who just added the plugin without properly reading the README.

Usage

The plugin provides a bunch of commands to enable interaction. It is recommended to map most of them to keys, as explained in Keybindings. However, this section will refer to the commands by their names (so as to not depend on some specific mappings).

Interface

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 :help api-extended-marks), so it will adjust to you editing the text within it.

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 display window, or output window. (To see more about whether a window is shown or not, see :MagmaShowOutput and g:magma_automatically_open_output). When you cursor is not in any cell, no cell is active.

Also, the active cell is searched for from newest to oldest. That means that you can have a cell within another cell, and if the one within is newer, then that one will be selected. (Same goes for merely overlapping cells).

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 are shown the outputs.

Jupyter provides a rich set of outputs. To see what we can currently handle, see Output Chunks.

Commands

MagmaInit

This command initializes a runtime for the current buffer.

It can take a single argument, the Jupyter kernel's name. For example,

:MagmaInit python3

will initialize the current buffer with a python3 kernel.

It can also be called with no arguments, as such:

:MagmaInit

This will prompt you for which kernel you want to launch (from the list of available kernels).

MagmaDeinit

This command deinitializes the current buffer's runtime and magma instance.

:MagmaDeinit

Note You don't need to run this, as deinitialization will happen automatically upon closing Vim or the buffer being unloaded. This command exists in case you just want to make Magma stop running.

MagmaEvaluateLine

Evaluate the current line.

Example usage:

:MagmaEvaluateLine

MagmaEvaluateVisual

Evaluate the selected text.

Example usage (after having selected some text):

:MagmaEvaluateVisual

MagmaEvaluateOperator

Evaluate the text given by some operator.

This won't do much outside of an mapping. Example usage:

nnoremap  r nvim_exec('MagmaEvaluateOperator', v:true)

Upon using this mapping, you will enter operator mode, with which you will be able to select text you want to execute. You can, of course, hit ESC to cancel, as usual with operator mode.

MagmaReevaluateCell

Reevaluate the currently selected cell.

:MagmaReevaluateCell

MagmaDelete

Delete the currently selected cell. (If there is no selected cell, do nothing.)

Example usage:

:MagmaDelete

MagmaShowOutput

This only makes sense when you have g:magma_automatically_open_output = v:false. See Customization.

Running this command with some active cell will open the output window.

Example usage:

:MagmaShowOutput

MagmaSave

Save the current cells and evaluated outputs into a JSON file, which can then be loaded back with :MagmaLoad.

It has two forms; first, receiving a parameter, specifying where to save to:

:MagmaSave file_to_save.json

If that parameter is omitted, then one will be automatically generated using the g:magma_save_path option.

:MagmaSave

MagmaLoad

Load the cells and evaluated outputs stored in a given JSON file, which should have been generated with :MagmaSave.

Like MagmaSave, It has two forms; first, receiving a parameter, specifying where to save to:

:MagmaLoad file_to_load.json

If that parameter is omitted, then one will be automatically generated using the g:magma_save_path option.

Keybindings

It is recommended to map all the evaluate commands to the same mapping (in different modes). For example, if we wanted to bind evaluation to r:

nnoremap  r  nvim_exec('MagmaEvaluateOperator', v:true)
nnoremap        rr :MagmaEvaluateLine
xnoremap        r  :MagmaEvaluateVisual
nnoremap        rc :MagmaReevaluateCell

This way, r will behave just like standard keys such as y and d.

You can, of course, also map other commands:

nnoremap  rd :MagmaDelete
nnoremap  ro :MagmaShowOutput

Customization

Customization is done via variables.

g:magma_automatically_open_output

Defaults to v:true.

If this is true, then whenever you have an active cell its output window will be automatically shown.

If this is false, then the output window will only be automatically shown when you've just evaluated the code. So, if you take your cursor out of the cell, and then come back, the output window won't be opened (but the cell will be highlighted). This means that there will be nothing covering your code. You can then open the output window at will using :MagmaShowOutput.

g:magma_cell_highlight_group

Defaults to "CursorLine".

The highlight group to be used for highlighting cells.

g:magma_save_path

Defaults to stdpath("data") .. "/magma".

Where to save/load with :MagmaSave and :MagmaLoad (with no parameters).

The generated file is placed in this directory, with the filename itself being the buffer's name, with % replaced by %% and / replaced by %, and postfixed with the extension .json.

[DEBUG] g:magma_show_mimetype_debug

Defaults to v:false.

If this is true, then before any non-iostream output chunk, Magma shows the mimetypes it received for it.

This is meant for debugging and adding new mimetypes.

Extras

Output Chunks

In the Jupyter protocol, most output-related messages provide a dictionary of mimetypes 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 mimetypes:

  • text/plain: Plain text. Shown as text in the output window's buffer.
  • image/png: A PNG image. Shown with Ueberzug.
  • image/svg+xml: A SVG image. Rendered into a PNG with CairoSVG and shown with Ueberzug.
  • application/vnd.plotly.v1+json: A Plotly figure. Rendered into a PNG with Plotly + Kaleido and shown with Ueberzug.
  • text/latex: A LaTeX formula. Rendered into a PNG with pnglatex and shown with Ueberzug.

This already provides quite a bit of basic functionality. As development continues, more mimetypes will be added.

Notifications

We use the vim.notify API. This means that you can use plugins such as rcarriga/nvim-notify for pretty notifications.

License

This code is licensed under the GPLv3 (due to Ueberzug using it).

Owner
Daniel Csillag
Daniel Csillag
🪛 A simple pydantic to Form FastAPI model converter.

pyfa-converter Makes it pretty easy to create a model based on Field [pydantic] and use the model for www-form-data. How to install? pip install pyfa_

20 Dec 22, 2022
A terminal client for connecting to hack.chat servers

A terminal client for connecting to hack.chat servers.

V9 2 Sep 21, 2022
A begginer reverse shell tool python.

A begginer tools for hacking. The theme of this repository is to bring some ready-made open-source tools for anyone new to the world of hacking. This

Dio brando 2 Jan 05, 2022
Yet another bash/zsh prompt script

Here we have yet another script for Git-aware customization of the command prompt in Bash and zsh. Unlike all the other scripts, I wrote this one, so

John T. Wodder II 5 Oct 13, 2021
Get Air Quality Index for your city/country 😷

Air Quality Index CLI Get Air Quality index for your City. Installation $ pip install air-quality-cli Contents Air Quality Index CLI Installation Cont

Yankee 40 Oct 21, 2022
Generate folder trees directly from the terminal.

Dir Tree Artist 🎨 🌲 Intro Easily view folder structure, with parameters to sieve out what you want. Choose to exclude files from being viewed (.git,

Glenda T 0 May 17, 2022
Pyreadline3 - Windows implementation of the GNU readline library

pyreadline3 The pyreadline3 package is based on the stale package pyreadline loc

32 Jan 06, 2023
slipit is a command line utility for creating archives with path traversal elements.

slipit is a command line utility for creating archives with path traversal elements. It is basically a successor of the famous evilarc utility with an extended feature set and improved base functiona

usd AG 35 Dec 23, 2022
A Simple Python CLI Lockpicking Tool

Cryptex a simple CLI lockpicking tool What can it do: Encode / Decode Hex Encode / Decode Base64 Break Randomly :D Requirements: Python3 Linux as your

Alex Kollar 23 Jul 04, 2022
Use case: quick JSON processing/restructuring with jq without terminal

alfred-jq Alfred workflow to conveniently process JQ on clipboard based on a jq query Also available at: packal/jq Use case: quick JSON processing/res

T on Meta Mode 5 Sep 30, 2022
frogtrade9000 - a command-line Rich client for the freqtrade REST API

frogtrade9000 - a command-line Rich client for the freqtrade REST API I found FreqUI too cumbersome and slow on my Raspberry Pi 400 when running multi

Robert Davey 79 Dec 02, 2022
PyDropper - pick colors everywhere

PyDropper - pick colors everywhere Downloads Settings PyDropper is an eyedropper

Herman Brunberg 2 Jan 04, 2022
Bear-Shell is a shell based in the terminal or command prompt.

Bear-Shell is a shell based in the terminal or command prompt. You can navigate files, run python files, create files via the BearUtils text editor, and a lot more coming up!

MichaelBear 6 Dec 25, 2021
Command line tool to automate transforming the effects of one color profile to another, possibly more standard one.

Finished rendering the frames of that animation, and now the colors look washed out and ugly? This terminal program will solve exactly that.

Eric Xue 1 Jan 26, 2022
Python commandline tool for remembering linux/terminal commands

ehh Remember linux commands Commandline tool for remembering linux/terminal commands. It stores your favorite commands in ~/ehh.json in your homedir a

56 Nov 10, 2022
Zecwallet-Python is a simple wrapper around the Zecwallet Command Line LightClient written in Python

A wrapper around Zecwallet Command Line LightClient, written in Python Table of Contents About Installation Usage Examples About Zecw

Priveasy 2 Sep 06, 2022
Universal Command Line Interface for Amazon Web Services

This package provides a unified command line interface to Amazon Web Services.

Amazon Web Services 13.3k Jan 07, 2023
Bonjour Software pypahe is a Python Package Helper command-line tool.

pypahe Bonjour Software pypahe is a Python Package Helper command-line tool. Requirements Docker runtime Usage print the latest available version of a

Bonjour Software 0 Aug 10, 2021
MsfMania is a command line tool developed in Python that is designed to bypass antivirus software on Windows and Linux/Mac in the future

MsfMania MsfMania is a command line tool developed in Python that is designed to bypass antivirus software on Windows and Linux/Mac in the future. Sum

446 Dec 21, 2022
Lexeme - CLI to play a word-guessing game like Wordle

What is this? Python program to play a word-guessing game like Wordle, but… More addictive because you can play it over and over and over, not just on

Dan Lenski 6 Oct 26, 2022