📝 Jupyter Cheatsheet


These are the list of commands and syntax I find using most often on Jupyter. These have been tested on Ubuntu and Mac.

If you are looking for a more complete cheatsheet with some nice visuals, you can try these:

Installation

Preferably install on the base environment.

pip install jupyterlab

or

conda install jupyterlab

Kernel not showing up in Jupyter

There are two parts to it:

  1. install the nb_conda_kernels package on your base environment

Install nb_conda_kernels package

conda install -n base nb_conda_kernels
  1. install ipykernel to the new environment
# Here, we are creating a new conda environment called py36
Replace <name> with your environment name
conda create -n <name> python=3.6
conda install -n <name> ipykernel
python -m ipykernel install --user --name <name> --display-name "<longer name>"

Adding modules from some folder

if you have your python scripts under a separate directory, you can add that directory to the sys path.

import sys
sys.path.insert(0, "../src/")
%load_ext autoreload
%autoreload 2

TQDM Progress

If you’re not using tqdm to show the progress then you’re missing out😉. Its one of those things, when you start using, you would try to put it everywhere. The tqdm progress bar requires a different renderer/tqdm instance from the default one, which is suited for running scripts in the shell.

from tqdm.notebook import tqdm

Pandas

Display all columns

pd.set_option('display.max_columns', 1000)

Similarly, you can opt to display all rows but in a most cases it is not a good idea.

pd.set_option('display.max_rows', 1000)

The above settings will impact the entire notebook. If you want to apply for a single or a few selected dataframes, you could use option_contet

with option_context('display.max_columns', 1000):
    print(df.head())
    # print(df)

Disable Altair large rows warning on Jupyter

Altair does not allow plotting large datasets by default. But, you can change that using the following snippet.

import altair as alt
alt.data_transformers.enable('default', max_rows=None)

Reference

Share Notebooks without the code

Certain scenarios require us to share the notebook as HTML or PDF report. The target audience is interested in tables or the figures with some commentary. They do not want to see the code.

Solution: jupyter nbconvert to rescue

# Install jupyter_contrib_nbextensions
conda install -c conda-forge jupyter_contrib_nbextensions
# Generate
jupyter nbconvert awesome_notebook.ipynb --no-input --to html

The following output formats are available: ‘asciidoc’, ‘custom’, ‘html’, ‘html_ch’, ‘html_embed’, ‘html_toc’, ‘html_with_lenvs’, ‘html_with_toclenvs’, ‘latex’, ‘latex_with_lenvs’, ‘markdown’, ‘notebook’, ‘pdf’, ‘python’, ‘rst’, ‘script’, ‘selectLanguage’, ‘slides’, ‘slides_with_lenvs’, ‘webpdf’

Adding R Kernel

I mainly use R Studio for anything R. But on my local server (Ubuntu), I prefer to work via a single interface. And, what better than Jupyter Lab.

Use the R shell (do not use R Studio)

install.packages('IRkernel')
IRkernel::installspec()

Reference

Adding Julia Kernel

Assuming you have already installed Julia. All you need to do is to add IJulia package.

# Julia REPL
Pkg.add("IJulia")
# Start the notebook
notebook()

Reproducibility

Always remember to set the seed.

# Numpy
import numpy as np
np.random.seed(42)

# PyTorch
import torch
torch.manual_seed(42)

# Tensorflow
import tensorflow as tf
tf.random.set_seed(42)