Description
Asking in nbclient repo because nbconvert.preprocessors.ExecutePreprocessor
gets it's execute_cell
method from nbclient.client.NotebookClient
, so I hope this is the right place.
I've been trying to run certain experiments in a Jupyter Notebook in order to take advantage of the nice layout of code and plots. However, in order to do so repeatably and store other metadata, I set up and run the notebook in memory and convert to HTML.
When I want to use the results of the experiment in some of the calling code, I currently print it in a nbformat.notebooknode.NotebookNode
cell and then read a notebook cell's ["outputs"] value. But for objects whose repr
isn't enough to reconstruct the object, is there a way to inspect the global scope of the NotebookNode
?
MWE:
import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
nb = nbformat.v4.new_notebook()
nb["cells"] = [nbformat.v4.new_code_cell(
source="foo=1\n"
"print(foo)"
)]
ep = ExecutePreprocessor(timeout=-1)
ep.preprocess(nb)
Currently, I run
result_string = nb["cells"][0]["outputs"][0]["text"][:-1]
Desired
result = ep.<some function>("foo")