10 Jupyter Notebook Features You Didn’t Know they exist

Go beyond the basics: advanced features to make the most of Jupyter Notebooks

Mohab A.Karim
The Pub
5 min readSep 8, 2024

--

Jupyter Notebooks have become the tool of choice for special creatures J data scientists, K machine learning engineers, and L Python developers all around! One of the strengths of Jupyter is that it provides an interactive environment where users can integrate code, text, and visualizations all in one. However, most users know enough of the basic Jupyter Notebook features while rarely even finding out about hidden gems that can make a difference and save precious time.

Here, we share a few lesser-known features in this article to help you unleash the true power of Jupyter Notebooks while working on your data science projects.

1. Magic commands — time-saver shortcuts

This comes with a collection of built-in magic commands to be utilized within Jupyter Notebooks enabling more efficient code execution. Magic commands can be identified by the fact that they start with % for line magics or %% in case they are cell magics. It greatly simplifies things like executing shell commands, benchmarking your code, or even dealing with memory.

Example:

%timeit sum(range(100000))

It is the simplest command you can run to get a high-level overview of how your code is performing and will save you hours but days at times.

2. Interactive widgets: elevate your user experience

IpyWidgets — A Jupyter Notebook extension for building interactive features such as sliders, drop-down lists, or buttons in the notebook side by side. Such widgets are super handy in cases of exploratory data analysis and visualization where you will often need to adjust certain parameters on the fly.

Example:

from Ipywidgets, import interact

def square(x):
return x * x

interact(square, x=(0, 10));

In the example, interact() creates an interactive slider that makes the output dynamic as you change its value. This makes it possible for you to create interactive dashboards or tools for parameter tuning thus improving user experience greatly.

3. Auto-reload: stay up-to-date with your library modules

If you are developing other long-term projects, frequently updating the Python modules happens in real. The %autoreload magic will automatically reload modules before executing a new line; meaning you are always running the latest version of your code.

How to Use:

%load_ext autoreload
%autoreload 2

This means anytime you make a change in your .py file, it automatically will be reflected in the notebook (no need to restart Kernel).

4. Confused documentation: don’t go outside to learn

One of the best features of Jupyter Notebooks is the ability to access documentation straight from there. By appending ? or ?? This way, if you are working with a function or object and need to see its docstrings or source code, you can do this without leaving your notebook.

Example:

print?

This command will show documentation for the print() function. If you append ??, you will get the source code of the function means that is a default method that implements specific behavior and how it works.

5. Expanding and collapsing headings — work better with the cleaver organization

It becomes very soon confusing if there are too many code blocks and markdown cells in a huge notebook. Collapsible Headings: This gives you another level of allowing a more optimized hyper-tuning pipeline by being able to hide anything that is not needed at the moment but still keep in view.

For collapsible headings, you need to install the nbextensions package:

pip install jupyter-contrib-nbextensions

The Collapsible Headings extension can be activated from the Jupyter interface, so if you have already installed this extension now is the time to enable it. This feature is relatively simple but it helps with readability and when navigating within long notebooks.

6. Using nbconvert: exporting Notebooks as other formats

If you want to produce your notebook in the form of a report or for blogging you can make use of Jupyter’s nbconvert which lets one convert their notebooks into multiple formats including HTML, PDF, and LaTeX as well.

jupyter nbconvert --to html notebook.ipynb

This series of commands will then convert your notebook into an HTML file that you can share with non-technical stakeholders or even publish online.

7. Variable inspector (view all variables at a glance)

This can be complicated to manage particularly in long and complex notebooks. The Variable Inspector extension allows us to see all currently running variables: type, size, and value in a clean popup display.

You can do this through the Jupyter interface, but be sure you are going to install nbextensions and enable Variable Inspector.

8. Jupyter Lab is easily my most used environment as a power user.

JupyterLab is the next-generation programming environment for Jupyter, and it includes built-in support for legacy kernels. It comes with an improved user experience more flexibly and powerfully; features included are multiple panes, tabs for views within the same nodes list of your workspace, better support to manage files, etc.

You can have several notebooks, terminals, and text files opened side by side which is a time saver for multi-taskers.

9. Executing terminal commands in Notebooks

Simple shell commands do not require you to switch back and forth with the terminal. By prefixing the command with an exclamation mark (!).

Example:

!pip install pandas

This comes in handy if you want to quickly install or upgrade a package, or even move between directories without leaving your notebook.

10. Debugging with %debug

If this code is in a notebook, the %debug magic command enables you to switch to interactive debugging mode. %debug will drop you into the post-mortem debugger at the point in time where an error was thrown.

How to Use:

%debug

This will give you an interactive shell where you can check the variable values, step by line in code, and see what exactly went wrong.

Conclusion

There are things you can do within Jupyter Notebooks to save you time and streamline your workflow. Using magic commands, interactive widgets, and hidden functionalities such as %autoreload you can turn your notebooks from simple coding environments into strong development tools.

Learning these hidden features will not only increase your day-to-day productivity but also take your data science project to the next level. These tips and tricks work whether you are in college, presentation, or doing a big machine-learning model.

So now that you know about these hidden things in Jupyter, have fun playing with those during your next project and see how it can up our workflow. In the comments section below, tell us which features you found most beneficial.

--

--