import nbclient
import nbformat as nbf
import pandas as pd
import scrapbook as sb
This notebook uses nbclient
to read and execute an empty notebook.
The empty notebook generates some fake data, makes a plot, and stores
both the data and the plot inside the notebook using the
scrapbook package. We will
then be able to access the generated contents of the notebook here.
You can see the empty notebook by clicking this button:
# We use nbformat to represent our empty notebook in-memory
nb = nbf.read('./empty_notebook.ipynb', nbf.NO_CONVERT)
# Execute our in-memory notebook, which will now have outputs
nb = nbclient.execute(nb)
# First we'll convert our nbformat NotebokNote into a *scrapbook* NotebookNode
nb = sb.read_notebook(nb)
# We can access the dataframe that was created and glued into the empty notebook
pd.DataFrame.from_dict(nb.scraps.get('dataframe').data).head()
# We can also access the generated plot by "re-gluing" the notebook here
nb.reglue('plot')