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
version0
time_init2022-06-29 14:38
time_run2023-04-26 14:24
pypackagenbproject==0.8.6 pandas==2.0.1

If you’d like a simple dictionary and intialization state, call:

header(metadata_only=True)
({'id': 'k6Bj4FXM9oPm',
  'time_init': '2022-06-29T14:38:42.171771+00:00',
  'version': '0'},
 False)

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

assert not meta.live.consecutive_cells
🔶 Cells [(2, 4), (9, 11)] were not run consecutively.
meta.store.id
'k6Bj4FXM9oPm'
Hide code cell content
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='0' time_init='2022-06-29T14:38:42.171771+00:00' pypackage={'pytest': '7.3.1'} parent=None user_handle=None user_id=None user_name=None
meta.store.pypackage
{'pytest': '7.3.1'}
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
'2023-04-26T14:24:38.683843+00:00'
meta.live.time_passed
0.102516
meta.live.pypackage
{'nbproject': '0.8.6', 'pandas': '2.0.1'}
meta.live
Fields: consecutive_cells pypackage time_passed time_run title user_handle user_id user_name