Skip to content

Instantly share code, notes, and snippets.

@txoof
Last active November 28, 2020 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save txoof/ed4319db317f813b9e500ff190ca4a87 to your computer and use it in GitHub Desktop.
Save txoof/ed4319db317f813b9e500ff190ca4a87 to your computer and use it in GitHub Desktop.
Installing Jupyter on Rasbian

Setting up Jupyter on Raspbian for Development

  • Install jupyter:
    • $ pip3 install jupyter
  • launch a Jupyter notebook server that is available over the local network:
    • $ jupyter-notebook --ip=$(hostname -I) --no-browser
$ jupyter-notebook --ip=$(hostname -I) --no-browser
[I 05:51:34.280 NotebookApp] Serving notebooks from local directory: /home/pi
[I 05:51:34.280 NotebookApp] Jupyter Notebook 6.1.5 is running at:
[I 05:51:34.281 NotebookApp] http://192.168.178.108:8888/?token=f217d68ea7f4666a71ec30745f399e2f6c725ef7c7af8895
[I 05:51:34.281 NotebookApp]  or http://127.0.0.1:8888/?token=f217d68ea7f4666a71ec30745f399e2f6c725ef7c7af8895
[I 05:51:34.281 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 05:51:34.302 NotebookApp]

    To access the notebook, open this file in a browser:
        file:///home/pi/.local/share/jupyter/runtime/nbserver-4494-open.html
    Or copy and paste one of these URLs:
        http://192.168.178.108:8888/?token=f217d68ea7f4666a71ec30745f399e2f6c725ef7c7af8895
     or http://127.0.0.1:8888/?token=f217d68ea7f4666a71ec30745f399e2f6c725ef7c7af8895
  • Point your web browser to the LAN IP address shown above

Adding pipenv virtual environments to Jupyter

Jupyter can have multiple kernels available at any time. Each project can have its own virtual environment. Make sure you have added a pipenv environment ($ pipenv --three|--two) before attempting this command.

Add a new kernel for the current pipenv using:

  • $ cd ~/src/path/to/project
  • $ pipenv run python -m ipykernel install --user --name=$(basename $(pipenv --venv))

Kernels can be added at any time; refresh the jupyter interface if a newly added kernel does not appear.

Scripts for automating this

  • Add a pipenv kernel to jupyter:
    • $ wget https://raw.githubusercontent.com/txoof/develtools/master/pipenv_jupyter.sh
  • Start a jupter notebook server:
    • $ wget https://raw.githubusercontent.com/txoof/develtools/master/jnote_server

Converting Jupyter Notebooks to python

Jupyter notebooks can be converted into python using: $ jupyter-nbconvert --to python notebook.ipynb

NOTE nbconvert will leave notebook magic lines (lines that start with % and !) as well as cell references. Use this template to override this behavior.

Add the template to ~/.jupyter/templates and run:

  • $ jupyter-nbconvert --to pytyhon --template python_clean notebook.ipynb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment