Using an IPython notebook to develop with the Django shell capabilities
Oct. 26, 2016
Developing in the django shell is an excellent way to quickly verify a query, or otherwise test code you are thinking about adding to your project; however, it can be a hassle programming into a shell. Luckily, there are alternatives! Here are a couple big reasons this blog can help if you're used to working in the default manage.py shell:
-
If you simply want to use the Django shell to test adding changes to a function, there is no way to 'reload' the changes without restarting the shell.
-
As is the nature of programming in a shell, common tasks amount to a wide range of trials and tribulations such as writing a function, checking back at what has been done thus far, reinstating variables, etc.
Running your shell through IPython with django-extensions
Here is first way I figured out a way to run the ./manage.py shell
in a IPython notebook environment. This is obviously incredibly nice to general testing, BUT, what is even better, is that you can reload changes without closing the shell and rewriting all of your code to get to your point by restarting the ipython kernel (Kernel —> restart).
First, install django-extensions, IPython, and jupyter to your local python environment.
# Getting dependencies...
pip install ipython
pip install jupyter
pip install django-extensions
You will also need to add the django_extensions application to the INSTALLED_APPS setting of your Django project settings.py file.:
INSTALLED_APPS = ( ... 'django_extensions', )
That's it, you have everything you need! Now for the magic.
# Starting the shell in an Ipython notebook environment
./manage.py shell_plus --notebook
The last thing is to make sure that you chose Django Shell-Plus
and not the standard Python 3 or 2 when creating a notebook after IPython opens up. A huge plus of this is that all models will already be imported into your environment, so you can immediately start accessing your models.
Django extensions provides a hosts of utilities that will become available to you. shell_plus
, which is used instead of shell
is just one such utility. You can read about the rest here: https://docs.djangoproject.com/en/1.9/topics/settings/#designating-the-settings
Manually creating the django environment in your IPython notebook
There are two ways to do this. Each will accomplish the same thing and has less overhead naturally, but requires a getting you hands a little bit more dirty.
Method 1
- Navigate to your projects root directory
-
In the shell, set the
DJANGO_SETTINGS_MODULE
environment variable tomysite.settings
$ export DJANGO_SETTINGS_MODULE=mysite.settings
-
Open up the Python shell
$ python
then use django's in house setup tool
import django django.setup()
And thats it, you will be able to interact with Django's API! This is an incredibly fast and efficient method of doing so. You can read more about this from the docs here: https://docs.djangoproject.com/en/1.9/intro/tutorial02/#playing-with-the-api
Method 2
Open up your environment you'd like to develop in.
from [project] import settings from django.core.management import setup_environ setup_environ(settings)
and then load your models in the python script. And that's it!
Of course, as is done in a IPython
/Jupyter
notebook, you can save the files and come back to them another time, which can be very convenient. Note that for pathing reasons it is best to save the .ipynb
file in the root project directory.
Happy coding!