I have an app that opens and closes forms in the main page to switch between forms visually. When a user goes to a new page, the below lines are called:
# In main form
self.content_panel.clear()
self.content_panel.add_component(my_form())
Within my_form I have a date selector where users can select a date (called var_date). When a user first opens my_form, the date selector has no date. I would normally get this variable by using:
# In my_form
self.var_date = self.datepicker.date
I want to preserve var_date when my user switches between forms such that when they return to my_form, the date selector will default to var_date rather than no date.
My question is, are variables of a form class preserved when .clear() and .add_component()* are called? If so, how can I access a closed form’s variables? If not, how can I preserve variables between form transitions?
As long as the Form object continues to exist, its member variables do, too. Even if it’s not currently being displayed (i.e., has no displayable parent).
So if my_form() returns the same Form object every time, that object will remember. If it creates and returns a new instance (of the same Form subclass) each time, then the new instance has nothing to remember.
Edit: Note: Form instances exist per browser window. They’re not shared from one window to the next.
You are talking about two forms here: the main form, the owner of self.content_panel, and my_form, the form loaded in it with add_component(my_form()).
The main form is never deleted, so you could use get_open_form().some_variable = some_value on any form or module. It is not the most elegant way, but perfectly working.
The form added to it instead, it’s deleted, unless there is a reference to it. So this would keep the form alive for the whole session:
# in __init__
self.used_forms = {}
# where you load the form
self.content_panel.clear()
if 'my_form' not in self.used_forms:
self.used_forms['my_form'] = my_form()
self.content_panel.add_component(self.used_forms['my_form'])
So here I gave you two ways to remember globals: store it in the main form, or never delete a form, keep it alive instead.
A cleaner way is to store the globals in a module rather than in the main form.
I use the routing module from Anvil Extras, where you can decide whether a form is cached or regenerated from scratch.
Thanks, this is exactly what I was looking for. I wasn’t sure if forms persisted after they are replaced. I have been using the get_open_form().var method but its inelegant and was curious if something similar could be done more locally (at each form level)
If there is a reason to preserve the form and the values you need to preserve are only used by that form, then by keeping a reference to the form variable, you do preserve the variables.
If there is no other reason to preserve the form, or if those values are used by other forms, then it’s better to use a module. Here is an example:
You also preserve the state (visual and otherwise) of the components on that form. Depending on the circumstances, that might be a reason to preserve it, or a reason to re-create it from scratch.