Open Analect opened 7 years ago
I see the attachments in a pre-save hook with the following code:
def pre_save(model, path, contents_manager=None):
print('pre_save called')
if model['type'] != 'notebook':
return
print("pre_save with notebook")
for i, cell in enumerate(model['content']['cells']):
for attachment in cell.get('attachments', {}):
print('Cell {} has attachment {}'.format(i, attachment))
Note that they're a separate key on the cell, not part of metadata.
I've been experimenting around enriching a notebook with cell meta-data, leveraging
nbformat
and saving changes to a 'shadow' notebook, since I can't overwrite an already open file in jupyterlab. This has been working fine.However, when it comes to handling cell attachments, this approach no longer works. While the notebook in jupyterlab still doesn't allow adding cell attachments, if I select
Launch Classic Notebook
from the help menu in JupyterLab, I am able to work with that instance of a notebook to copy and paste images into a cell which are automatically added as cell attachments.From the readthedocs, it would appear that "attachments" are not exposed as part of the 'model', alongside
cell_type
,metadata
andsource
.That might explain why enriching the model
cells.metadata
(as per script above) and then using that updated in-memory 'model' to save a copy of the notebook (on right below) is missing theattachments
element from the json (red box below).Could anyone suggest how I might access the
attachments
part of a notebook from a pre-save hook python script, so that I can include it as part of a saved 'shadow' notebook that includes my enriched meta-data?