Share data between IPython Notebooks

会有一股神秘感。 提交于 2019-11-28 19:23:52

This works for me :

The %store command lets you pass variables between two different notebooks.

data = 'this is the string I want to pass to different notebook' %store data

Now, in a new notebook… %store -r data print(data) this is the string I want to pass to different notebook

I've successfully tested with sklearn dataset :

from sklearn import datasets

dataset = datasets.load_iris()

%store dataset

in notebook to read data :

%store -r dataset

src : https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/

IPython supports the %store magic (here is the documentation). It seems to have the same constraints of pickle: if the file can be pickled it will also be storable.

Anyway, it will work for sure with common Python types. Here's a basic example:

var_1 = [1,2,3,4] #list
var_2 = {'a':1,'b':2,'c':3} #dict
var_3 = (6,7,8) #tuple
var_4 = {'d','e','f'} #set
%store var_1
%store var_2
%store var_3
%store var_4
 Stored 'var_1' (list)
 Stored 'var_2' (dict)
 Stored 'var_3' (tuple)
 Stored 'var_4' (set)

Then on a different IPython notebook it will be sufficient to type:

%store -r var_1 
%store -r var_2 
%store -r var_3 
%store -r var_4

If your data is in a single variable then have a try at saving it to a file using the %save magic in one notebook and then reading it back in another.

The one difficulty is that the text file will contain the data but no variable definition so I usually contatenate it with a variable definition and then exec the result.

dmargol1

I believe that theoretically you should be able to do so with messaging, though I would have to dig a lot deeper to figure it out.

Why would you need this capability though?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!