简体   繁体   English

在 IPython 笔记本之间共享数据

[英]Share data between IPython Notebooks

If I have several IPython notebooks running on the same server.如果我在同一台服务器上运行多个 IPython 笔记本。 Is there any way to share data between them?有没有办法在它们之间共享数据? For example, importing a variable from another notebook?例如,从另一个笔记本导入一个变量? Thanks!谢谢!

This works for me :这对我有用:

The %store command lets you pass variables between two different notebooks. %store 命令允许您在两个不同的笔记本之间传递变量。

data = 'this is the string I want to pass to different notebook' %store data data = '这是我想传递给不同笔记本的字符串' %store data

Now, in a new notebook… %store -r data print(data) this is the string I want to pass to different notebook现在,在一个新笔记本中... %store -r data print(data) 这是我想传递给不同笔记本的字符串

I've successfully tested with sklearn dataset :我已经成功地测试了 sklearn 数据集:

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/源代码: https : //www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/

Notebooks in Jupyter Lab can share the same kernel. Jupyter Lab 中的笔记本可以共享相同的内核。 In your notebook you can choose the kernel of another notebook and variables from the other notebook will be available in both notebooks.在您的 notebook 中,您可以选择另一个 notebook 的内核,并且来自另一个 notebook 的变量将在两个 notebook 中都可用。

屏幕截图 Jupyter Lab 内核选择弹出窗口

  1. Click on the button that describes your current kernel.单击描述您当前内核的按钮。
  2. Choose the kernel of the other notebook, whose variables you want to access.选择另一个笔记本的内核,您要访问其变量。

IPython supports the %store magic ( here is the documentation ). IPython 支持%store魔法( 这里是文档)。 It seems to have the same constraints of pickle : if the file can be pickled it will also be storable.它似乎具有相同的pickle约束:如果文件可以被腌制,它也将是可存储的。

Anyway, it will work for sure with common Python types.无论如何,它肯定适用于常见的 Python 类型。 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:然后在不同的 IPython 笔记本上输入:

%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.如果您的数据在单个变量中,那么尝试在一个笔记本中使用%save魔法将其%save到一个文件中,然后在另一个笔记本中读取它。

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.一个困难是文本文件将包含数据但没有变量定义,所以我通常将它与变量定义连接起来,然后exec结果。

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?你为什么需要这种能力?

Thanks a lot for these answers. 非常感谢您提供这些答案。 It is really needed in various scenarios. 在各种情况下确实需要它。 In my case I have a dataframe of around 5 lakhs rows. 就我而言,我有一个大约50万行的数据框。 I am applying a Bert vector operations on all the columns which contains text. 我在包含文本的所有列上应用了Bert向量运算。 As the process starts you won't be able to look what actual values have been put inside the dataframe. 在该过程开始时,您将无法查看在数据框内已放置了哪些实际值。 Hence using this %save magic function we can keep on looking at the progress from another notebook. 因此,使用此%save魔术功能,我们可以继续查看其他笔记本的进度。 Thanks a lot guys. 非常感谢。 Thanks @Kyle for asking the question 感谢@Kyle提出问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM