Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify cell's metadata

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?

like image 868
Manuel Avatar asked May 28 '14 16:05

Manuel


People also ask

How do I change cell metadata in Jupyter?

For notebook level: in the Jupyter Notebook Toolbar go to Edit -> Edit Notebook Metadata. For cell level: in the Jupyter Notebook Toolbar go to View -> Cell Toolbar -> Edit Metadata and a button will appear above each cell.

How do I edit markdown cells?

Just double click on the markdown cell. Edit what you want to and Run. It will reflect the changes. Save your notebook.

How do you edit a Jupyter cell?

When a cell is in edit mode, you can type into the cell, like a normal text editor. Enter edit mode by pressing Enter or using the mouse to click on a cell's editor area.

What is Nbformat?

nbformat contains the reference implementation of the Jupyter Notebook format, and Python APIs for working with notebooks. There is also a JSON schema for notebook format versions >= 3.


2 Answers

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.

like image 78
Gordon Bean Avatar answered Oct 12 '22 23:10

Gordon Bean


If I understand what you are looking for: from the Cell Toolbar (top right of the ipython notebook toolbar), select Edit Metadata from the drop-down list.

like image 22
Aziz Alto Avatar answered Oct 13 '22 01:10

Aziz Alto