Persistent Data

Anvil has been great so far, but I found an issue in displaying the data retrieved from databases.
Is there a way to have ‘persistent data’? Meaning, when a user goes to another form on the webpage, I want the data the user entered on the previous form to be saved.

Is this already implemented in anvil?

when using open_form to change the main form, just send the necessary data as arguments:

Also, it’s probably best to put these types of questions in Q&A first, and if it turns out not to exist, then it can be made a feature request.

1 Like

Welcome to the forum!

I moved the post to Q&A since it’s a question about existing features.

In addition to the technique that @duncan_richards12 posted, you can also use a client-side module to hold global data that’s available to all client-side code. Do some forum searches on it and you’ll see a variety of techniques.

2 Likes

A quick plug for the persistence module in anvil extras which do this all for you.

https://anvil-extras.readthedocs.io/en/latest/guides/modules/persistence.html

2 Likes

The Anvil extras persistence module is a good choice. So is passing the data to the next form as arguments especially is the data flow is linear and used only once.

If you are doing this regularly and on many forms I generally make a client side module and store dictionaries (or a list of dicts) there. That way you can just import the variable and do what you want with it.

2 Likes

I don’t understand the details of your implementation, but the way I “save” the data from the previous form is simply saving the previous form, which is an instance of a class and its content is stored in its attributes.

A form is a Python variable, and you can save a form just like you save any other Python variable, but I do it with the routing module from Anvil Extras. One of the goodies coming with the routing module is the (optional) caching of forms.

I can prevent the user from leaving a form without first clicking the Save button, I can always reload the data from the server when the form shows, or I can rely on the cashed instance.

2 Likes