gpt4 book ai didi

ipython - 创建在单元运行后发生变化的 Jupyter Notebook 对象

转载 作者:行者123 更新时间:2023-12-01 02:04:01 32 4
gpt4 key购买 nike

我想制作一个在 Jupyter notebook 中异步更新的进度条(使用 ipython 内核)

例子

In [1]: ProgressBar(...)
Out[1]: [|||||||||------------------] 35% # this keeps moving

In [2]: # even while I do other stuff

我计划启动一个后台线程来检查和更新进度。我不确定如何更新渲染的输出(或者即使这是可能的。)

最佳答案

这可能有助于让您走上正轨,代码取自 Lightning-viz,其中大量借鉴了 matplotlib。警告这一切都没有充分记录。

在python中你必须实例化一个comm对象

from IPython.kernel.comm import Comm
comm = Comm('comm-target-name', {'id': self.id})

完整代码在这里 https://github.com/lightning-viz/lightning-python/blob/master/lightning/visualization.py#L15-L19 .例如,如果您想管理多个不同的进度条,该 id 就在那里。

然后在javascript中做同样的事情:
var IPython = window.IPython;

IPython.notebook.kernel.comm_manager.register_target('comm-target-name', function(comm, data) {
// the data here contains the id you set above, useful for managing
// state w/ multiple comm objects

// register the event handler here
comm.on_msg(function(msg) {
})
});

完整的例子在这里。注意 javascript on_msg代码未经测试,因为我只使用 comm 从 js -> python 开始。要查看该处理程序的样子,请参阅 https://github.com/lightning-viz/lightning-python/blob/master/lightning/visualization.py#L90

最后在python中发送消息:
comm.send(data=data)

https://ipython.org/ipython-doc/3/api/generated/IPython.kernel.comm.comm.html#IPython.kernel.comm.comm.Comm.send

关于ipython - 创建在单元运行后发生变化的 Jupyter Notebook 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34033652/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com