Jupyter

while avoiding using your browser as an IDE


Starting a new notebook (on the command line)

1. Verify that the desired conda environment is available to Jupyter:

jupyter kernelspec list

2. If environment is not listed:

python -m ipykernel install --user --name env_name

3. Create python file, set kernel, and pair with notebook

See shell script to automate the steps below at: https://github.com/jsta/bin/blob/master/jupyinit

touch foo.py 
jupytext --set-kernel env_name foo.py
jupytext --to notebook --execute foo.py
jupytext --set-formats ipynb,py foo.ipynb

Starting a new notebook (manually)

jupyter notebook
# then click new, etc

Opening an existing notebook

jupyter notebook mynotebook.ipynb

Pair existing (downloaded) notebook with py file

jupytext --set-formats ipynb,py downloaded.ipynb
jupytext --set-kernel env_name downloaded.py

Editing notebooks

1.A If it doesn’t already exist, create notebook following the steps above

See shell script to automate this step at: https://github.com/jsta/bin/blob/master/jupyinit

1.B If it does already exist, pair with py file following the steps above

2. Open in browser

jupyter notebook foo.ipynb

3. Open .py file in VSCode and edit

# Spacings between code lines create separate notebook chunks
# Specify code chunks with by setting the first line as `# %% `
# See: https://jupytext.readthedocs.io/en/latest/formats.html#the-percent-format

4. Sync .py and .ipynb

See shell script to automate the steps below at: https://github.com/jsta/bin/blob/master/jupysync

# Update all paired representations of notebook.ipynb  
jupytext --sync notebook.ipynb                  

5. Execute all notebook cells

papermill notebook.ipynb notebook.ipynb

6. Refresh browser


Misc

within jupyter: type '%whos' to see what variables you’ve assigned, like ls() in R


https://stackoverflow.com/a/56753936/3362993

https://code.visualstudio.com/docs/python/jupyter-support#_debug-a-jupyter-notebook