

Django Setup for use in Jupyter Notebooks
source link: https://gist.github.com/codingforentrepreneurs/76e570d759f83d690bf36a8a8fa4cfbe
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Django for Jupyter
It's true packages exist to make it "easy" to use Django inside of a jupyter notebook. I seem to always run into issues successfully running these packages. I've found the below method useful although I cannot recall how I discovered how this works (aka attribution needed).
Requirements
- Virtual Environment (
virtualenv
,venv
,pipenv
, etc) - Django installed & project created (we'll use the project name
cfehome
) - Jupyter installed at least in the virtual environment
Setup:
It's simple. Just copy django_for_jupyter.py
next to your Jupyter notebook files (.ipynb
).
init_django
Via Project Name
First cell
from django_for_jupyter import init_django
init_django("cfehome")
Change
cfehome
to your Django project name.
init_django
Using Environment Variables
In, .env
:
DJANGO_PROJECT="cfehome"
Or on the CLI:
DJANGO_PROJECT="cfehome" jupyter notebook
First cell
from django_for_jupyter import init_django
init_django()
Change
cfehome
to your Django project name.
Using Django Models
Now that you've run init_django
with no errors. You can import Django models:
Second Cell
from myapp.models import MyModel
MyModel.objects.all()
Simple enough right?
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK