【问题标题】:Bokeh: textInput on_change function on widget doesn't work as expected散景:小部件上的 textInput on_change 功能无法按预期工作
【发布时间】:2020-05-03 17:12:00
【问题描述】:

我的短脚本如下所示:

output_server('ts_sample.html')

count = 0
def update_title(attrname, old, new):
    global count
    count = count + 1

textInput = TextInput(title="query_parameters", name='fcp_chp_id', value='fcp_chp_id')
textInput.on_change('value', update_title)

curdoc().add_root(textInput)
p = figure( width=800, height=650,title="ts_sample",x_axis_label='datetime' )
p.line(np.array(data['date_trunc'].values, dtype=np.datetime64), data['latitude'], legend="test")
p.xaxis[0].formatter=bkmodels.formatters.DatetimeTickFormatter(formats=dict(hours=["%F %T"]))
show(curdoc())

当 bokeh server(bokeh serve) 正在运行并且我得到了绘图时,它可以工作,但是 on_change 回调没有按预期工作。

假设 textInput 的值应该是输入框中的内容/字符串,但我多次更改但从未调用回调函数 update_title(count 全局变量始终为 0)。所以显然底层的 textInput.value 没有改变,我怎样才能改变 value attr 并触发 on_change 函数?

【问题讨论】:

    标签: bokeh


    【解决方案1】:

    这是一个简单的TextInput 示例,它使用回调而不是.on_change()。对于像我这样的初学者来说,这可能比 OP 更有帮助。我从

    稍微修改了滑块示例

    http://docs.bokeh.org/en/latest/docs/user_guide/interaction/callbacks.html#customjs-for-model-property-events

    from bokeh.layouts import column
    from bokeh.models import CustomJS, ColumnDataSource, Slider
    from bokeh.models import TextInput
    from bokeh.plotting import figure, show
    
    x = [x*0.005 for x in range(0, 200)]
    y = x
    
    source = ColumnDataSource(data=dict(x=x, y=y))
    
    plot = figure(plot_width=400, plot_height=400)
    plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)
    
    callback = CustomJS(args=dict(source=source), code="""
            var data = source.get('data');
            var f = cb_obj.get('value')
            x = data['x']
            y = data['y']
            for (i = 0; i < x.length; i++) {
                y[i] = Math.pow(x[i], f)
            }
            source.trigger('change');
        """)
    
    #slider = Slider(start=0.1, end=4, value=1, step=.1, title="power", callback=callback)
    #layout = vform(slider, plot)
    
    text_input = TextInput(value="1", title="power", callback=callback)
    layout = column(text_input, plot)
    
    show(layout)
    

    【讨论】:

      【解决方案2】:

      我和你有同样的问题。 搜索后,on_change 函数不适用于 bokeh 0.10 版本,但适用于即将发布的 0.11 版本。

      发件人:https://groups.google.com/a/continuum.io/forum/#!topic/bokeh/MyztWSef4tI

      如果您在最新的开发版本中使用(新)Bokeh 服务器,您可以按照以下示例进行操作,例如: https://github.com/bokeh/bokeh/blob/master/examples/app/sliders.py

      发件人:https://groups.google.com/a/continuum.io/forum/#!topic/bokeh/PryxrZPX2QQ

      服务器最近完全从头开始重写 并且更快、更小/更简单、更易于使用和部署 解释。主要 PR 刚刚合并到 master 中,将显示在 即将在 12 月发布的 0.11 版本

      下载开发版:https://anaconda.org/bokeh/bokeh/files

      【讨论】:

      • @A Laurent thx 获取信息,等待版本 0.11
      【解决方案3】:

      我改编了这个例子,它可能会有所帮助:

      from bokeh.layouts import column
      from bokeh.models import TextInput
      from bokeh.models import CustomJS, ColumnDataSource, Slider
      from bokeh.plotting import figure, show
      
      x = [x*0.005 for x in range(0, 200)]
      y = x
      
      source = ColumnDataSource(data=dict(x=x, y=y))
      
      plot = figure(plot_width=400, plot_height=400)
      plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)
      
      callback = CustomJS(args=dict(source=source), code="""
              var data = source.data;
              var f = cb_obj.value;
              x = data['x']
              y = data['y']
              for (i = 0; i < x.length; i++) {
                  y[i] = Math.pow(x[i], f)
              }
              source.change.emit();
          """)
      
      #slider = Slider(start=0.1, end=4, value=1, step=.1, title="power", callback=callback)
      #layout = vform(slider, plot)
      
      text_input = TextInput(value="1", title="power", callback=callback)
      layout = column(text_input, plot)
      
      show(layout)
      

      【讨论】:

      • 这在散景 1.4.0 中按预期工作。上面的例子没有
      猜你喜欢
      • 2020-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-12
      • 1970-01-01
      • 1970-01-01
      • 2020-08-04
      相关资源
      最近更新 更多