The Jupyter Notebook: 3 More Little-Known Tricks


This is the second part of the blog post titled The Jupyter Notebook: 3 Little-Known Tricks. We’ll explore here 3 more tricks that will greatly expand the potential of your notebook, from how it looks to what language you’ll be coding with!

Let’s get started!

Change your notebook appearance and functionality: custom.js

Jupyter provides an entry point to inject Javascript into your notebook: custom.js. Changing this file allows an extremely powerful customization of the notebook, from its appearance to its functionalities. custom.js is executed when you start the notebook app, and it’s located at ~/.jupyter/custom/custom.js. If this path does not exist on your machine, you can simply create it and Jupyter will look for it there. Here are just a few examples of what you can do with it!

Does the auto closing parenthesis feature annoy you to no end? Simply add:

IPython.CodeCell.options_default.cm_config.autoCloseBrackets = false;

How about displaying line numbers by default? In this case add:

IPython.Cell.options_default.cm_config.lineNumbers = true;

You can also remove the header of the notebook while keeping the menus:

$([]).on(“app_initialized.NotebookApp”, function () {

Go ahead and modify it, changes should take effect immediately after a refresh! If that doesn’t work, a server restart will be necessary.

Comment out large snippets AND maintain syntax highlighting!

There are two ways of adding comments to Python code. Single-line comments begin with the pound character # and are terminated by the end of line, multi-line comments have “”” or '” as the delimiter on each end. Typically, neither of these two methods maintain the syntax highlighting in common text editors, and for a good reason: comments should be visually different from the rest of the code.

But the Jupyter notebook is not a common text editor! So here is a way to keep the best of both worlds. First, convert your cell into a markdown cell, then prepend ```python — code block — ```, like so:

Python Code Block

This effectively disables the cell, while keeping its good looks!

Write and execute functions in languages other than Python

Of course, Python is great. It’s one of the most readable and easy-to-use programming languages. Its built-in, high-level data types (e.g. dictionaries) and dynamic typing (i.e. no need to declare the types of arguments and variables) allow Python code to be on average 3–5 times shorter than analogous Java code, and 5–10 times shorter than C++ code.

But don’t you ever feel nostalgic? Don’t you wake up at night thinking “I really wish I could use Fortran in my Jupyter notebook”? Well, worry no more. By installing and loading the fortranmagicmodule, your dream will come true. First run !pip install fortran-magic, then

First run !pip install fortran-magic

For pretty much any other language, check out the Jupyter Kernels!

Data Science Jupyter Notebook Jupyter