python - Jupyter Notebook Widgets: Create dependent dropdowns -


i want create 2 dropdown widgets in jupyter notebook. dropdown content taken dataframe.

let's have pandas dataframe consisting of 3 categorical variables 'a', 'b', 'c'. 'a' has 3 subtypes 'a1','a2' , 'a3'. 'b' , 'c' similar in sense have own subtypes. want create 2 dropdown widgets: first dropdown widget have ['a','b','c'], , second dropdown widget display subtypes depending on variable user selects first widget.

i have idea how this. i'll try write out codes this:

import pandas pd ipython.display import * import ipywidgets widgets ipywidgets import *  # create dataframe df = pd.dataframe([['a1','a2','a3'],              ['b1','b2','b3'],              ['c1','c2','c3']], index = ['a','b','c']).transpose()  # widgets widget1 = dropdown(options = ['a','b','c']) display(widget1) widget2 = dropdown(???????) display(widget2) 

and depending on select 2 dropdown widgets, want function executed.

any appreciated.

i found out how this. hope helps else who's looking same thing.

x_widget = dropdown(options = ['a','b','c']) y_widget = dropdown()  # define function updates content of y based on select x def update(*args):     y_widget.options = df[x_widget.value].unique().tolist() x_widget.observe(update)  # function want executed def random_function(): ...  interact(random_function,          x = x_widget,          y = y_widget); 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -