Customize your Jupyter Notebooks

Contents Outline

Customize your Jupyter Notebooks

Apr 23, 2021 4 minutes read

Introduction

Jupyter Notebook is a great programming environment and often the most popular choice for data scientists or data analysts that are coding in python. Unfortunately, its default settings do not allow the level of customization that you have with standard programming environments such as PyCharm or similar tools.

Jupyter Notebooks themes are trying to diminish this gap and allow you to make the notebook a bit prettier and also more functional using the themes. In this article, I will walk you through the installation process of Jupyter Notebook themes and show you some of their most important features.

Installation

Jupyter Notebook themes is an open-source library and can be installed with pip install. Just type the following code in a command line:

pip install jupyterthemes

This should trigger the installation of the latest version. Once done you should be able to switch between themes, adjust fonts used in the notebooks, or customize the style of the plots. We will go through these features in detail in the next sections.


Changing themes

After installation, you can launch Jupyter Notebooks as normal and inspect the themes from within the notebook itself.

In order to list all possible themes, you can use the following code:

!jt -l


As you can see there are currently nine themes available. In order to switch themes you can use this command:

!jt -t <theme_name>

Let’s choose onedork theme.

!jt -t onedork

You will see that the theme does not change immediately. Some people report that they can reload the page and see the effect after that. From my personal experience, I have to restart Jupyter Notebook in order for the theme to change. Just stop the notebook and launch it again. This is how it should look like with onedork theme when it is loaded.


Now, you can play with different themes and choose your favorite one.

What you will notice is that some parts of the standard GUI are not visible by default in the theme settings. I am referring to the part pictured below.

In order to switch the theme but keeping the standard GUI look, you could use the following variation of the code.

!jt -t <theme_name> -T -N -kl

With onedork theme, these would like that.

!jt -t onedork -T -N -kl

Restarting Jupyter Notebooks should give you the result similar to the screenshot below.


In order to restore the notebook to its default settings, you can use this code.

!jt -r

Note that I have shown the commands being executed in the Jupyter notebook itself but you can use them without the exclamation mark in the terminal window as well.


Setting up the graphing style

Once you are using the themes you will notice that the graphs created with Matplotlib library do not look the best. For example, this is a simple code to create a line chart.

import matplotlib.pyplot as plt
%matplotlib inline
bp_x = np.linspace(0, 2*np.pi, num=40, endpoint=True)
bp_y = np.sin(bp_x)
# Make the plot
plt.plot(bp_x, bp_y, linewidth=3, linestyle="--",
         color="blue", label=r"Legend label $\sin(x)$")
plt.xlabel(r"Description of $x$ coordinate (units)")
plt.ylabel(r"Description of $y$ coordinate (units)")
plt.title(r"Title here (remove for papers)")
plt.xlim(0, 2*np.pi)
plt.ylim(-1.1, 1.1)
plt.legend(loc="lower left")
plt.show()

And this is the screenshot of the plot that is created in the notebook as seen using the onedork theme without customization.


This definitely does not match the theme you have chosen. It turns out that in order to customize Matplotlib to match the theme you will need to add two additional lines of code at the top of your notebook.

from jupyterthemes import jtplot
jtplot.style()

Once you run the same chart code (with the two code lines from above added at the top of the notebook) you should see that the chart now matches the current style of the theme.


That looks much better!

You can actually change the graph style to match any theme that you want.

from jupyterthemes import jtplot
jtplot.style(<theme_name>)

If you do not give a parameter theme for the style function it will use the theme that is currently loaded in the notebook.

Note unlike with theme setting there is no need to restart the notebook by changing the Matplotlib graphing style.

Changing fonts

Jupyter Notebook themes do not only allow you to change themes but also do some additional customization regarding the fonts being used in the notebook.

You can change the fonts when loading a theme with jt command and adding some additional parameters. You can customize…
  • font used for code (-f) and its size (-fs),
  • font used for notebook (-nf) and its size (-nfs) ,
  • font used for text/markdown (-ft) and its size (-fts).

Summary

In this article, you have learned how to customize your standard Jupyter Notebook with Jupyter Notebook themes. We went through the details of the installation of the library and set up the themes including graph and font customization.

You should be able to try it yourself now.

Happy customizing!

Originally published at aboutdatablog.com:
Customize your Jupyter Notebooks with Jupyter Notebooks Themes, on January 20, 2021.
Join our private community in Discord

Keep up to date by participating in our global community of data scientists and AI enthusiasts. We discuss the latest developments in data science competitions, new techniques for solving complex challenges, AI and machine learning models, and much more!