How do I set up Jupyter/IPython Notebook for Django?
I have been using the method described in this post for setting up IPython Notebook to play nicely with Django. The gist of the method is to create an IPython extension which sets the DJANGO_SETTINGS_MODULE and runs django.setup() when IPython starts.
The code for the extension is:
def load_ipython_extension(ipython): # The `ipython` argument is the currently active `InteractiveShell` # instance, which can be used in any way. This allows you to register # new magics or aliases, for example. try: import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") import django django.setup() except ImportError: pass
With a recent upgrade to Jupyter Notebook this setup is now broken for me. I am able to run Django code in the Jupyter notebook by adding a similar bit of code to the first cell of the notebook. However, I was not able to figure out how to get Jupyter to run the extension automatically so I would not have to do this again for each and every notebook I am creating.
What should I do to get Django and Jupyter to play nicely?
UPDATE: For @DarkLight - I am using Django 1.8.5 with Jupyter 1.0.0. The code I run in the notebook is:
import os, sys sys.path.insert(0, '/path/to/project') os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settingsfile") import django django.setup()
How to use Django in Jupyter Notebook - Ayuth's Story, Running Django, especially Django querysets in jupyter notebooks might be Setup. I assume that you have python installed on your machine and you are (.venv) $ pip install django jupyter ipython django-extensions. I want to setup a Jupyter Notebook in a new container, and be able to have kernels for all these django projects (kernels that use their shell and environment). Any ideas? I came across this answer but this can only be used for when the django project and the jupyter notebook are running on the container :
pip install django-extensions
Change your settings file to include 'django-extensions'
INSTALLED_APPS += ['django_extensions']
Run your Django server like this:
python manage.py shell_plus --notebook
alter to suit, and run this in your first cell
import os, sys PWD = os.getenv('PWD') os.chdir(PWD) sys.path.insert(0, os.getenv('PWD')) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "local_settings.py") import django django.setup()
Now you should be able to import your django models etc. eg:
from app.models import Foobar Foobar.objects.all()
How to use the Django shell in Jupyter Notebook?, Our environment will be setup inside a virtual environment using Django 2.2. Once inside the virtual environment, install IPython, Jupyter, django-extensions: Provides the shell_plus management command that makes it easy to run a notebook server in your Django project; Jupyter Notebook. Notebook Basics: One of several example notebooks in the Jupyter docs; IPython Examples: Notebooks demonstrating the features of the backend (aka "kernel") used for running Python code in Jupyter
Here's what just worked for me
- install Django Extensions (I used 1.9.6) 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, e.g.
- Go to that directory
- start django extensions shell_plus from inside that directory:
../manage.py shell_plus --notebookThe 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.
† 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', ]
Django shell_plus with Pandas, and Jupyter Notebook, Using Jupyter Notebook with Django. These are the notebooks used for my presentation at the Django Boston Meetup. On GitHub, view any of the .ipynb files to Project Jupyter succeeded Ipython Notebook and is based on Ipython as it makes use of its kernel to do all the computations and then serves the output to the front-end interface. The kernel provides the multiple language support to Jupyter notebooks(R, Python, Julia, Java, etc) and it extends Ipython’s store and output features to build a
Note: I am using Python 3.7 and Django 2.1, it works for Django 2.2. I don't have to run anything in my first cell, and this works like charm as long as you don't mind having the notebooks in the root of your Django project.
It is assumed that you have a virtual environment for your project, and it is activated. I use
pipenv to create virtual environments and track dependencies of my python projects, but it is up to you what tool you use.
It is also assumed that you have created a Django project and your current working directory is the root of this project.
pip install jupyter
pipenv install jupyter
pip install django-extensions
pipenv install django-extensions
django-extensionsby adding it to the
INSTALLED_APPSsetting of your Django project
INSTALLED_APPS = ( ... 'django_extensions', )
shell_plusmanagement command that is part of
django-extensions. Use the option
--notebookto start a notebook:
python manage.py shell_plus --notebook
Jupyter Notebooks will open automatically in your browser.
Start a new Django Shell-Plus notebook
Again, you don't have to run anything in the first cell, and you can corroborate by running
dir() to see the names in the current local scope.
If you want to put your notebooks in a directory called
notebooks at the root directory, you can do the following:
$ mkdir notebooks && cd notebooks $ python ../manage.py shell_plus --notebook
Thanks to Mark Chackerian whose answer provided the idea to make run the notebooks in a directory other than the project's root.
These are the modules that are imported automatically thanks to
# Shell Plus Model Imports from django.contrib.admin.models import LogEntry from django.contrib.auth.models import Group, Permission, User from django.contrib.contenttypes.models import ContentType from django.contrib.sessions.models import Session # Shell Plus Django Imports from django.core.cache import cache from django.conf import settings from django.contrib.auth import get_user_model from django.db import transaction from django.db.models import Avg, Case, Count, F, Max, Min, Prefetch, Q, Sum, When, Exists, OuterRef, Subquery from django.utils import timezone from django.urls import reverse
bhrutledge/jupyter-django: Using Jupyter Notebook with , This tutorial will walk you through setting up Jupyter Notebook to run either locally or from an Ubuntu 18.04 server, as well as teach you how to Jupyter Notebook offers a command shell for interactive computing as a web application. The tool can be used with several languages, including Python, Julia, R, Haskell, and Ruby. It is often used for working with data, statistical modeling, and machine learning. This tutorial will walk you through setting up Jupyter Notebook to run either
Actually turns out you (might not) need to do all that crap. Just install django-extensions and run jupyter!
(myprojectvenv)$ cd myproject (myprojectvenv)$ pip install jupyter (myprojectvenv)$ pip install django-extensions (myprojectvenv)$ jupyter notebook
In the browser, start a new "Django Shell-Plus":
And you should be good to go. eg:
from myproject.models import Foobar Foobar.objects.all()
How To Set Up Jupyter Notebook for Python 3, This information explains how to install the Jupyter Notebook and the IPython kernel. Prerequisite: Python¶. While Jupyter runs code in many I'm new to jupyter/ipython and django. I have Django 1.9.5 installed on Ubuntu 16.04 and my application is running fine. I want to configure iPython(version 5.1.0) to be used with my Django application.
Installing Jupyter Notebook, This tutorial explains how to install, run, and use Jupyter Notebooks for Alternatively, if you're working with Python 3 and you want to set up a I have been using the method described in this post for setting up IPython Notebook to play nicely with Django. The gist of the method is to create an IPython extension which sets the DJANGO_SETTINGS_MODULE and runs django.setup() when IPython starts.
(Tutorial) Jupyter Notebook: The Definitive Guide, If you have a different directory structure just make sure you put the proper path in. You should now have the IPython notebook up and running. If The Jupyter project is the successor to the earlier IPython Notebook, which was first published as a prototype in 2010. Although it is possible to use many different programming languages within Jupyter Notebooks, this article will focus on Python as it is the most common use case. (Among R users, R Studio tends to be a more popular choice).
Django with IPython Notebook (Jupyter), That said, if you have experience with another language, the Python in this article shouldn't be too cryptic, and will still help you get Jupyter Notebooks set up $ ./manage.py shell_plus --notebook. Without Django Extensions. If you’d rather not use the Django Extensions app, you can load an IPython extension that performs the imports that an IPython session needs to run Django. I’ve packaged up such an extension (very simple) that you can install from by executing this command from within IPython: