How to Use Jupyter Notebooks

Sources:

  • Installing Jupyter

Installation

Install the classic Jupyter Notebook with:

1
pip install notebook

To run the notebook:

1
jupyter notebook

Execution in CLI

To execute a notebook in command line,

  1. Install ipykernel in Your Conda Environment

    1
    2
    3
    conda activate <your_env_name>
    conda install ipykernel
    conda install nbconvert
  2. Add Your Conda Environment as a Jupyter Kernel

    1
    python -m ipykernel install --user --name <your_env_name> --display-name "Display Name of Your Env"
    • --name your_env_name: This should be the name of your Conda environment.
    • --display-name "Display Name of Your Env": This is the name that will be displayed in Jupyter interfaces (e.g., Jupyter Notebook, JupyterLab) when selecting the kernel.
  3. Edit the kernelspec section of your notebook's JSON to select the onda env when running the notebook:

    1
    2
    3
    4
    5
    "kernelspec": {
    "display_name": "Display Name of Your Env",
    "language": "python",
    "name": "your_env_name"
    }
  4. Execute the Notebook Using Your Environment's Kernel:

    1
    jupyter nbconvert --to notebook --debug --execute --inplace your_notebook.ipynb
    • You can add --debug flag to enable the output to the CLI.
    • --inplace: Modifies the original notebook file directly with the output of the conversion (which includes the execution of its cells in this context). This means the changes made (including code execution outputs) will be saved in the original your_notebook.ipynb file.

Commands

Some special commands in jupyter:

  • When you use ! before a command in Jupyter, it tells the notebook to run the command as if it were in a system shell.
  • You can indeed use %pip install as an alternative, and it's actually recommended over !pip install for several reasons. The %pip install command is a Jupyter-specific magic command which is more integrated with the Jupyter environment.