I am building some interactive graphs and am trying to build one with second dynamic dropdown based on the first.
def update_dropdown(change):
global country_list
global country_list2
global drop2
print(change.new)
##country_list2 = country_list.remove(change.new) <- Not working because of multiple values
##drop2.options = country_list2
country_list2 = country_list
drop1 = widgets.Dropdown(options=country_list, value='United States', description='Country:')
drop2 = widgets.Dropdown(options=country_list2, value='China', description='Country:')
drop1.observe(update_dropdown)
interactive(compare_country_graph, Country1=drop1, Country2=drop2, Column=widgets.Dropdown(options=column_list, value='GDP', description='Metric:'))
**Ouput:**
{'index': 189}
Uruguay
Uruguay
189
{}
The problem is, change.new is returning mutiple values and no matter how I try and spin it, I can't just get one value. I have tried lists, dictionaries, series, and arrays to no avail.
More code I have tried:
def update_dropdown(change):
global country_list
global country_list2
global drop2
global y
global newword
y= []
for letter in str(change.new):
if letter.isalpha():
y.append(letter)
new = ""
for x in y:
new += x
print(new)
**Output:**
index
Uruguay
Uruguay
Does anyone know how I can access and get ONLY the country name from this dropdown?
You can simply specify names='value'
in your observe function and you will get the result you want.
See code below as an example:
import ipywidgets as widgets
country_list=['Uruguay', 'France','United States','Brazil']
def update_dropdown(change):
print(change.new)
drop1 = widgets.Dropdown(options=country_list, value='United States', description='Country:')
drop1.observe(update_dropdown,names='value')
display(drop1)