如何使用python代码在jupyter笔记本中保存ipython小部件的状态 [英] How to save state of ipython widgets in jupyter notebook using python code

查看:390
本文介绍了如何使用python代码在jupyter笔记本中保存ipython小部件的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jupyter笔记本中使用ipython小部件.现在,当内核重新启动时,小部件状态会丢失,并显示一条消息:

I am using ipython widgets in my jupyter notebook. Now when kernel is restarted, the widget state is lost and a message is displayed:

无法显示Jupyter窗口小部件,因为找不到窗口小部件状态.如果存储窗口小部件的内核不再可用,或者窗口小部件状态未保存在笔记本中,则可能发生这种情况.您可以通过运行适当的单元格来创建窗口小部件."

"Jupyter widget could not be displayed because the widget state could not be found. This could happen if the kernel storing the widget is no longer available, or if the widget state was not saved in the notebook. You may be able to create the widget by running the appropriate cells"

因此,当用户在内核重新启动后重新打开其笔记本时,他没有在笔记本中找到以前拥有的小部件.我们要保存所有小部件的状态,以便在内核重新启动后,当用户重新打开笔记本时,他发现他的所有小部件都完好无损.

So when a user reopens his notebook after a kernel restart, he doesn't find the widget in the notebook that he had earlier. We want to save all widgets state so the after kernel restart when user reopens the notebook, he finds all his widgets intact.

PS:有一个保存小部件状态"菜单选项可以执行此操作,但这是一个手动步骤,我们希望通过我们的python代码保存小部件状态.

PS: There is "save widget state" menu option which does that but that is a manual step and we want widgets state to be saved through our python code.

推荐答案

这有点骇人听闻,可能会带来意想不到的后果.在您通过交互方式调用的每个函数上,都可以将调用添加到以下函数:

This is a bit of a hack and may have unintended consequences. On every function you call from interactive, you can add a call to the function below:

def save_with_widgets ():
    display(
        HTML('<script>Jupyter.menubar.actions._actions["widgets:save-with-widgets"].handler()</script>') )

它将保存您的笔记本(请注意),并随其保存小部件状态.

It will save your notebook (be aware of that), and the widget state with it.

但是,请注意,当您运行具有实际小部件的单元格时,它将不遵守保存状态.

However, please note that, when you run the cell that has the actual widget, it will not respect the saved state.

这不是上面的功能所特有的;如果使用保存小部件状态菜单选项,也会发生同样的情况.

That's not specific to the function above; the same happens if you use the save widget state menu option.

所以...我不确定该选项是否有用.它将保存状态以向您显示上次笔记本的状态,但是您将无法在下一次运行时实际重用工单状态(嗯,您可以记下值并重新输入它们,但是我不要以为那是你想要的...)

So... I'm not really sure how useful that option is. It will save the state to show you how the notebook looked like last time, but you won't be able to actually reuse the ticket state on the next run (well, you can write the values down and re-enter them, but I don't think that is what you wanted...)

这篇关于如何使用python代码在jupyter笔记本中保存ipython小部件的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