【问题标题】:Update dropdown based on selection of another dropdown in ipywidgets根据 ipywidgets 中另一个下拉列表的选择更新下拉列表
【发布时间】:2016-11-23 15:50:26
【问题描述】:

我有一个下拉菜单,显示一些可能的“循环”。这些实际上是包含一些数据的 .csv 文件。然后我有另一个下拉列表,应该显示这个 .csv 文件中的所有变量。因此,第二个下拉列表应根据第一个下拉列表中选择的文件进行更新。我找到了a similar 问题并试图实现这一点,但它没有奏效。第二个下拉列表仍然为空。请注意,我使用 pyspark 来读取 .csv 文件。

Cycles 是一个包含我工作目录中所有 .csv 文件的元组。

scW1 = widgets.Dropdown(description = 'Select file',options = cycles)
scW2 = widgets.Dropdown(description = 'Select variable')

def plotvar(sender):
    df=sc.textFile(str(scW1.value)).map(lambda x: x.split(','))
    header = tuple(df.first())
    print(header)
    with scW2.hold_trait_notifications():
        scW2.options = header

display(scW1)
display(scW2)

出了什么问题?

【问题讨论】:

    标签: python widget ipywidgets


    【解决方案1】:

    大声笑 4 年,但我一直在寻找这个......

    from IPython.display import display
    from ipywidgets import Dropdown
    
    def dropdown_eventhandler(change):
        determine(dropdown.value)
    #     print(change.new)
    
    option_list = (1, 2)
    dep_1 = (1, 1)
    dep_2 = (2, 2)
    
    def determine(x):
        if x == 1:
            dropdown_dep.options = dep_1
        else:
            dropdown_dep.options = dep_2
    
    dropdown = Dropdown(description="Choose one:", options=option_list)
    dropdown_dep = Dropdown(description="Choose one:")
    dropdown.observe(dropdown_eventhandler, names='value')
    display(dropdown, dropdown_dep)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-04
      • 2021-04-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多