A2oz

How Do I Run a Jupyter Notebook From a Notebook?

Published in Jupyter Notebook 2 mins read

You can run another Jupyter Notebook from within a notebook using the %%bash magic command. This command allows you to execute shell commands directly within your notebook.

Here's how you can do it:

  1. Start a new cell in your notebook.

  2. Type %%bash at the beginning of the cell. This tells Jupyter to interpret the following lines as shell commands.

  3. Enter the command to run your other notebook. This will typically involve using the jupyter nbconvert command with the --execute flag. For example, to run a notebook named my_other_notebook.ipynb, you would use the following command:

    jupyter nbconvert --execute my_other_notebook.ipynb
  4. Run the cell. Jupyter will execute the shell command, which will run the specified notebook.

Example:

%%bash
jupyter nbconvert --execute my_other_notebook.ipynb

Important Notes:

  • The notebook you are running must be in the same directory as the notebook you are currently working in or in a directory on your system's PATH.
  • You can use the --to flag with the jupyter nbconvert command to specify the output format of the executed notebook. For example, you can use --to html to generate an HTML file.

This method allows you to execute other notebooks within your workflow, making it easier to manage and organize your code.

Related Articles