I have a data set with 3 of the columns having categorical values. I want to create 3 drop downs in Bokeh or HoloViews in which the first drop down selection determines the values of the list in the other 2 drop downs. Can anyone point me to any of the tutorials or blog or docs that show how to this. I don't seem to fine any. I appreciate your time. Thanks!
2 Answers
If you are working in a Jupyter notebook, you can use paramnb to do this, taking advantage of the way a notebook separates code into different cells:
Here I did "Run all", then selected c3 in the first widget, which re-runs the two cells below it to update them. I then selected a "high" value in the second set of widgets, which re-runs the plot to update it.
This pattern will let you do arbitrary chaining like this if you have notebooks, and if Jupyter Dashboards is an option for you then you can string these cells together to make an application. But this is just one approach...
Here's an example where the values of one dropdown change dependent of the selection made in the other dropdown.
In the example, when you select a continent in one dropdown, the dropdown with possible countries changes in the other dropdown. This happens because of: @pn.depends(continent.param.value, watch=True)
import panel as pn
_countries = {
'Africa': ['Ghana', 'Togo', 'South Africa'],
'Asia' : ['China', 'Thailand', 'Japan'],
'Europe': ['Austria', 'Bulgaria', 'Greece']
}
continent = pn.widgets.Select(
value='Asia',
options=['Africa', 'Asia', 'Europe']
)
country = pn.widgets.Select(
value=_countries[continent.value][0],
options=_countries[continent.value]
)
# the countries dropdown is dependent on the continent dropdown
@pn.depends(continent.param.value, watch=True)
def _update_countries(continent):
countries = _countries[continent]
country.options = countries
country.value = countries[0]
pn.Row(continent, country)
This holoviews + panel question has an example of dropdowns that are dependent on the value of another dropdown:
How do i automatically update a dropdown selection widget when another selection widget is changed? (Python panel pyviz)
The example with dropdowns dependent on other dropdowns comes from the GoogleMapViewer on this tutorial page: https://panel.pyviz.org/user_guide/Param.html