How to Install Jupyter Notebook in a Dockerized Django Project

Having shell with full access to the project files and database is crucial to its development and testing.

In this tutorial, we will use Cookiecutter Django.
First, we need to install and run it:

1
2
sudo pip install "cookiecutter>=1.4.0"
cookiecutter https://github.com/pydanny/cookiecutter-django

Cookiecutter will ask us for configuration options. We can keep pressing enter (selecting default option) until the use_docker [n]: which we accept by y.

Start here if you already have a project

First, we add Jupyter to the project requirements in requirements/local.txt.
We can do it by appending jupyter==1.0.0 at the end of the file.

We modify settings to allow jupyter running in a container. At the end of the file config/settings/local.py we add:

1
2
3
4
5
6
NOTEBOOK_ARGUMENTS = [
'--ip', '0.0.0.0',
'--port', '8888',
'--allow-root',
'--no-browser',
]

Next, we build and run the project:

1
2
cd project_name
docker-compose -f local.yml up --build

As we can see on http://127.0.0.1:8000/, the project runs perfectly.

Now we can run the Jupyter Notebook:

1
docker-compose -f local.yml run -p 8888:8888 django python manage.py shell_plus --notebook

And open the link displayed in the console to access the notebook (something like http://0.0.0.0:8888/?token=secrettoken).

Now our notebook has access to the project database and files.

After modifying project files, there is no need to restart the whole shell, like we have to do in using shell_plus without the online notebook. We just need to restart kernel in the web interface.

If you have any question or problem with it, let me know in the comments.