I am new to Python and I want to create an interactive dropdown list from an ipywidget. The main purpose is to update the dropdown list based on two other widgets. In the code below, the widget plotType will be updated based on the input from the widgets headers_x and headers_y (both refer to the dataframe columns selected for plotting). If both headers_x and headers_y have the Select option, then the plotType needs to show "Make selection". But if the headers_x and headers_y have other options selected (columns from the dataframe), then the plotType needs to change accordingly. If the headers_x and headers_y are both numeric, then the plotType needs to show: numericVsNumeric, but if headers_x is categorical and headers_y is numeric, then plotType needs to show 'catergoricalVsNumeric' I have attempted my solution as follows, but the options in the plotType widget do not update. Any help is much appreciated. Thank you.
from ipywidgets import *
import seaborn.apionly as sns
df = sns.load_dataset('iris')
#identifies the columns in the dataframe
df_cols = list(df.columns.values)
df_cols.insert(0, 'Select')
str_cols = list(df.select_dtypes(include=['object']).columns.values)
str_cols.insert(0, 'Select')
#plot function
def set_plot(headers_x, headers_y, plotType):
data = df
#plotting functions to be added
#function to specify the type of plot based on users input
def set_plotType():
data = df
#If no selection has been made
if headers_x.value == 'Select' and headers_y.value == 'Select':
init = list(['Make Selection'])
else:
#if x and y are both numeric
if data[headers_x.value].dtype == np.float and data[headers_y.value].dtype == np.float:
init = list(['NumericVsNumeric'])
#if x is categorical and y is numeric
elif data[headers_x.value].dtype == np.object and data[headers_y.value].dtype == np.float:
init = list(['CategoricalVsNumeric'])
return init
#define widgets
headers_x = widgets.Dropdown(
options=df_cols,
value=df_cols[0],
description='X'
)
headers_x.set_title = 'headers_x'
headers_y = widgets.Dropdown(
options=df_cols,
value=df_cols[0],
description='Y'
)
headers_y.set_title = 'headers_y'
plotType = widgets.Dropdown(
options=set_plotType(),
#value=df_cols[0],
description='Plot Type'
)
plotType.set_title = 'plotType'
#interact function
interact(set_plot, headers_x = headers_x, headers_y = headers_y, plotType = plotType)
