Out-Of-This-World Django DX

Berel Levy
Stackademic
Published in
4 min readMay 7, 2024

--

How to set up the Jupyter Notebook VSCode extension in Django.

What’s Wrong with Earth’s Shell?

Testing code in the shell is a necessary part of django development, that goes without saying. But it’s an experience that leaves you wanting. The improvements suggested in Boost Your Django DX (Great book. Read it.) help a lot, but there’s something even better.

Imagine you could run your code right inside the file you’re editing! You get access to all the benefits available in your editor (intellisense, ctrl+clicking, etc.) and run it right then and there, fast as a repl. You can all this and more, with Jupyter Notebook.

Server or Plugin?

Jupyter Notebook comes in two flavors; as a web page and as an integrated VSCode plugin. The web page version is easier to get started with but the plugin version is much better. I’ll be showing you how to use the plugin version because that’s what I like, and there’s already an article that shows you how to set up the web page version: How to use Django in Jupyter Notebook by Ayuth Mangmesap.

Getting Started.

Make sure you have VSCode installed.

1. Install the Jupyter VSCode Extension

2. Make sure you have a virtual environment set up. If you don’t you can set one up by typingpython -m venv .venv into your terminal.

3. Next, install the Jupyter python library by typing pip install jupyter into your terminal.

4. Open the VSCode command palette (⇧⌘P on mac, ctrl+shift+p on windows) and choose the Python: Select Interpreter command.

4a. Choose the interpreter from your virtual environment. It will be something like path-to-your-venv/bin/python, if you created the virtual environment using python -m venv .venv it should look like the selection below.

Your Jupyter Notebook will use this interpreter and stay in sync with your project’s dependencies.

5. Create a jupyter notebook file like dev.ipynb in the root directory of your django project, next to your manage.py file. Since you’ll be using this file in dev it’s a good idea to add it to your .gitignore.

5a. Open the dev.ipynb file. It should look like this:

A Fresh Jupyter Notebook in vscode

6. Click on the Select Kernel button, in the top right corner of the open Notebook.

6a. Select the Python Environments… option. Then choose your project’s virtual environment. It’s the same one you chose in the Python: Select Interpreter menu above.

Congratulations! You’ve set up Jupyter Notebook. You should now be able to run vanilla python in it, try something simple, like a print statement.

Using Django in Your Jupyter Notebook.

If you try to do anything Django-related you’ll get an error like
ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. This is because a lot of setup happens when django starts up when you run python manage.py COMMAND and we’ll need to replicate this in the beginning of our Notebook to access all our Django stuffs.

Add the following code to the first cell:

import os, django 
os.environ["DJANGO_SETTINGS_MODULE"] = "<your.settings.module>"
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
django.setup()

The last line, django.setup() is the magic command that does what is says it does. This is what gets called early on when you run manage.py COMMAND.

The line above that, os.environ[“DJANGO_ALLOW_ASYNC_UNSAFE”] = “true” ensures that the ORM works smoothly in Jupyter Notebook. I won’t get into it here, but in short, Jupyter is async and the ORM isn’t. Just be carefull, don’t change this setting in your settings file because it’s bad for production.

The first two lines are pretty self explanatory.

Test it Out!

You can now test some Django code in Notebook and it will run properly, just like it would in the shell.

Autoreload

Add the following code above the django related code at the beginning of the first cell of your jupyter notebook:

%reload_ext autoreload        # NEW
%autoreload 2 # NEW

import os, django
os.environ["DJANGO_SETTINGS_MODULE"] = "<your.settings.module>"
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
django.setup()

This will reload everything whenever you change something in another file, similar to how runserver reloads when you change something. Unlike runserver however, you don’t lose all your variables. Also, since Django runs a lot of setup in the beginning, sometimes you’ll need to restart the whole Notebook from fresh. You can do this by clicking on the restart button on the top of the Notebook.

Restart is the solution to most problems anyway tbh

That’s it. Happy coding, and I’ll leave you to get aquainted with the joys of Jupyter NoteBook.

Check out https://code.visualstudio.com/docs/datascience/jupyter-notebooks for more info on customizing the Jupyter VSCode extension.

Stackademic 🎓

Thank you for reading until the end. Before you go:

--

--

Experienced software engineer and data engineer who enjoys the finer things in coding.