Jupyter Dark Mode & Themes
Dark Mode, Customized Fonts, Text Size & More…
Staring at a screen for hours on end, especially at night has been the literal call-to-arms for the emergence of dark mode in websites, browsers, mobile operating systems, and web APIs. Jupyter notebook is no exception, and while other IDE’s like Spyder, PyCharm, and VS Code offers built-in dark modes for that all so easy-on-the-eyes look, unfortunately, the same cannot be said for Jupyter.
This doesn’t mean all is lost, however. Jupyter Themes exists as an open-source library created and developed by Kyle Dunovan; and is used to change themes for notebooks including colors, fonts, text size, and graph visuals and plot styles. The theme package is easily installable by using either pip or conda (depending on your environment) and is customizable to be either globally changed to your notebooks or on a case-by-case basis. There are great references on this topic including other blogs, Dunovan’s GitHub repo, and stack overflow.
Want to read this story later? Save it in Journal.
You may be asking yourself then, “what the hell is the purpose of this blog then…?”. The answer is two-fold. Firstly, Jupyter Themes not only changes how your notebook looks, but at times things can be out of place, and it takes some work to get them back to some semblance of working order. Secondly, there are only 9 available themes to choose from despite years of availability, with no sign of planned updates in the future from Dunovan, unless someone else decides to take the reigns.
My solution to this issue gives great flexibility to how you customize your themes and is specially tailored to those who like their computer screens to not angrily burn their retinas every time they look at them. But even so, let’s go ahead and take a look at both options.
Installing and Using Jupyter Themes
Jupyter Themes can be easily installed as described above, using pip or conda.
From here you can do much, and here is a snapshot of all of your available command line inputs.
Note that when changing anything with Jupyter Themes, you can do so either in your terminal (or Anaconda Prompt) or inside of the notebook using the magic notation:
Here are screenshots showcasing the nine available themes and how they look:
Also, be sure to refresh your kernel after applying any changes. Also note that these changes are global and made to run every time you run any notebook on startup, and are somewhat customizable. This blog post does a nice job of showcasing what you can do with the theme package.
Using The Darkmode Google Chrome Extention with Jupyter
If only having 9 options to play around with is a non-starter for you, or if like me you just like to have options, you can customize your themes and dark modes using the Google extension aptly named ‘darkmode’.
The dark mode extension is fully customizable, doesn’t change the behavior of your Jupyter Notebook, and can be made to ignore certain websites or run on only those you specifically select. This flexibility lets you ignore websites as you specify allows the extension to not interfere with sites or APIs that have their own integrated dark modes.
You can also run the extension in conjunction with Jupyter Themes to create even more customization (as jt not only changes the color/background of your notebook but fonts and text size are adjustable as well).
I’ve personally found that the -dark invert- options in the general dark styles (b) section work best for Jupyter notebooks while maintaining the color highlighting of code that we are used to normally.
For those HTML-versed individuals, you can even write your own custom dark theme, which of course does not necessarily need to be ‘dark’ at all.
When using the dark mode extension, you’ll notice that dark labeled axes on visualizations are invisible due to being rendered against the dark backgrounds. One fix is to use the matplotlib style sheets reference themes available on their website.
You can call the matplotlib style you’d like to use globally in an import statement at the beginning of your notebook as such:
As a tradeoff, this sets the new default as whatever new style you’ve selected, and won’t be reset unless you specifically specify it to using:
One way around this is to call the plot style locally using a with statement, ahead of your plot syntax. The plot code is then indented under your import statement in the cell you are working in.
Personally, I like this option as it's only a few more lines of code, but gives you more freedom and flexibility in how you plot your visualizations. Most importantly, it also works with seaborn as well! Note whatever style you specify when saving images using plt.savefig() will be in the format of whatever style you have set globally or inline.
Time to Play with All of These Options!
With so many options at your fingertips for customizing your notebooks, it’s easy to fall down the rabbit hole and spend hours upon hours getting that perfect look. Have fun getting your laptop or rig configured for those long hours of coding!
References, Resources, and Further Reading
Jupyter Themes Github Repo: https://github.com/dunovank/jupyter-themes
Making Jupyter Dark Mode Great!: https://medium.com/@rbmsingh/making-jupyter-dark-mode-great-5adaedd814db
Customize Your Jupyter Notebook Theme in 2 Lines of Code: https://towardsdatascience.com/customize-your-jupyter-notebook-theme-in-2-lines-of-code-fc726cea1513
Matplotlib Style Sheets Reference: https://matplotlib.org/stable/gallery/style_sheets/style_sheets_reference.html
📝 Save this story in Journal.