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
Django + 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
- 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
- Generates visual maps of model relations
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()
# 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
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 |
Geoff Beier |
Saransh Sood |
Anna Zhydko |
Tejo Kaushal |
Simon Davy |
Null |
Dave Parr |
Siew-Yit Yong |
Special thanks
These are people who aren't in our formal git history but should be.
- Tom Preston did seminal work on Python paths that later became the foundation of dj-notebook
- Evie Clutton was co-author of a pull request and they don't show up in the contributor list above
- Tim Schilling assisted with the
model_graph
method - Charlie Denton is responsible for django-schema-graph, which we leverage as part of the
model_graph
feature - Christopher Clarke built
django-pandas
, which dj-notebook uses
Tom Preston |
Null |
Tim Schilling |
Charlie Denton |
Christopher Clarke |
Construction
This package was created with Cookiecutter and the simplicity project template.
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
File details
Details for the file dj_notebook-0.6.1.tar.gz
.
File metadata
- Download URL: dj_notebook-0.6.1.tar.gz
- Upload date:
- Size: 27.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c208e175f3852112ca3f427fc45e0a6b8de8296439e4e00969de271338fd246 |
|
MD5 | 3ce6cd296c35c8fe8e61b15a08c7ff9e |
|
BLAKE2b-256 | 6e3f53091c3cd22f58081ab188377e6155ac0e6229074a4793e65cc071e982f6 |
File details
Details for the file dj_notebook-0.6.1-py3-none-any.whl
.
File metadata
- Download URL: dj_notebook-0.6.1-py3-none-any.whl
- Upload date:
- Size: 21.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7aab52729d5a0d92debcf092b7faba86de86df3280c463acc41ea0d7f148a1ee |
|
MD5 | 43d38c04354ea21e36e9ae430b678ab1 |
|
BLAKE2b-256 | 080b81cc03316c3820eee3e26a5a8d662a11ecefeace00fb6f083380230bae57 |