Jupyter/Extensions: Difference between revisions
From charlesreid1
| Line 140: | Line 140: | ||
===How It Works=== | ===How It Works=== | ||
Again, see: https://mindtrove.info/4-ways-to-extend-jupyter-notebook/#nb-extensions | |||
(From 2015, but about Jupyter - how much has changed?) | |||
To actually put this into practice, all of the logic goes into a JS file. It must conform to AMD specification so it can be loaded by Require.js. | To actually put this into practice, all of the logic goes into a JS file. It must conform to AMD specification so it can be loaded by Require.js. | ||
Revision as of 02:10, 9 December 2017
Notebook extensions
To enable/install a notebook extension:
jupyter nbextension enable <nbextension require path>
Example: Jupyter/Base16mpl
Also see: https://github.com/ipython-contrib/jupyter_contrib_nbextensions
Server extensions
To enable/install a server extension:
jupyter serverextension enable <sererextension require path and flags>
Example: Jupyter/MPI
User-Contributed Notebook Extensions
Full instructions here:
Start by installing the different nb extensions available:
pip3 install jupyter_contrib_nbextensions
Install the notebook extension files:
jupyter contrib nbextension install --user
Command line options:
--user to install into the user's home jupyter directories
--system to perform installation into system-wide jupyter directories
--sys-prefix to install into python's sys.prefix, useful for instance in virtual environments, such as with conda
--symlink to symlink the nbextensions rather than copying each file (recommended, on non-Windows platforms).
--debug, for more-verbose output
To enable an extension:
jupyter nbextension enable <nbextension require path>
for example,
jupyter nbextension enable codefolding/main jupyter nbextension enable toc2/main
To disable the extension again, use
jupyter nbextension disable <nbextension require path>
Making An Extension
Rolling Your Own
See ipyvolume: https://github.com/maartenbreddels/ipyvolume
Basically just defines a function _jupyter_nbextension_paths: https://github.com/maartenbreddels/ipyvolume/blob/master/ipyvolume/__init__.py#L12
And that's it.
def _jupyter_nbextension_paths():
return [{
'section': 'notebook',
'src': 'static',
'dest': 'ipyvolume',
'require': 'ipyvolume/extension'
}]
To install it, the recommended method is:
pip install ipyvolume jupyter nbextension enable --py --sys-prefix ipyvolume
Or with a dev version (git):
$ git clone https://github.com/maartenbreddels/ipyvolume.git $ cd ipyvolume $ pip install -e . $ jupyter nbextension install --py --symlink --sys-prefix ipyvolume $ jupyter nbextension enable --py --sys-prefix ipyvolume
Build Extensions from Gists
The Vision
This is an intriguing blog post: https://mindtrove.info/4-ways-to-extend-jupyter-notebook/#nb-extensions
Today, the recommended way of installing, loading, and unloading extensions requires running a few snippets of code either within a notebook associated with an IPython kernel or in an external Python script. To install, for example, minrk's gist nbextension, you can execute the following code in a notebook.import notebook.nbextensions notebook.nbextensions.install_nbextension('https://rawgithub.com/minrk/ipython_extensions/master/nbextensions/gist.js', user=True)The code above downloads the JavaScript file and copies it into your Jupyter data directory (e.g., ~/.local/share/jupyter/nbextensions). Once installed, you can load the gist extension by executing the following JavaScript in a code cell:
%%javascript Jupyter.utils.load_extensions('gist')The cell above emits a <script> element into the output area of the notebook cell. That script loads the gist.js JavaScript module from the Notebook server backend. On load, the gist extension adds a button appears to the toolbar which you can click to post the current notebook document as a gist.
Now a notebook configuration file can be updated to load the extension automatically:
from notebook.services.config import ConfigManager
cm = ConfigManager()
cm.update('notebook', {"load_extensions": {"gist": True}})
How It Works
Again, see: https://mindtrove.info/4-ways-to-extend-jupyter-notebook/#nb-extensions
(From 2015, but about Jupyter - how much has changed?)
To actually put this into practice, all of the logic goes into a JS file. It must conform to AMD specification so it can be loaded by Require.js.
A load_ipython_extension function should be defined in the module - Jupyter will invoke this after it initializes its own components.
From the load_ipython_extension function, you can manipulate the DOM of the page, invoke several APIs provided by Jupyter, listen for Jupyter events, load other modules, etc.
Using User Contributed Notebook Extensions
Use this repo as a template: https://github.com/ipython-contrib/jupyter_contrib_nbextensions
This repo contains a large selection of extensions for notebook. An extension consists of a Javascript file and, optionally, a CSS file.
By loading an extension, you can provide functionality from the Javascript portion of a Jupyter notebook (ajax, updating text, rendering equations, etc.)
Start by checking out a copy (optional):
git clone https://github.com/ipython-contrib/jupyter_contrib_nbextensions
Now build nb extensions:
cd jupyter_contrib_nbextensions pip install -e .
or directly from pip to just use the extensions:
pip install jupyter_contrib_nbextensions
or directly from github to also just use the extensions:
pip install https://github.com/ipython-contrib/jupyter_contrib_nbextensions/tarball/master