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

Zoltán Vörös zvoros at gmail.com
Thu Jan 26 16:39:31 EST 2017



On 01/26/2017 10:06 PM, Klymak Jody wrote:
>
>> 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
>
> For my taste, I’d just save that result in a file (`pickle` or 
> `shelf`, or netcdf if I wanted to be formal).  Its a lot more 
> transparent what is going on.


But why is it more transparent? By the same token, you could say that 
the %%writefile magic command is more obscure than saving the file 
explicitly with

with open('file.txt') as fout: fout.write('text')

Magic commands are abbreviations for common tasks, therefore, obscure:)

>
> Imagine this case:  I `%store_in_notebook` the results of a long 
> calculation, and then remove that code from the notebook for some 
> reason.  I might very well wonder a year from now why my notebook is 
> 50 Gb, and have no documentation of how it got that way.

Or imagine this case: I save the results of a long calculation in a 
file, and then remove that code from the notebook for some reason. I 
might very well wonder a year from now why there is a 50 Gb in my 
folder, and have no documentation of how it got that way;)

As William Stein pointed out, when pickling the variables, one would 
have to impose some sensible upper bound on the size.


>
> However, if you do have a whole slew of variables you suddenly want to 
> save, did you try `dill`?
>
> import dill
> import numpy as np
>
> filename= 'globalsave.pkl'
>
> if 1:
>     x = np.arange(20)
>     dill.dump_session(filename)
> else:
>     dill.load_session(filename)
>

The question is not how one can save variables in a file, the question 
is, how one can avoid having to save variables in a file.

Cheers,
Zoltán



More information about the IPython-dev mailing list