I have a dataframe that contains the category of the item, item_id, the sales of all days and the total sales of each product(sum(axis=1)).
The next 4 dataframes have 2 columns item_id and sales depending on the category and their name is household_sort,hobbies_sort,foods_sort and sales_total that includes the total sales of each product.
I want to create 3 dependent widgets (2 drop menu and 1 slide bar) at jupyter notebook.First i want a drop menu about some categories of prducts (for example sales of foods) which are defined in dictionary cat in the code below.After a slidebar where the user will specify the limits of total sales from the category of th first drop menu.Finally drop menu with item id according to the previous I tried this code but i cant change the values of the second drop menu.
from ipywidgets import interact, Dropdown
import ipywidgets as wg
range_slider=wg.IntRangeSlider(value=[1000,10000],min=sales_total['total'].min() - 5,max=sales_total['total'].max() + 5,description='Test:',readout_format='d')
cat={'HOBBIES_SALES':hobbies_sort[(hobbies_sort['total']>= range_slider.value[0]) & (hobbies_sort['total']<=range_slider.value[1])]['item_id'],
'HOUSEHOLD_SALES':household_sort[(household_sort['total']>= range_slider.value[0]) & (household_sort['total']<=range_slider.value[1])]['item_id'],
'FOODS_SALES':foods_sort[(foods_sort['total']>= range_slider.value[0]) & (foods_sort['total']<=range_slider.value[1])]['item_id'],
'TOTAL_SALES':sales_total[(sales_total['total']>=range_slider.value[0]) & (sales_total['total']<=range_slider.value[1])]['item_id']}
catW=Dropdown(options=cat.keys())
idW=Dropdown(options=cat[catW.value])
display(catW,range_slider,idW)
Both dropdowns aren't essentially linked. Since you are populating the values on the server side you may link both widgets using observe
and an event handler of choice.
Add an event handler (reference) as follows which updates the options available in the second dropdown based on the selected category in the first. You can add as many events as desired and perform any computations in the handler. This was tested in Python 3 in Jupyter Notebook v5.7.3
from ipywidgets import interact, Dropdown
import ipywidgets as wg
range_slider=wg.IntRangeSlider(value=[1000,10000],min=sales_total['total'].min() - 5,max=sales_total['total'].max() + 5,description='Test:',readout_format='d')
cat={'HOBBIES_SALES':lambda : hobbies_sort[(hobbies_sort['total']>= range_slider.value[0]) & (hobbies_sort['total']<=range_slider.value[1])]['item_id'],
'HOUSEHOLD_SALES':lambda : household_sort[(household_sort['total']>= range_slider.value[0]) & (household_sort['total']<=range_slider.value[1])]['item_id'],
'FOODS_SALES':lambda : foods_sort[(foods_sort['total']>= range_slider.value[0]) & (foods_sort['total']<=range_slider.value[1])]['item_id'],
'TOTAL_SALES':lambda : sales_total[(sales_total['total']>=range_slider.value[0]) & (sales_total['total']<=range_slider.value[1])]['item_id']}
catW=Dropdown(options=cat.keys())
idW=Dropdown(options=cat[catW.value]())
# Creating event handler
def ddl_event_handler(event):
idW.options=cat[event['new']]() #updating the options to the selected value
# Add an observer
catW.observe(ddl_event_handler,names="value")
#optionally specify a `display_id` to update the same area
display(catW,range_slider,idW,display_id="options_area")
Linking Widgets - https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Events.html