Well, what a nightmare that was.

My quest to see if I could get Dark Mode working for Jupyter Notebook on a 64-bit Windows 10 machine ended up costing me 3 nights.

Yet the desire to have my Jupyter Notebooks look like this…

image
Fig 1 – Jupyter Notebook dark theme, courtesy of dunovank’s Jupyter-themes github.

…was just too strong.

I began as I usually do on a Linux machine running virtual python environments, and that is to command-line in to Python environment using Hyper in Windows. This being the same environment that my Windows System Environment PATH variable is pointing to, and the same environment that I knew I had installed Python 3.7 to some months ago.

However, I was quickly realising that since I had then installed Jupyter Notebook as part of the Anaconda Package, that my pip installs and updates to various packages where not being picked up by my notebook.

It seemed Jupyter Notebook was running off a different Python environment.

But where?

I couldn’t remember setting one up.

It turns out that Anaconda likes to create its own Python environment buried deep within the user’s roamin appdata (took me forever to find this). Furthermore, it also uses IPython, a rich toolkit shell for base Python, and this is installed somewhere just as deep and just as cryptic.

In the end, I eventually uninstalled all my attempts, started a fresh Jupyter Notebook session and installed everything from the notebook. It’s a bit cringeworthy as I like to do everything on the command line and safely within virtual environments, but since this is just Windows, meh, whatever.

So let’s get started.

Requirements

First some requirements. Obviously you have to have these installed, and matplotlib so we can chart some stuff.

  1. Python 3.4, 3.5, 3.6, 3.7 and 3.8
  2. Jupyter
  3. Matplotlib

This blog assumes that you are using Anaconda package manager for your Python packages in Windows 10.

Upgrade Jupyter Notebook

I hadn’t upgraded in a while, so I opened up Anaconda Prompt and ran:

pip install --upgrade notebook --user

The above upgrade should install the following two vital packages

pip install msgpack
pip install tornado

Next, time up make sure you have upgraded pip:

python -m pip install --upgrade pip

Good. Pip upgraded to 21.2.4.

We should be able to fire up a new Jupyter Notebook session:

jupyter notebook

You should get a Browser window up.

Navigate using the folders to any area where you want to run some code. In the top right-hand corner, click New and in the drop-down box click Python 3 to create a new.

At this point you may encounter a 500: Internal Server Error message. Please consult this section in the Troubleshooting section at the end of this article.

Save your new untitled python notebook something like: install.ipynb, because we will be using it to run some preliminary setup commands in the next section.

Install Themes with Jupyter Notebook

So you have started a new Jupyter Notebook session (Start Menu > Jupyter Notebook, or ‘jupyter notebook’ on the Anaconda prompt command line), and you are staring at a blank notebook. It’s horrible and white and you’d like to see some Dark Mode action!

We will install the dark mode theme from the notebook. To install packages from a Jupyter Notebook, all you need to know is the exclaimation point operator. This is how you can trick Jupyter in to thinking it is a command line.

Before we can commence we need to ensure that the Jupyter version is higher than

In [1]: !jupyter --version
jupyter core     : 4.7.1
jupyter-notebook : 6.4.3

Good, the Notebook version is higher than 5.6.0 (lowest compatible version with JupyterThemes).

Instal the JupyterThemes package:

In [1]: !pip install jupyterthemes

Please refer to dunovank‘s GitHub for reference: https://github.com/dunovank/jupyter-themes.

Next, upgrade what you just installed:

In [1]: !pip install --upgrade jupyterthemes

JupyterThemes should now be installed (to Anaconda’s mysteriously hidden local roaming environment)!

Install Your First Dark Theme

Try the following as your first dark theme:

In [1]: !jt -t monokai -f fira -fs 13 -nf ptsans -nfs 11 -N -kl -cursw 5 -cursc r -cellw 95% -T

You will need to fully restart Jupyter Notebook for the theme to take effect.

Note: at this point you may get the dreaded: ‘jt is not a recognised command’. Please refer to the troublshooting section at the end of this article for some fixes.

Finally! Success

Fig 2 – Finally, success! Jupyter Notebook fires up in dark mode.

But the troubles aren’t over just yet!

Fix Black Matplotlib Axes

When I plot using matplotlib I end up with black axes:

Fig 3 – Oh oh! Black axes in Dark Mode is not good!

To fix, I used Raveen Beemsingh’s recommendation here, and went to implement the hack.

However, I had to find precisely where to save down the python hack.

Jupyter Notebook Profile Default Startup

