[IPython-dev] storing variables *in* the notebook

Zoltán Vörös zvoros at gmail.com
Fri Jan 27 12:39:23 EST 2017


On 01/27/2017 05:53 PM, Kiko wrote:
>
> An example about how to write to your metadata. This should provide 
> some hints to create a line magic to do so:
>
> from IPython.display import Javascript
>
> x = [1,2,3]
>
> def add_metadata(var_name, var):
>     js = """
> if (Jupyter.notebook.metadata['stored'] === 
> undefined){{Jupyter.notebook.metadata['stored'] = {{}}}}
> Jupyter.notebook.metadata['stored']['{var_name}']={var_content_serialised}
> """
>     return Javascript(js.format(var_name=var_name, 
> var_content_serialised=var.__repr__()))
>
> add_metadata('x', x)

Thanks for the pointer! I have already figured that I could re-use the 
code in the notebook extension 'toc2', and that of %store magic, but 
here you present a more or less complete solution. Thanks!

Zoltán



More information about the IPython-dev mailing list