9

I have a notebook that I need to call in a Python file. I know that calling a notebook in another notebook is done using %run ./NotebookName, and calling a Python module in a notebook can be done using import. So how can a notebook be called in a Python file?

4
  • 2
    If it can run top-down, you could just use jupytext to convert to a format that would run as a script. Commented Jun 2, 2021 at 20:13
  • I am trying to build an interactive interface and I need to plot figures which is done in jupyter notebook and building an interactive interface using notebooks is complicated for me. Do you have any idea how it can be done? I need to take input from the user and then work with it. Is it possible to be done using jupyter notebook? Thanks in advance Commented Jun 2, 2021 at 20:35
  • @Nour If you're trying to build an app, jupyter notebook is not the right environment for that. You should either convert to a script, if the interface will be command-line, or build a gui app through a framework (react). Either of those options will let you plot as needed (also lots of options for plotting: plotly, bokeh, matplotlib, etc.). Commented Jun 3, 2021 at 16:03
  • @AndrewHolmgren could you create an answer that shows the use of jupytext? It looks like that might be the most straightforward answer and potentially less error prone than trying to read the raw JSON structure. Commented Aug 10, 2021 at 14:32

3 Answers 3

8

You can use the nbconvert package to execute ipython/jupyter notebooks from within python. Instructions are available within the nbconvert documentation: Executing notebooks.

Here is a short example

import nbformat
from nbconvert.preprocessors import ExecutePreprocessor

filename = 'NotebookName.ipynb'
with open(filename) as ff:
    nb_in = nbformat.read(ff, nbformat.NO_CONVERT)
    
ep = ExecutePreprocessor(timeout=600, kernel_name='python3')

nb_out = ep.preprocess(nb_in)

The output is an ipython/jupyter notebook including the output of all cells.

Sign up to request clarification or add additional context in comments.

Comments

4

Ipython/Jupyter *.ipynb notebooks are actually just JSON files with a particular structure. To execute the cells of a notebook in a python script one can read the file using the python json library, extract the code from the notebook cells, and then execute the code using exec().

Here is an example that also includes removal of any ipython magic commands:

from json import load

filename = 'NotebookName.ipynb'
with open(filename) as fp:
    nb = load(fp)

for cell in nb['cells']:
    if cell['cell_type'] == 'code':
        source = ''.join(line for line in cell['source'] if not line.startswith('%'))
        exec(source, globals(), locals())

Comments

-2

You can just download de notebook as a file.pyenter image description here

3 Comments

This would let you execute the notebook as a script. But the question is asking how to execute the notebook within a script
You can import the resulting Python (.py) file into your script and execute it.
That doesn't work if, say, the notebook creates many plots and you want them stored in the notebook after execution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.