How to use Django in Jupyter Notebook
Django
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.
The Jupyter Notebook
The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more.
The objective of this blog is to let you can use Django in Jupyter Notebook so let’s get started.
Assumed that you installed all django dependencies and initialized a project. The next thing we will do is install all of the dependencies to make django can be run under jupyter notebook via (I recommend you to use virtualenv
instead of installing directly to your system)
$ pip install jupyter ipython django-extensions
then append django_extensions
in file settings.py
under INSTALLED_APPS
section
INSTALLED_APPS = [
...
'django_extensions',
]
And then let’s run jupyter notebook
$ python manage.py shell_plus --notebook
After that create a jupyter notebook file and type a simple command for import django models from django.db.models import Model
then try to execute using Shift + Enter
. If there are errors coming so you can use every command in jupyter notebook as same as python manage.py shell
.
If you are getting errors then try to change the kernel to our Django Shell-Plus
Have fun.