DJ-NoteBook

tejoooo

Tejo Kaushal

Posted on September 30, 2023

DJ-NoteBook

Power of Django and Jupyter Integration:

Introduction:

In today's data-driven world, harnessing data's full potential is crucial. With modern Django applications and Python 3.9+, combining Jupyter notebooks and Django's ORM offers powerful data exploration and ad-hoc querying capabilities. This article explores this fusion, highlighting features like seamless django-extensions integration and inheritance diagram visualization.

Setting Up Your Environment:

Before we dive into the exciting features, let's ensure your development environment is ready for this dynamic Django-Jupyter integration.

Install Django :

If you haven't already, install Django using pip, ensuring you have a modern Python version (3.9, 3.10, or 3.11).

pip install Django
Enter fullscreen mode Exit fullscreen mode

Jupyter Notebook Installation: Install Jupyter Notebook to create an interactive environment for your data exploration.

pip install jupyter
Enter fullscreen mode Exit fullscreen mode

Create a Django Project: Begin by creating a new Django project if you haven't already.

django-admin startproject myproject
Enter fullscreen mode Exit fullscreen mode

Activate the Virtual Environment: It's best practice to work within a virtual environment. Activate it and install the necessary packages.

python -m venv myenv
Enter fullscreen mode Exit fullscreen mode
source myenv/bin/activate
Enter fullscreen mode Exit fullscreen mode
pip install django django-extensions
Enter fullscreen mode Exit fullscreen mode

Launch Jupyter Notebook:

Open your Jupyter Notebook session within your Django project directory.

jupyter notebook
Enter fullscreen mode Exit fullscreen mode

Configure Django Environment: In your Jupyter notebook, import Django settings and configure the project.

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django
django.setup()
Enter fullscreen mode Exit fullscreen mode

Now, your environment is primed for harnessing the potential of Django and Jupyter together.

Exploring the Capabilities Data Retrieval: Begin your exploration by loading your Django models. For instance, if you have a User model, query all users with ease:

User.objects.all()
Enter fullscreen mode Exit fullscreen mode

Ad-Hoc Queries: Execute custom ad-hoc queries against your database, even for complex queries that may not be easily achieved through Django's admin interface:

User.objects.filter(username__startswith='john')
Enter fullscreen mode Exit fullscreen mode

Data Analysis: Leverage Python's powerful data analysis libraries, such as Pandas and Matplotlib, to perform in-depth data analysis and generate insightful visualizations.

import pandas as pd
import matplotlib.pyplot as plt
Enter fullscreen mode Exit fullscreen mode

Integration with django-extensions: Benefit from seamless integration with django-extensions. The plus variable instantiated in your Django environment provides access to a wealth of tools and features.

plus.User.objects.all()
Enter fullscreen mode Exit fullscreen mode

Inheritance Diagrams: Visualize inheritance diagrams for any object, including your ORM models, using the plus.diagram() utility. This is especially helpful in complex project architectures.

plus.diagram(plus.User)
Enter fullscreen mode Exit fullscreen mode

Usage:

To harness the power of Django and Jupyter integration with dj-notebook, follow these steps:

  1. Create an IPython Notebook: Begin by creating an IPython notebook in the same directory as your Django project's manage.py file. In VSCode, simply add a new .ipynb file. If you're using Jupyter Lab, you can create a new notebook using the File -> New -> Notebook menu option.

  2. Activate the Django Environment: In the first cell of your notebook, import the activate function from dj_notebook and set up the Django environment.

    from dj_notebook import activate
    
    plus = activate("DJANGO_SETTINGS_MODULE_VALUE")
    

    Replace "DJANGO_SETTINGS_MODULE_VALUE" with the actual Django settings module value from your project's manage.py file.

  3. Utilize Django ORM: With the Django environment activated, you can now seamlessly work with your Django models and database using the ORM. Here's an example of how to perform basic operations with the ORM:

    from django.contrib.auth import get_user_model
    User = get_user_model()
    
    # Clean up the users (optional)
    User.objects.all().delete()
    
    # Create some users
    User.objects.create_user("Audrey")
    User.objects.create_user("Daniel")
    
    # Query the users
    User.objects.all()
    
  4. Unlock the Power of "plus": When you activated the Django environment, you instantiated a variable called 'plus,' which contains everything loaded from django-extensions shell_plus. Let's explore some of its capabilities:

- Query users using the `plus` variable:
Enter fullscreen mode Exit fullscreen mode
plus.User.objects.all()
Output : 
   <QuerySet [<User: Audrey>, <User: Daniel>]>
Enter fullscreen mode Exit fullscreen mode
- List the first 5 permissions:

for perm in plus.Permission.objects.all()[:5]:
    print(perm)

Output:
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
Enter fullscreen mode Exit fullscreen mode
  1. Introspect Your Project: For more advanced projects, introspection of classes can be invaluable. With dj-notebook, you have access to the plus.diagram() function. Let's see what happens when we use it to visualize the User model:
plus.diagram(plus.User) 
Enter fullscreen mode Exit fullscreen mode

Image description
Now you have a powerful environment for data exploration, ad-hoc querying, and introspection of your Django project's classes, all within the convenience of a Jupyter notebook.

Conclusion:

Integrating Jupyter notebooks with Django ORM provides an interactive environment for data exploration and analysis within your Django app. This empowers data-driven decision-making through ad-hoc queries, custom reports, and visualization. Embrace this integration for efficient data tasks and valuable insights.

πŸ’– πŸ’ͺ πŸ™… 🚩
tejoooo
Tejo Kaushal

Posted on September 30, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related