After a while searching, I finally found where Jupyter Notebook’s profile default startup folder is:

C:/Users/[My Name]/.ipython/

Profiles are typically stored in ~/.ipython or ~/.config/ipython, where ~ is the current user’s home directory. This directory is typically called the IPython directory and is sometimes referred to as IPYTHONDIR. To find the exact location of the profiles, we can run the ipython locate command for the IPython configuration directory:

By default, IPython starts with the default…

In [1]: !ipython locate
C:/Users/[Your Username]/.ipython

Navigating to this folder in File Explorer, I found the profile_default subdirectory and then the startup folder.

In this folder I created a new Python file called 00_startup.py and put Raveen’s snippet in there:

import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from jupyterthemes import jtplot
jtplot.style(theme='monokai', context='notebook', ticks=True, grid=False)

NB: It’s probably a good idea to see if this program compiles before using it, just to make sure all the syntax is correct (when I copied it, it had copied the wrong inverted commas!)

Now fully restart your Jupyter session and then fully re-run your notebook to see the changes:

Fig 4 – Fixed! Dark Mode matplotlib charts rendering correctly now!

Troubleshooting

Could not install due to WinError 5 Access is Denied

If you get these error

Fig – Pip Install Jupyterthemes resulted in this error.

then simply add “–user” at the end like so:

> pip install jupyterthemes --user

it should install now.

‘jt’ is not a recognised command

You haven’t installed JupyterThemes. If you are sure that you have (like I was), then you haven’t installed JupyterThemes in to the correct site-packages folder. If you are using Windows 10 (like me), there are actually quite a few “environments” that need to be addressed. Things are further complicated if you are using a package manager like Anaconda; because it likes to automatically create a local virtual environment in the Windows user’s local roaming appdata folder. There will also be another base installation where you actually installed Anaconda, and I actually found another one randomly in My Documents.

Even though I had been managing my Python PATH variable in Windows Environment Variables manager, the path to your local roaming Anaconda environment could very well be different; and this will explain why Jupyter Notebook might not recognise packages you were sure your pip-installed on the command line.

To fix Python not recognising ‘jt’ as a command (even though you know for certain it is installed and it is even showing up in pip list), you need to first check your Windows Environment Variables:

Steps to Fix

  1. Start Menu. Search for “Edit the System Environment Variables”.
  2. In the System Properties window, click on the Advanced tab, and then click the Environment Variables button.
  3. Under User Variables (the top window), click on Path in the list, and then click Edit…
  4. You will probably notice that Python is missing from your Path variable. This is why the command it not recognised.
  5. Click New and the click browse.
  6. Now you have to browse to Anaconda’s recognised python environment.
  7. For me, Anaconda had installed a virtual python environment at this location:
C:/Users/[Your Username]/AppData/Local/Programs/Python/Python37

But sometimes it might be here:

C:/Users/[Your Username]/AppData/Roaming/Python/Python36

But it could also be someplace else! Thanks Windows.

Don’t forget to also add the Scripts path:

C:/Users/[Your Username]/AppData/Local/Programs/Python/Python37/Scripts

Restart your Jupyter session and it should now recognise jt.

Starting Jupyter Notebook from Windows ImportError Secure_Write

If (like me) you haven’t run Jupyter Notebook for a while you might meet this error the first time you fire up a new session:

To fix this and be able to fire up new Jupyter Notebook sessions you need first need to upgrade the jupyter client:

pip install --upgrade jupyter_client

500 Internal Server Error

The first time you create a new Notebook in your new Jupyter session you may encounter the 500: Internal Server Error. Simply quit the notebook session and go back to your command line. Run the following upgrade:

https://github.com/jupyter/notebook/issues/5014

pip install --upgrade nbconvert

and restart Jupyter Notebook and try creating a new Python 3 notebook. You should now be able to create new notebooks.

CondaValueError: Malformed version string ‘~’: invalid character(s)

Simply update Conda

conda upgrade -n base conda

Wait for the updates:

Fig 5 – Updating Base Conda

References

  1. https://github.com/dunovank/jupyter-themes
  2. https://medium.com/@rbmsingh/making-jupyter-dark-mode-great-5adaedd814db
  3. https://hyper.is/
  4. https://pythonforundergradengineers.com/opening-a-jupyter-notebook-on-windows.html
  5. https://github.com/jupyter/notebook/issues/5014
  6. http://ipython.org/ipython-doc/rel-0.10.2/html/config/customization.html
  7. https://stackoverflow.com/questions/12370457/what-is-the-difference-between-python-and-ipython