February 26, 2025
Notebooks
5 min
Diego García

Running Django Inside Jupyter Notebooks: A Step-by-Step Guide

This guide provides a clear, step-by-step approach to manually integrating Django with Jupyter Notebooks on the MINEO platform, enabling developers to tap into Django’s powerful ORM and models for tasks like interactive data analysis and prototyping.

Django and Jupyter Notebooks are powerful tools on their own—Django for building robust web applications and Jupyter for interactive coding and data analysis. But when you combine them, you unlock a new level of productivity. Imagine querying your Django models directly from a notebook, prototyping new features, or debugging issues interactively. This guide will show you how to run Django inside your existing Python notebooks on MINEO, step by step.

💡 Why Integrate Django with Jupyter Notebooks?

Integrating Django with Jupyter Notebooks allows you to:

  • Access Django’s ORM for interactive data analysis and manipulation. This is very useful to access to DB and keep the state of them thanks to the migrations.
  • Prototype and test new features or fixes in a flexible, cell-based environment.
  • Debug interactively, inspecting models and database queries on the fly.

This setup is especially useful for developers who want to leverage Django’s backend capabilities while enjoying the interactivity of Jupyter.

⚙️ Prerequisites

Before you start, make sure you have:

  • A basic understanding of Django and Jupyter Notebooks.
  • A Django project set up and accessible on MINEO.
  • Django installed in your notebook’s Python environment. If it’s not installed, you can install it with:
1 !pip install django

📋 Step-by-Step Guide to Running Django in a Notebook

Follow these steps to configure Django in your existing Python notebook:

Step 1: Set Up the Django Environment

In your notebook, you need to manually configure the Django environment. This involves setting the DJANGO_SETTINGS_MODULE environment variable and initializing Django.

1import os
2import django
3
4# Set the DJANGO_SETTINGS_MODULE to your project's settings module
5os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
6
7# Initialize Django
8django.setup()

Replace myproject.settings with the actual name of your Django project’s settings module.

Step 2: Add the Django Project Path

If your notebook isn’t in the same directory as your Django project, you must add the project’s directory to the Python path. This ensures Django can find your apps and models.

import sys

# Add the project directory to the Python path
project_path = '/path/to/your/django/project'
if project_path not in sys.path:
    sys.path.append(project_path)

Do not forget to replace /path/to/your/django/project with the actual path to your Django project on MINEO. If you’re unsure of the path, use !ls o !pwd in a notebook cell to explore the file system.

Step 3: Import and Use Django Models

Once the environment is set up, you can import your Django models and start querying the database.

1from myapp.models import MyModel
2
3# Example query
4objects = MyModel.objects.all()
5for obj in objects:
6    print(obj)

Step 4: Handle Asynchronous Code (If Needed)

If your Django project uses asynchronous features, you might encounter a SynchronousOnlyOperation error in the notebook. To fix this, set the following environment variable:

os.environ['DJANGO_ALLOW_ASYNC_UNSAFE'] = 'true'

Important: This is for development or testing only. Do not use this in production.

Complete Example Code

Here’s a full snippet combining all the steps:

1import os
2import sys
3import django
4
5# Add the project directory to the Python path
6project_path = '/path/to/your/django/project'
7if project_path not in sys.path:
8    sys.path.append(project_path)
9
10# Set the DJANGO_SETTINGS_MODULE
11os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
12
13# Allow async code in synchronous context (optional, for development only)
14os.environ['DJANGO_ALLOW_ASYNC_UNSAFE'] = 'true'
15
16# Initialize Django
17django.setup()
18
19# Import and query your models
20from myapp.models import MyModel
21
22# Example query
23objects = MyModel.objects.all()
24for obj in objects:
25    print(obj)

🖥️ Running Django Management Commands

When working with Django in a Jupyter Notebook, you’re not limited to just querying models or testing code interactively. You can also execute Django management commands—like running migrations, creating superusers, or collecting static files—directly within your notebook. This capability streamlines your development workflow by allowing you to manage your Django project without switching to a terminal.

To run these commands programmatically, you’ll use Django’s call_command utility from the django.core.management module. Here’s how to do it, step by step.

Step 1: Import the Required Module

After setting up Django in your notebook, import the call_command function:

from django.core.management import call_command

This utility lets you invoke any built-in or custom Django management command as if you were running it from the command line.

Step 2: Execute Management Commands

With call_command imported, you can run various Django management commands. Below are some practical examples:

Example 1: Running Migrations

To apply all pending migrations to your database, use:

call_command('migrate')

This is equivalent to running python manage.py migrate in a terminal.

Example 2: Creating a Superuser

To create a superuser for your Django admin interface:

call_command('createsuperuser', username='admin', email='admin@example.com', interactive=False)

Note: The interactive=False flag allows you to set the username and email programmatically, but it requires you to handle the password separately (e.g., via environment variables or additional code). If you omit this flag, the notebook will prompt you interactively for the password, which is more secure but requires manual input.

Example 3: Collecting Static Files

To gather all static files into the STATIC_ROOT directory:

call_command('collectstatic', interactive=False)

Note: The interactive=False flag skips the confirmation prompt that would normally ask if you’re sure you want to proceed. Without it, you’d need to interact with the notebook to confirm.

Example 4: Running Custom Management Commands

If your Django project includes custom management commands (e.g., defined in myapp/management/commands/my_custom_command.py), you can run them like this:

call_command('my_custom_command')

Replace my_custom_command with the name of your custom command.

Step 3: Managing Output and Errors

When you execute a management command in a notebook, its output (e.g., migration logs or success messages) appears directly in the cell’s output area. Errors, such as database connection issues or migration conflicts, will also be displayed, making it easy to debug interactively.

If you need to capture the output for further processing or logging, redirect it to a string buffer using Python’s io.StringIO. Here’s an example:

1from io import StringIO
2
3# Create a buffer to capture output
4output = StringIO()
5
6# Run the command and redirect output
7call_command('migrate', stdout=output)
8
9# Display or process the captured output
10print(output.getvalue())

This approach is handy for automating workflows or analyzing command results within your notebook.

✅ Tips and Best Practices

To make the most of this setup, keep these tips in mind:

  • Environment Management: Ensure your notebook is using the correct Python environment where Django is installed. Check MINEO’s kernel or environment options if multiple are available.
  • Path Handling: If possible, use relative paths or environment variables to make your code more flexible and easier to share.
  • Permissions: Be aware of any platform-specific restrictions on file access or database connections. Consult MINEO’s documentation if you encounter permission issues.
  • Debugging: If you face import errors, use `print(sys.path)` to verify that your project path is correctly added.
  • Performance: Be mindful of resource usage, especially when running large queries or working with big datasets, to avoid slowing down your notebook.

🎯 Conclusion

Running Django inside your Jupyter Notebooks on MINEO is a powerful way to interact with your web application’s data and logic. By following these steps, you can set up the Django environment manually, giving you full control and flexibility. Remember to handle paths carefully, manage your environment, and use the tips provided to avoid common pitfalls.

Now that you’re equipped with this knowledge, dive into your notebook and start exploring your Django project in a whole new way.

Happy coding!

Further reading

By clicking “Accept All Cookies”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. View our Privacy Policy and our Cookies Policy for more information.