And here is the second answer that talks about the routing
module instead of answering your question 
When I started using the routing module, I switched the self.search_text.change
event from performing the search to redirecting to the same page with f'?search_term={self.search_text.text}'
, then performing the search in the form_show
event.
My instinct says that this may be not ideal with very complex forms, but that’s what I do for example in a form with 6 data grids, each with hundreds of items, one of them has dozens of drawings dynamically generated in a canvas of its template. The whole form is regenerated, including the things that are not affected by the search, but you don’t even see it flickering. I will listen to my instinct only the day after I see performance problems.
The advantages are two:
- I don’t need to do the search twice, when it’s driven by the url and when it’s driven by the UI
- I can always copy the url and paste it in an email to go to the same page with the same search terms, regardless of how I ended up in that page with those search terms
In other words, if you look at the url for any page of any of my apps, you can see how many input elements each form has, because every change on every value of every input element is immediately reflected on the url.
I need to keep track of which input element has the focus, so I can restore it after the form has been rendered, and I do it with the local_storage
module of Anvil Extras.
Here is an example of the form_show
event:
def form_show(self, **event_args):
self.search_text.text = self.url_dict.get('search', '')
self.status_filter.text = self.url_dict.get('status', Status.default_filter)
self.facility_filter.text = self.url_dict.get('facility', Facility.default_filter)
focus_was_on = local_storage.get('focus')
if focus_was_on == 'status_filter':
self.status_filter.focus()
elif focus_was_on == 'facility_filter':
self.facility_filter.focus()
else:
self.search_text.focus()
And the function assigned to both search button click
and search text pressed_enter
events:
def search_click(self, **event_args):
local_storage['focus'] = 'search_text'
routing.clear_cache()
routing.set_url_hash(url_pattern='projects', url_dict={
'search': self.search_text.text,
'status_filter': self.status_filter.text,
'facility_filter': self.facility_filter.text,
})
I also often switched from using the self.search_text.change
to using the self.search_text.pressed_enter
event, just to decrease the flickering.