django-notebook-config
Easily run Django queries and tasks within Jupyter Notebooks in IDEs.
Overview
vscode-django-notebook simplifies the process of initializing Django projects directly in Jupyter Notebook cells. This is particularly useful for quick debugging, querying, and testing in a flexible notebook environment.
Features
- Convenient Django Initialization: Avoid repetitive setup steps in Jupyter Notebooks.
- Interactive Django ORM Access: Use Django models and utilities interactively.
- VS Code Compatible: Tailored for the Jupyter extension in IDEs.
Installation
Install the package via pip:
pip install django-notebook-config
Setup Guide
Step 1: Create a Folder for Notebooks
On the same level as manage.py, create a folder to organize your Jupyter Notebooks. For example:
your_project/
├── manage.py
├── config/
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── notebook/
│ ├── __init__.py
│ └── user.ipynb
- Folder Name : You can name this folder anything you like (e.g.,
notebooks,playground,testing). In this example, we usenotebook. - Notebook File Name : The notebook file (e.g.,
user.ipynb) can also have any name, depending on its purpose.
Step 2: Select the Project's Environment in VS Code
- Open the
user.ipynbnotebook in VS Code. - In the top-right corner of the Jupyter Notebook interface, click on the kernel selection dropdown .
- Choose the Python environment where your Django project dependencies are installed.
Step 3: Install and Initialize django-notebook-config
- Add the following code at the top of your notebook to initialize Django:
from django-notebook-config import init_django
# Initialize Django by specifying the project name
init_django(project_name="config")
Replace "config" with the name of your Django project module (where settings.py is located).
Step 4: Write Complex Queries or Debug Utilities
Once initialized, you can interact with Django models, run complex queries, or debug utilities directly in the notebook. Here's an example:
Example: Testing a Query
# Import Django models
from myapp.models import User, Order
# Write and test complex queries
users_with_recent_orders = User.objects.filter(
id__in=Order.objects.filter(date__gte="2024-01-01").values_list("user_id", flat=True)
)
# Inspect the results
print(users_with_recent_orders)
Example: Writing and Debugging Utilities
# Utility function to calculate total revenue for a user
def calculate_user_revenue(user_id):
from myapp.models import Order
return Order.objects.filter(user_id=user_id).aggregate(total_revenue=Sum("amount"))["total_revenue"]
# Test the utility
test_user_id = 1
print(f"Total revenue for user {test_user_id}: {calculate_user_revenue(test_user_id)}")
Benefits of This Workflow
- Interactive Testing : Test Django ORM queries and helper functions interactively.
- Debugging Made Easy : Debug utilities or inspect data before adding them to the project.
- Reusable Code : Refine and reuse tested code in the actual project.
Troubleshooting
- Jupyter Kernel Not Working : Ensure you’ve selected the correct Python environment in VS Code.
- Initialization Error : Verify that the
project_nameininit_django()matches the folder containing yoursettings.py.
Now you're ready to streamline your Django development workflow using Jupyter Notebooks! 🚀
Metadata
Release files for django-notebook-config 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_notebook_config-0.1.1.tar.gz | 4.4 kB | Without provenance | Details for django_notebook_config-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_notebook_config-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for django_notebook_config-0.1.1-py3-none-any.whl |
Total release size: 9.4 kB
Release files / django_notebook_config-0.1.1.tar.gz
| Download URL | django_notebook_config-0.1.1.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
857a09c0e7cdbdd1f4553431d0a03219ecfce70af88193a953aae7876bda3ea1
|
|
BLAKE2b-256 checksum How to use checksums |
c96e36ecb31c4afd89b9588546c3249626d7bab96b586ee85ad8fe1556259d8a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.3
|
Release files / django_notebook_config-0.1.1-py3-none-any.whl
| Download URL | django_notebook_config-0.1.1-py3-none-any.whl |
|---|---|
| Size | 5.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a9fbb406c5f19500a50432f22b06afcb1b697789a679bca6ee45d2da45c18374
|
|
BLAKE2b-256 checksum How to use checksums |
9a253b76f403a6b8ba651b2566b2456fb764162b74f299ca574ea6c53b528998
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.3
|
Security for django-notebook-config 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.