Usage¶
All examples here are based on dj-notebook's test project at:
github.com/pydanny/dj-notebook/blob/main/tests/django_test_project
Activation¶
First, activate the project. If that doesn't work, try one of the other methods for activating dj-notebook.
from dj_notebook import activate
plus = activate("book_store.settings")
Now we can use the ORM:
from django.contrib.auth import get_user_model
User = get_user_model()
# Clean up the users
User.objects.all().delete()
# Create some users
User.objects.create_user("Audrey")
User.objects.create_user("Daniel")
# Query the users
User.objects.all()
<QuerySet [<User: Audrey>, <User: Daniel>]>
Usage Plus¶
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, let try running this snippet:
plus.User.objects.all()
<QuerySet [<User: Audrey>, <User: Daniel>]>
Or this:
for perm in plus.Permission.objects.all()[:5]:
print(perm)
admin | log entry | Can add log entry admin | log entry | Can change log entry admin | log entry | Can delete log entry admin | log entry | Can view log entry auth | group | Can add group
Dataframes from QuerySets¶
New in dj-notebook 0.3.0
Powered by django-pandas, we can also trivially turn any Django QuerySet into a Dataframe.
plus.read_frame(plus.User.objects.all())
id | password | last_login | is_superuser | username | first_name | last_name | is_staff | is_active | date_joined | ||
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 30 | !e91xGexuejHJTtcK5UK6VLPs74uTojmlMVbBCHF1 | None | False | Audrey | False | True | 2023-10-21 12:54:07.318920+00:00 | |||
1 | 31 | !wqNcAZOEw4KUIQdaIzMyf46lgEYKbrLsGDHZgqtE | None | False | Daniel | False | True | 2023-10-21 12:54:07.319889+00:00 |
Diagrams of Objects¶
We're not done yet!
We also provide a utility for introspection of classes, which can be useful in sophisticated project architectures. Let's see what happens when we use the plus.diagram()
function:
plus.diagram(plus.User)
Visualizing relations between models¶
New in dj-notebook 0.5.0
Useful for introspecting new or existing projects!
plus.model_graph(plus.User)
Rendering mermaid diagrams using dj-notebook¶
New in dj-notebook 0.4.0
Mermaid is such a useful tool tool that dj-notebook provides a shortcut.
diagram = """
flowchart TD
A[pip install dj-notebook]
B[from dj_notebook import activate]
C["plus = activate()"]
D["plus.mermaid(diagram)"]
A -->|wait a few seconds| B
B -.-> C
C -.-> D
"""
plus.mermaid(diagram)
Printing what dj-notebook loads from django-extensions¶
Vastly improved in dj-notebook 0.4.0
There are two ways to get a list of the loaded items by dj-notebook's activate()
function:
# Print all the objects to the screen on activate
plus = activate(quiet_load=False)
# Print the objects to the screen at any time
plus.print()
Here is plus.print()
in action:
plus.print()
# Shell Plus Model Imports from book_outlet.models import Address, Author, Book, Country 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 from django.utils import timezone from django.urls import reverse from django.db.models import Exists, OuterRef, Subquery
Dataframes from CSVs¶
New in dj-notebook 0.7.0
This turns strings or files on defined paths into Dataframes.
csv_string = """Name,FirstLetter
Daniel,D
Audrey,A"""
# Also works with plus.csv_to_df(pathlib.path('path/to/data.csv'))
plus.csv_to_df(csv_string)
Name | FirstLetter | |
---|---|---|
0 | Daniel | D |
1 | Audrey | A |