How do I set up Jupyter/IPython Notebook for Django?

后端 未结 8 1292
鱼传尺愫
鱼传尺愫 2020-11-30 17:17

I have been using the method described in this post for setting up IPython Notebook to play nicely with Django. The gist of the method is to create an IPython extension whic

8条回答
  •  醉话见心
    2020-11-30 17:55

    While the accepted answer from RobM works, it was less clear than it could be and has a few unnecessary steps. Simply put, to run notebooks through Django from a notebook environment outside of the project directory:

    Install:

     pip install django-extensions
    

    Add 'django-extensions' to your INSTALLED_APPS list in settings.py

     INSTALLED_APPS += ['django_extensions']
    

    Run a notebook from within Django, then close it:

    python manage.py shell_plus --notebook
    

    This will create your kernel, which we will now edit to point to an absolute path of Python rather than a relative path.

    On OSX, the kernel file is at: ~/Library/Jupyter/kernels/django_extensions/kernel.json

    On Linux: ~/.jupyter/kernels/django_extensions/kernel.json

    We only need to make two changes:

    The first is to edit the first value in the "argv" list from "python" to the full address of the python version in your Django virtual environment. E.g.: "/Users/$USERNAME/Documents/PROJECT_FOLDER/venv/bin/python"

    Secondly, to the "env" dictionary, add "DJANGO_SETTINGS_MODULE": "mysite.settings", where mysite is the folder that contains your Django settings.

    Optionally, change the value of "display_name".

    Now when you run a notebook from any directory, choosing the "Django Shell-Plus" kernel will allow your notebooks to interact with Django. Any packages such as pandas will need to be installed in the Django venv.

提交回复
热议问题