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

后端 未结 8 1279
鱼传尺愫
鱼传尺愫 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:43

    Here's what just worked for me

    1. install Django Extensions (I used 1.9.6) as per other answers
    2. install jupyter
      pip install jupyter
    3. some stuff I did to setup jupyter inside my Docker container -- see below if this applies to you †
    4. from your base Django directory, create a directory for notebooks, e.g.
      mkdir notebooks
    5. Go to that directory
      cd notebooks
    6. start django extensions shell_plus from inside that directory:
      ../manage.py shell_plus --notebook
      The notebook server should now be running, and may launch a new browser. If it doesn't launch a browser window, follow the instructions to paste a link or a token.
    7. from the browser, open a new "Django Shell Plus" notebook, as per John Mee's answer's screenshot

    AND, importantly, what didn't work was changing directories from inside the notebook environment. If I tried to work with any notebook that was not in the directory that manage.py shell_plus --notebook was run in, then the kernal was not configured correctly. For me, having the notebook be configured for just a single directory at a time was good enough. If you need a more robust solution, you should be able set PYTHONPATH prior to starting jupyter. For example add export PYTHONPATH="$PYTHONPATH:/path/to/django/project" to a virtualenv activate script. But I haven't tried this.

    † Docker Setup (optional)

    1. add a port mapping for your container for port 8888

    For example, in your docker compose file;

    ports:
          -  "8890:8888"
    
    1. Configure your project settings file to use ip 0.0.0.0

    This is what I did:

    NOTEBOOK_ARGUMENTS = [
        '--ip', '0.0.0.0', 
        '--allow-root',
        '--no-browser', 
    ]
    

提交回复
热议问题