Modify cell's metadata

jupyter-notebook, metadata, python

Solution

I don't know how to do this from within the notebook, but I found a way to do it with a custom preprocessor and nbconvert.

You can create a class that extends nbconvert.preprocessors.ExecutePreprocessor. In the preprocess (or preprocess_cell) method, add logic for storing the relevant output in the cell metadata.

Something like:

class MyExecutePreprocessor(ExecutePreprocessor):
    def preprocess_cell(self, cell, resources, index):
        # Execute the cell normally
        cell, resources = super().preprocess_cell(cell, resources, index)
        # Add your magic here
        cell.metadata['widgets'] = {'stuff':'that is cool'}
        return cell, resources

You can then execute this preprocessor programatically, or as an argument to nbconvert.

Problem

I want to store the values of my `IPython.html.widgets` in my ipython notebook somehow. Is there a way to modify the metadata of the current cell from the code within the cell itself?

Original source