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()
id | k6Bj4FXM9oPm |
version | 0 |
time_init | 2022-06-29 14:38 |
time_run | 2023-03-12 16:58 |
pypackage | nbproject==0.8.2 pandas==1.5.3 |
If you’d like a simple dictionary, call:
header(display=False)
{'id': 'k6Bj4FXM9oPm',
'time_init': '2022-06-29T14:38:42.171771+00:00',
'version': '0'}
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'
Show 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.2.2'} parent=None user_handle=None user_id=None user_name=None
meta.store.pypackage
{'pytest': '7.2.2'}
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-03-12T16:58:38.517475+00:00'
meta.live.time_passed
0.096991
meta.live.pypackage
{'pandas': '1.5.3', 'nbproject': '0.8.2'}
meta.live
Fields: consecutive_cells pypackage time_passed time_run title user_handle user_id user_name