A Jupyter notebook with access to objects from the Django ORM is a powerful tool to introspect data and run ad-hoc queries.
Project description
dj-notebook
A Jupyter notebook with access to objects from the Django ORM is a powerful tool to introspect data and run ad-hoc queries. This works with modern Django and Python 3.9, 3.10, and 3.11.
Features
- Easy ipython notebooks with Django
- Built-in integration with the imported objects from django-extensions
- Inheritance diagrams on any object, including ORM models
- Converts any Django QuerySet to Pandas Dataframe
- Handy function for displaying mermaid charts in
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
. 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("DJANGO_SETTINGS_MODULE_VALUE")
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
QuerySet to Dataframe
plus.read_frame(plus.User.objects.all())
Check out the official documentation for more things you can do!
dj-notebook official documentation
Contributors
Daniel Roy Greenfeld |
Cody Antunez |
Anna Zhydko |
Simon Davy |
Null |
Dave Parr |
Siew-Yit Yong |
Pair programmers
Being a co-author of a commit doesn't show up in the contributor list above, so the following is for anyone who has paired on a commit and deserves credit.
Null |
Special thanks to Tom Preston
Tom Preston did seminal work on Python paths that later became the foundation of dj-notebook.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for dj_notebook-0.4.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d9fb8191be58f3f4153232c4de9e62ae5d200e7e05879ffbc78c57f6bf26e588 |
|
MD5 | a3cbfce15bb4af07de098caadaa01107 |
|
BLAKE2b-256 | a25164659bd69788727886eddea0c79375721838ac8f5fdde6a035581ed78ca7 |