Passing outputs between notebooks#
New in version 0.0.22.
Let’s create two simple notebooks:
The first notebook has:
x = 1
While the second notebook contains:
x = 0
y = x + 1
For this example, we’ll genearate the notebooks programmatically, but you can use existing .ipynb
files.
from pathlib import Path
import nbformat
first = nbformat.v4.new_notebook()
first.cells = [nbformat.v4.new_code_cell("x = 1")]
Path("first.ipynb").write_text(nbformat.v4.writes(first), encoding="utf-8")
second = nbformat.v4.new_notebook()
second.cells = [
nbformat.v4.new_code_cell("x = 0", metadata=dict(tags=["defaults"])),
nbformat.v4.new_code_cell("y = x + 1"),
]
Path("second.ipynb").write_text(nbformat.v4.writes(second), encoding="utf-8")
418
Now, we’ll execute the first notebook and extract the outputs:
from ploomber_engine.ipython import PloomberClient
client_first = PloomberClient.from_path("first.ipynb")
ns_first = client_first.get_namespace()
ns_first
0%| | 0/1 [00:00<?, ?it/s]
Executing cell: 1: 0%| | 0/1 [00:00<?, ?it/s]
Executing cell: 1: 100%|█████████████████████████| 1/1 [00:00<00:00, 432.89it/s]
{'x': 1}
Execute the second notebook:
client_second = PloomberClient.from_path("second.ipynb")
ns_second = client_second.get_namespace()
ns_second
0%| | 0/2 [00:00<?, ?it/s]
Executing cell: 1: 0%| | 0/2 [00:00<?, ?it/s]
Executing cell: 2: 0%| | 0/2 [00:00<?, ?it/s]
Executing cell: 2: 100%|█████████████████████████| 2/2 [00:00<00:00, 601.55it/s]
{'x': 0, 'y': 1}
We see that the second notebook is using x=0
, the defeault value. Now, let’s use the namespace
argument so it uses the outputs from the first notebook:
client_second = PloomberClient(second, remove_tagged_cells="defaults")
ns_second = client_second.get_namespace(namespace=ns_first)
ns_second
0%| | 0/1 [00:00<?, ?it/s]
Executing cell: 1: 0%| | 0/1 [00:00<?, ?it/s]
Executing cell: 1: 100%|█████████████████████████| 1/1 [00:00<00:00, 490.68it/s]
{'x': 1, 'y': 2}