The metadata API#

We already learned how to display metadata to provide humans with context (e.g., Basic metadata).

If we want to build functionality on top of notebook metadata, we can access metadata through the API.

from nbproject import header, meta
import pandas as pd

header()
idk6Bj4FXM9oPm
versiondraft
time_init2022-06-29 14:38
time_run2022-10-07 21:45
pypackagenbproject==0.5.3 pandas==1.5.0

For instance, we can retrieve the stored notebook ID as follows.

assert not meta.live.consecutive_cells
🔶 Cells [(1, 3), (8, 10)] were not run consecutively.
meta.store.id
'k6Bj4FXM9oPm'
assert meta.store.id == "k6Bj4FXM9oPm"
assert hasattr(meta.store, "time_init")
meta.store.pypackage

Manually add pypackages to meta.store.pypackage.

meta.store.add_pypackages(["pytest"])
Wrapper object for the stored metadata:
  id='k6Bj4FXM9oPm' version='draft' time_init='2022-06-29T14:38:42.171771+00:00' pypackage={'pytest': '7.1.3'} parent=None
meta.store.pypackage
{'pytest': '7.1.3'}
meta.live.title
'The metadata API'
# assert meta.live.consecutive_cells  # it'd be nice to test this here, but that would require flushing the ci buffer before this cell
meta.live.time_run
'2022-10-07T21:45:01.786948+00:00'
meta.live.time_passed
0.28186
meta.live.pypackage
{'pandas': '1.5.0', 'nbproject': '0.5.3'}
meta.live
Fields: consecutive_cells pypackage time_passed time_run title