How to Write a Jupyter Notebook Extension

(Source)How to Write a Jupyter Notebook ExtensionMake the Jupyter Notebook your playgroundWilliam KoehrsenBlockedUnblockFollowFollowingDec 8Jupyter Notebook Extensions are simple add-ons which can significantly improve your productivity in the notebook environment..While there are numerous existing extensions, we can also write our own extension to do extend the functionality of Jupyter.In this article, we’ll see how to write a Jupyter Notebook extension that adds a default cell to the top of each new notebook which is useful when there are libraries you find yourself importing into every notebook..The complete code for this extension is available on GitHub.The final outcome of the Default cell extension is shown below:Extension to add a default cell to the top of every notebook.Notes before Starting(If you don’t yet have Jupyter Extensions, check out this article or just run the following code in a command prompt: pip install jupyter_contrib_nbextensions && jupyter contrib nbextensions install and then start a new notebook server and navigate to the extensions tab).Unfortunately, there’s not much official documentation on writing your own extension..This folder, in turn, needs to be in the nbextensions subdirectory of the jupyter_contrib_extensions library (what you installed with pip. To find the location of a library installed with pip run pip view package. )My jupyter_contrib_extensions directory is:/usr/local/lib/python3.6/site-packages/jupyter_contrib_nbextensions/nbextensionsSo the file structure looks like (with nbextensions as shown above):nbextensions/ default_cell/ – description.yaml – main.js – README.mdWhen you run jupyter notebook, Jupyter looks in this location for extensions, and shows them on the extensions tab on the server:Jupyter Notebook Extensions tabWhen you make a change to the extension files while developing and you want to see the effects in a Jupyter Notebook,you need to run the command jupyter contrib nbextensions install to rewrite the Jupyter config files..This is displayed on the extensions tab:README rendered on the extensions tab(The actual code is pretty boring but for completeness, here it is)Default Cell=========Adds and runs a default cell at the top of each new notebook..Feel free to make contributions on GitHub or contact the author (Will Koehrsen) at wjk68@case.eduOnce you have the three required files, your extension is complete.To see the extension working, make sure the default_cell directory is in the correct location, run jupyter contrib nbextensions install and start up a Jupyter Notebook server..If you navigate to the NBExtensions tab, you will be able to enable the extension (if you don’t have the tab, open a notebook and got to Edit > nbextensions config).Extension enabledStart up a new notebook and see your extension at work:Default cell jupyter notebook extensionThis extension isn’t life-changing, but it might save you a few seconds!ConclusionsPart of the joy of gaining computer literacy is realizing that if you want to accomplish something on a computer, chances are that you probably can with the right tools and willingness to learn.. More details

Leave a Reply