3

I have a dropdown menu that shows soms posssible 'cycles'. These are actually .csv files containing some data. Then I have another dropdown that should show all the variables in this .csv file. So this second dropdown should update according to the file selected in the first dropdown. I found a similar question and tried to implement this, but it did not work. The second dropdown remains empty. Note that I use pyspark to read the .csv files.

Cycles is a tuple containing all the .csv files in my working directory.

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)

What goes wrong?

Community
  • 1
  • 1
user3387899
  • 601
  • 5
  • 18

1 Answers1

2

Lol 4 years, but I've been looking for this...

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)
fakedane
  • 95
  • 11