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-24 12:34
pypackagenbproject==0.7.0 pandas==1.5.1

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 user_handle=None user_id=None user_name=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-24T12:34:50.078843+00:00'
meta.live.time_passed
0.217919
meta.live.pypackage
{'pandas': '1.5.1', 'nbproject': '0.7.0'}
meta.live
Fields: consecutive_cells pypackage time_passed time_run title user_handle user_id user_name