pydanny / dj-notebook

Django + shell_plus + Jupyter notebooks made easy.
https://dj-notebook.readthedocs.io/
GNU General Public License v3.0
127 stars 21 forks source link
django django-extensions jupyter-notebook notebook

dj-notebook

Django + shell_plus + Jupyter notebooks made easy


A Jupyter notebook with access to objects from the Django ORM is a powerful tool to introspect data and run ad-hoc queries.

Full documentation available at dj-notebook


Features

The ever-growing list of features:

Installation

Use your installation tool of choice, here we use venv and pip:

python -m venv venv
source venv/bin/activate
pip install dj_notebook

Usage

First, find your project's manage.py file and open it. Copy whatever is being set to DJANGO_SETTINGS_MODULE into your clipboard.

Create an ipython notebook in the same directory as manage.py, or another directory of your choosing. In VSCode, simply add a new .ipynb file. If using Jupyter Lab, use the File -> New -> Notebook menu option.

Then in the first cell enter:

from dj_notebook import activate

plus = activate()

# If you have created your notebook in a different directory, instead do:
# plus = activate(search_dir="/path/to/your/project")

# If that throws an error, try one of the following:

# DJANGO_SETTINGS_MODULE_VALUE aka "book_store.settings"
# plus = activate("DJANGO_SETTINGS_MODULE_VALUE")

# Point to location of dotenv file with Django settings
# plus = activate(dotenv_file='.env')

In future cells, you can now load and run Django objects, including the ORM. This three line snippet should give an idea of what you can now do:

from django.contrib.auth import get_user_model
User = get_user_model()
User.objects.all()

Usage Plus

But wait, it gets better!

When you activated the Django environment, you instantiated a variable called 'plus'. The 'plus' variable is an object that contains everything loaded from django-extensions' shell_plus. Here's a demonstration, try running this snippet:

plus.User.objects.all()

We also provide a utility for introspection of classes, which can be useful in sophisticated project architectures. Running this code in a Jupyter notebook shell:

plus.diagram(plus.User)

Generates this image

Here's another useful diagram:

plus.model_graph(plus.User)

QuerySet to Dataframe

Want to convert a Django query to a Pandas Dataframe? We got you covered.

plus.read_frame(plus.User.objects.all())

More things you can do!

dj-notebook official documentation

Contributors

pydanny
Daniel Roy Greenfeld
skyforest
Cody Antunez
geoffbeier
Geoff Beier
nmpowell
Nick Powell
specbeck
Saransh Sood
anna-zhydko
Anna Zhydko
Tejoooo
Tejo Kaushal
bloodearnest
Simon Davy
akashverma0786
Null
DaveParr
Dave Parr
syyong
Siew-Yit Yong

Special thanks

These are people who aren't in our formal git history but should be.

prestto
Tom Preston
evieclutton
Null
tim-schilling
Tim Schilling
meshy
Charlie Denton
chrisdev
Christopher Clarke
delfick
Stephen Moore
django-extensions
Django Extensions

Construction

This package was created with Cookiecutter and the simplicity project template.