DJ-NoteBook
Published: 2023-09-30 12:02:16


## Power of Django and Jupyter Integration:


  
## <u>Introduction:</u>

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.

## <u>Setting Up Your Environment:</u>

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
```
**Jupyter Notebook Installation**: Install Jupyter Notebook to create an interactive environment for your data exploration.


```
pip install jupyter
```
**Create a Django Project**: Begin by creating a new Django project if you haven't already.

```
django-admin startproject myproject
```
**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
```

```
source myenv/bin/activate
```

```
pip install django django-extensions
```
## Launch Jupyter Notebook: 

Open your Jupyter Notebook session within your Django project directory.


```
jupyter notebook
```

**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()
```
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()
```

**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')
```
**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
```

**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()
```
**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)
```
## <u>Usage:</u>

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:

  
```   
plus.User.objects.all()
Output : 
   <QuerySet [<User: Audrey>, <User: Daniel>]>
   ```

    - 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
    
5. **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) 
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sgadw5i391dygjlmhuan.jpeg)
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.


## <u>Conclusion:</u>

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.