1. <i id='UVaUc'><tr id='UVaUc'><dt id='UVaUc'><q id='UVaUc'><span id='UVaUc'><b id='UVaUc'><form id='UVaUc'><ins id='UVaUc'></ins><ul id='UVaUc'></ul><sub id='UVaUc'></sub></form><legend id='UVaUc'></legend><bdo id='UVaUc'><pre id='UVaUc'><center id='UVaUc'></center></pre></bdo></b><th id='UVaUc'></th></span></q></dt></tr></i><div id='UVaUc'><tfoot id='UVaUc'></tfoot><dl id='UVaUc'><fieldset id='UVaUc'></fieldset></dl></div>
      <legend id='UVaUc'><style id='UVaUc'><dir id='UVaUc'><q id='UVaUc'></q></dir></style></legend>
    2. <small id='UVaUc'></small><noframes id='UVaUc'>

    3. <tfoot id='UVaUc'></tfoot>

        • <bdo id='UVaUc'></bdo><ul id='UVaUc'></ul>

        如何在散景 python 中捕获下拉小部件的值?

        How to capture value of dropdown widget in bokeh python?(如何在散景 python 中捕获下拉小部件的值?)

        <small id='wHdOR'></small><noframes id='wHdOR'>

        <tfoot id='wHdOR'></tfoot>
            <tbody id='wHdOR'></tbody>
            <bdo id='wHdOR'></bdo><ul id='wHdOR'></ul>

                  <legend id='wHdOR'><style id='wHdOR'><dir id='wHdOR'><q id='wHdOR'></q></dir></style></legend>
                1. <i id='wHdOR'><tr id='wHdOR'><dt id='wHdOR'><q id='wHdOR'><span id='wHdOR'><b id='wHdOR'><form id='wHdOR'><ins id='wHdOR'></ins><ul id='wHdOR'></ul><sub id='wHdOR'></sub></form><legend id='wHdOR'></legend><bdo id='wHdOR'><pre id='wHdOR'><center id='wHdOR'></center></pre></bdo></b><th id='wHdOR'></th></span></q></dt></tr></i><div id='wHdOR'><tfoot id='wHdOR'></tfoot><dl id='wHdOR'><fieldset id='wHdOR'></fieldset></dl></div>
                2. 本文介绍了如何在散景 python 中捕获下拉小部件的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  链接中散景 0.12.1 的官方文档提供了以下用于创建下拉列表的代码.

                  The official documentation of bokeh 0.12.1 in the link give the below code for creating a dropdown.

                  http://docs.bokeh.org/en/latest/docs/user_guide/interaction/widgets.html#userguide-interaction-widgets

                  但它没有明确提及当有人单击并从下拉列表中选择一个值时如何捕获下拉小部件的值.

                  But it doesn't clearly mention how to capture the value of the dropdown widget when someone click and selects a value from the dropdown.

                  from bokeh.io import output_file, show
                  from bokeh.layouts import widgetbox
                  from bokeh.models.widgets import Dropdown
                  
                  output_file("dropdown.html")
                  
                  menu = [("Item 1", "item_1"), ("Item 2", "item_2"), None, ("Item 3", "item_3")]
                  dropdown = Dropdown(label="Dropdown button", button_type="warning", menu=menu)
                  
                  show(widgetbox(dropdown))
                  

                  问题

                  看到有两个方法叫做 on_click() &on_change() 但从文档中无法弄清楚如何捕获该值.我们如何将选定的值分配给新变量?

                  Is see that there are 2 methods called on_click() & on_change() but from the documentation couldn't figure out how to capture the value. How can we assign the selected value to a new variable?

                  编辑

                  根据@Ascurion 的输入,我更新了我的代码,如下所示.但是当我在下拉列表中选择一个值时,Spyder 的 ipython 控制台中不会打印任何内容.请指教.

                  Based on input from @Ascurion i have updated my code as shown below. But when i select a value in dropdown nothing is printed in ipython console in Spyder. Please advise.

                      from bokeh.io import output_file, show
                      from bokeh.layouts import widgetbox
                      from bokeh.models.widgets import Dropdown
                  
                      output_file("dropdown.html")
                  
                  
                      menu = [("Item 1", "item_1"), ("Item 2", "item_2"), None, ("Item 3", "item_3")]
                      dropdown = Dropdown(label="Dropdown button", button_type="warning", menu=menu)
                  
                      def function_to_call(attr, old, new):
                          print dropdown.value
                  
                      dropdown.on_change('value', function_to_call)
                      dropdown.on_click(function_to_call)
                      show(widgetbox(dropdown))
                  

                  推荐答案

                  EDIT 此答案不再适用于 Bokeh Versions 2.X.X.请参阅下面的评论和其他答案.

                  EDIT This answer does not apply for Bokeh Versions 2.X.X anymore. See comment and the other answer below.

                  如果你设置 on_change 例如如下:

                  If you set on_change e.g. as follows:

                  dropdown.on_change('value', function_to_call)
                  

                  可以在function_to_call中访问被选中项的值,如下:

                  one can access the value of the selected item in function_to_call as follows:

                  def function_to_call(attr, old, new):
                      print dropdown.value
                  

                  为此,必须在 function_to_call 之前定义下拉菜单.

                  For this to work dropdown has to be defined before function_to_call.

                  有关如何使用 on_click 和 on_change(bokeh 版本 12.1)访问小部件中设置的值的文档可以在页面顶部找到:

                  The documentation on how to access values set in widgets with on_click and on_change (bokeh version 12.1) can be found here at the top of the page:

                  http://docs.bokeh.org/en/latest/docs/user_guide/interaction/widgets.html

                  编辑

                  要获得交互式反馈,您必须在服务器模式下运行散景,以便在您与小部件交互时评估 Python 代码.我稍微更改了您的示例以允许使用

                  To get interactive feedback you have to run bokeh in server mode, so that the python code can be evaluated when you interact with a widget. I changed your example slightly to allow to be run with the

                  bokeh serve --show file_name.py
                  

                  命令.然后下面的代码会在终端中打印出选定的项目.

                  command. The code below then prints out the selected item in the terminal.

                  from bokeh.io import output_file, show
                  from bokeh.layouts import widgetbox
                  from bokeh.models.widgets import Dropdown
                  from bokeh.plotting import curdoc
                  
                  menu = [("Quaterly", "time_windows"), ("Half Yearly", "time_windows"), None, ("Yearly", "time_windows")]
                  dropdown = Dropdown(label="Time Period", button_type="warning", menu=menu)
                  
                  def function_to_call(attr, old, new):
                      print dropdown.value
                  
                  dropdown.on_change('value', function_to_call)
                  
                  curdoc().add_root(dropdown)
                  

                  更多信息请看这里:

                  http://docs.bokeh.org/en/latest/docs/user_guide/server.html

                  这篇关于如何在散景 python 中捕获下拉小部件的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

                      <tfoot id='B3Otn'></tfoot>
                        <legend id='B3Otn'><style id='B3Otn'><dir id='B3Otn'><q id='B3Otn'></q></dir></style></legend>

                          <bdo id='B3Otn'></bdo><ul id='B3Otn'></ul>
                        • <i id='B3Otn'><tr id='B3Otn'><dt id='B3Otn'><q id='B3Otn'><span id='B3Otn'><b id='B3Otn'><form id='B3Otn'><ins id='B3Otn'></ins><ul id='B3Otn'></ul><sub id='B3Otn'></sub></form><legend id='B3Otn'></legend><bdo id='B3Otn'><pre id='B3Otn'><center id='B3Otn'></center></pre></bdo></b><th id='B3Otn'></th></span></q></dt></tr></i><div id='B3Otn'><tfoot id='B3Otn'></tfoot><dl id='B3Otn'><fieldset id='B3Otn'></fieldset></dl></div>

                          <small id='B3Otn'></small><noframes id='B3Otn'>

                            <tbody id='B3Otn'></tbody>