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

Zoltán Vörös zvoros at gmail.com
Thu Jan 26 15:35:49 EST 2017


Hi Jackson,



On 01/26/2017 08:57 PM, Loper, Jackson wrote:
> Zoltán --
>
> I am very curious to know why you want the data embedded in the ipynb 
> file, instead of storing a file in the same directory.  Is it so you 
> can share files with colleagues?  If so, why not just share the whole 
> directory?  Is it just too bulky?  Just curious as to what your 
> motivation is.


I guess, it always comes down to taste, but I will try to give some 
rational argument, all the same.

1. In many cases, I have multiple notebooks in a single directory, 
simply because most of the time, I really don't need a separate folder 
for just a single file.So, I have all notebooks that belong to a 
particular subject in a single folder, and I don't necessarily want to 
share all of them with others.

2. In such cases, it would become messy quite soon, if I started to save 
variables to separate data files. Suppose you want to save 10 variables, 
all differing in shape and type. You can either save them separately, 
which looks somewhat stupid, because you'll have then 10 very small 
files, plus you have to load them one by one in the new session, or you 
pack them by hand into a single file, and unpack them somehow (probably 
by pickling and unpickling), when you want to load them.



What I would like to point out is that this notion already exist in the 
notebook, because %store does exactly what I want. The only snag is that 
it ties the data to the user, and not the notebook that generated the 
data. (This is actually quite bad in my opinion, because notebooks 
running on different computers will produce different results, simply 
because the %store -r magic can assign different values to the same 
variable names.) I understand the arguments and development decisions 
brought up by Thomas yesterday, but I feel that those arguments are not 
valid, or, at least, make it very hard to "unjustify" a 
%store_in_notebook magic, or something similar.


>
> Anywho, if you really want to do it, and you're in the mood, I think 
> it would be fairly straightforward to make a combined 
> python/javascript plugin that allowed one to conveniently store code 
> cells of the form
>
>   # This is a datacell.  If you do not have the datacell javascript 
> extension,
>   # this code cell may look really really long.  Sorry about that.
>   x = 
> pickle.loads(b"\x80\x03}q\x00(X\x07\x00\x00\x00Purposeq\x01X$\x00\x00\x00Very 
> important data just for 
> Kluyverq\x02X\x07\x00\x00\x00Contentq\x03X\r\x00\x00\x00You're 
> great!q\x04u.")
>
> and make them appear in the notebook as a "data cell" that looks like
>
>   x = pickle.loads(<<<content abridged>>>)
>
> Such a data cell would be uneditable, but could be executed.
>
> I think the simplest way to do this would be to design an ipython 
> widget that, when it comes online, adds such a "data cell" directly 
> after the current one.  Creating a cell should then be as simple as
>
>   datacells.make_data_cell(varname='x',data="Hello world.")
>
> I could be missing something that makes this utterly impossible though.
>

I think this goes far beyond what I had in mind. I think this function 
or whatever would just be

In [221]: x = long_calculation()  # x is 42
                 %store_in_notebook x

and in the new session

In [1]: %store_in_notebook -restore_variables
In [2]: x
Out [2]: 42

I don't think it would have to be a cell that cannot be edited, or 
anything like that.

Perhaps, the purpose of my first e-mail was to inquire about how one can 
write into the notebook metadata from a code cell. I know that I can 
load up the metadata editor, but that's not any better then just putting 
the data in a markdown cell.

Cheers,
Zoltán




More information about the IPython-dev mailing list