Jupyter和计时器功能? [英] Jupyter and timer functions?

查看:81
本文介绍了Jupyter和计时器功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Windows 10上通过Anaconda使用Jupyter.

我想绘制一个图,每秒绘制一个新的随机值.到目前为止,我有以下代码:

 <代码>导入plotly.graph_objs将numpy导入为np导入时间表,时间随机导入从ipywidgets导入小部件xs = np.linspace(0,10,100)ys = np.zeros(100)无花果= go.FigureWidget()fig.add_trace(go.Scatter(x = xs,y = ys,mode ='lines',name ='Random'))fig.update_xaxes(范围= [0,10])fig.update_yaxes(范围= [0,10])s = sched.scheduler(time.time,time.sleep)yi = 0def tick_func(sc):全球ys,yiys [yi] = random.random()* 10yi =(yi + 1)%100fig.data [0] .y = yss.enterabs(time.time()+ 1,1,tick_func,(sc,))#s.enterabs(time.time()+ 2,1,tick_func,(s,))s.run()widgets.VBox([fig]) 

此代码的输出如右图所示,显示在此屏幕截图中:

...,这是正确的起始/初始图.然后的想法是,一旦代码开始,我将每秒沿x轴获得一个新的随机值.

但是,一旦我启用/取消注释 s.enterabs(time.time()+ 2,1,tick_func,(s,))行-实际上会触发计时器功能开始循环-那么根本就没有输出(没有绘制图形)!没有错误,也没有输出!

那么,如何在Jupyter笔记本中运行计时器功能,并在绘图(此处为Plotly)上显示效果?

我在这里找到了有关 sched 的建议:

您还可以使用HBox等将代理窗口小部件与其他窗口小部件类型结合起来,并如上所述将代理窗口小部件用于计时目的.

请参阅 https://github.com/AaronWatters/jp_proxy_widget 以及关于异步性的讨论教程笔记本中的小部件.

I am using Jupyter via Anaconda on Windows 10.

I would like to have a plot, on which a new random value is plotted each second. So far, I have this code:

import plotly.graph_objs as go
import numpy as np
import sched, time
import random
from ipywidgets import widgets

xs = np.linspace(0, 10, 100)
ys = np.zeros(100)

fig = go.FigureWidget()
fig.add_trace(go.Scatter(x=xs, y=ys,
                    mode='lines',
                    name='Random'))
fig.update_xaxes(range=[0, 10])
fig.update_yaxes(range=[0, 10])

s = sched.scheduler(time.time, time.sleep)
yi = 0
def tick_func(sc):
    global ys, yi
    ys[yi] = random.random() * 10
    yi = (yi+1)%100
    fig.data[0].y = ys
    s.enterabs(time.time()+1, 1, tick_func, (sc,))
#s.enterabs(time.time()+2, 1, tick_func, (s,))
s.run()

widgets.VBox([fig])

The output of this code, as is, is shown on this screenshot:

... which is the correct starting/initial plot, as intended. The idea then is, as soon as the code has started, I would get a new random value along the x-axis, each second.

However, as soon as I enable/uncomment the s.enterabs(time.time()+2, 1, tick_func, (s,)) line - which actually triggers the timer function to start looping - then there is simply no output (no graph drawn)! No errors, but no output either!

So, how can I get a timer function running inside a Jupyter notebook, with effects shown on a plot (here Plotly)?

I found the recommendation for sched here: What is the best way to repeatedly execute a function every x seconds in Python? - but maybe it interferes with some of the implementation in Jupyter (or Plotly, or both?), so maybe there is something else I should use?

( possibly related, though I couldn't find much that helps me with this specific problem: Interactive Timer in Jupyter Notebook )

解决方案

The basic problem is that the code cell which displays a widget must finish executing before the widget will display.

What you need is a way for the widget to call back to the Python kernel after it has initialized in order to request an update. You could implement this using proxy widgets like this.

You can also combine proxy widgets with other widget types using HBox, etcetera and use the proxy widget for timing purposes as shown above.

Please see https://github.com/AaronWatters/jp_proxy_widget and the discussion of asynchronicity of widgets in the Tutorial notebook.

这篇关于Jupyter和计时器功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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