Hi everyone, My name is Soumitra Dutta, an Oxford-based entrepreneur & photographer.
I’m working on passing data between forms and server functions in Anvil, and I want to make sure I’m doing it the right way. How do you usually handle sending data from one form to another and then to the server? Do you rely more on parameters, shared state, or Data Tables?
Would really appreciate hearing your approach or any best practices you’ve found helpful.
Regards
Soumitra Dutta
Shared state is the trickiest (both to achieve, but especially to make sure it is actually happening)
Parameters when you are using open_form or add_component is a good option, but that means you are keeping the same URL path.
Data Tables are good if you want the data to last a long-time and/or use server routing.
If you have specific questions, you can post those and/or look on the forum as these topics have gotten a lot of traction in the past 
1 Like
For client-side communication between forms, a common approach is to use a shared Globals module that is imported by all forms. Any variables defined there can be accessed and modified across forms, making them effectively global within the client.
For communication between a form and the server, the usual pattern is to call a server function (anvil.server.call) and pass the required data as arguments. The server processes the request and returns a result, which becomes available back on the client side.
There are other approaches, but this is the one I typically use.
3 Likes
I generally follow Stefano’s pattern, with one exception: when the data are short-lived, I generally pass them as parameters.
Between Forms, that usually means passing them to a Form’s constructor, to one of its member functions, or to one of its event-handlers.
1 Like
I also do something similar to what @p.colbert mentioned for small amounts of data, but in general I stick to routing as my main pattern.
I don’t open one form directly from another. I rely on routing and let it handle passing data through the URL.
For example, if I’m on a project summary page and want to navigate to another page related to the same project, I don’t instantiate the form. I route to that page and include the project ID in the URL.
Each form is designed to load its data from the URL rather than from constructor arguments, so the same setup works both for navigation within the app and when opening a page directly.
1 Like