Goal
So I’m trying to both build an app with a couple of goals.
- I need it
- I want folks who don’t know panel very well to be able to understand and maintain it
- I think it would make a good blog post, improve docs to show how you would go from experimenting to something you would actually deploy
Anyway, I’m really liking the reactive API of Panel as something that is fairly straightforward to understand and explain to folks but I’m having trouble moving from the functional approach to a working Class based approach. I have a small example below that demonstrates the issue.
any pointers are greatly appreciated.
Caveats:
- avoid using Param directly since it would be one more thing to explain. just use widgets.
- minimal changes between the functional and class approach
Function Approach (this works):
import panel as pn
pn.extension()
_countries = {'Africa': ['Ghana', 'Togo', 'South Africa'],
'Asia' : ['China', 'Thailand', 'Japan', 'Singapore'],
'Europe': ['Austria', 'Bulgaria', 'Greece', 'Switzerland']}
continent = pn.widgets.AutocompleteInput(
name='Continent',
options=list(_countries.keys()),
case_sensitive=False,
placeholder='Enter Continent',
)
country = pn.widgets.Select(name='Country')
@pn.depends(continent, watch=True)
def _update_countries(continent):
countries = _countries[continent]
country.options = countries
pn.Row(continent, country)
Class Based Approach (does not work)
- (minor) The Jupyter cell does not expand vertically to show the options in the Autocompleter
- (critical) country list does not update on selection of continent. I’ve tried various ways of writing the class. I saw some examples using
pn.viewer.Viewer
but couldn’t find any docs so the example below is cobbled together from various sources.
import panel as pn
pn.extension()
class Viewer(pn.viewable.Viewer):
_countries = {'Africa': ['Ghana', 'Togo', 'South Africa'],
'Asia' : ['China', 'Thailand', 'Japan', 'Singapore'],
'Europe': ['Austria', 'Bulgaria', 'Greece', 'Switzerland']}
continent = pn.widgets.AutocompleteInput(
name='Continent',
options=list(_countries.keys()),
case_sensitive=False,
placeholder='Enter Continent',
)
country = pn.widgets.Select(name='Country')
@pn.depends('continent', watch=True)
def _update_countries(self):
countries = self._countries[self.continent]
self.country.options = countries
def __panel__(self):
return pn.Row(self.continent, self.country)
view = Viewer()
view
I’ve tried a couple of different ways of passing things to pn.depends
in the Class, i.e. "continent"
vs continent
vs continent.value
etc but it doesn’t seem like Panel is watching for the changes.