How do I set up Jupyter/IPython Notebook for Django?
Here’s what just worked for me
install Django Extensions (I used 1.9.6)
check official site → click here
Installing
You can use pip to install django-extensions for usage:
$ pip install django-extensions
Development
Django-extensions is hosted on github:
https://github.com/django-extensions/django-extensions
Source code can be accessed by performing a Git clone.
Tracking the development version of django command extensions should be pretty stable and will keep you up-to-date with the latest fixes.
$ pip install -e git+https://github.com/django-extensions/django-extensions.git#egg=django-extensions
You find the sources in src/django-extensions now.
You can verify that the application is available on your PYTHONPATH by opening a python interpreter and entering the following commands:
>>> import django_extensions
>>> django_extensions.VERSION
(0, 8)
Keep in mind that the current code in the git repository may be different from the packaged release. It may contain bugs and backwards-incompatible changes but most likely also new goodies to play with.
Configuration
You will need to add the django_extensions application to the INSTALLED_APPS setting of your Django project settings.py file.:
INSTALLED_APPS = (
…
‘django_extensions’,
)
This will make sure that Django finds the additional management commands provided by django-extensions.
The next time you invoke ./manage.py help you should be able to see all the newly available commands.
Some commands or options require additional applications or python libraries, for example:
‘export_emails’ will require the python vobject module to create vcard files.
‘graph_models’ requires pygraphviz to render directly to image file.
If the given application or python library is not installed on your system (or not in the python path) the executed command will raise an exception and inform you of the missing dependency.
as per other answers
install jupyter
pip install jupyter
some stuff I did to setup jupyter inside my Docker container — see below if this applies to you †
from your base Django directory, create a directory for notebooks,
mkdir notebooks
Go to that directory
cd notebooks
start django extensions shell_plus from inside that directory:
../manage.py shell — notebook
The notebook server should now be running, and may launch a new browser. If it doesn’t launch a browser window, follow the instructions to paste a link or a token.
from the browser, open a new “Django Shell Plus” notebook, as per John Mee’s answer’s screenshot
AND, importantly, what didn’t work was changing directories from inside the notebook environment. If I tried to work with any notebook that was not in the directory that manage.py shell_plus — notebook was run in, then the kernal was not configured correctly. For me, having the notebook be configured for just a single directory at a time was good enough. If you need a more robust solution, you should be able set PYTHONPATH prior to starting jupyter. For example add export
PYTHONPATH=”$PYTHONPATH:/path/to/django/project” to a virtualenv activate script. But I haven’t tried this.
† Docker Setup (optional)
add a port mapping for your container for port 8888
For example, in your docker compose file;
ports:
— “8890:8888”
Configure your project settings file to use ip 0.0.0.0
This is what I did:
NOTEBOOK_ARGUMENTS = [
‘ — ip’, ‘0.0.0.0’,
‘ — allow-root’,
‘ — no-browser’,
]