To store a list of filters that looks like this (typically btwn 2-10 elements - very short lists):
['filter1', 'filter3', 'filter4']
The call to assign this to the session state is taking >200ms. For so little data, that’s a ton of time and is impacting user experience. (For reference, 200ms is about 25% of total time per page load - the rest is due to an external API call to my own service running in us-east-1.)
Anything I can do to improve the performance? Is there another Anvil tool that’s more appropriate for storing this kind of data during a session?
And a Button that calls that function with a short list:
def button_1_click(self, **event_args):
"""This method is called when the button is clicked"""
start = datetime.now()
anvil.server.call('set_active_filters', ['filter1', 'filter3', 'filter4'])
print((datetime.now() - start).total_seconds())
In the Restricted Server Module environment, the print within set_active_filters reports 8ms. The print within the Button click handler reports 170ms for the first call, and ~100ms for subsequent calls (the first call takes longer because it’s establishing the websocket connection). In the Full Python environments, the set_active_filters takes 11 microseconds.
This is incredibly helpful, thank you for the thorough reply! Given the round trip from California to us-west-2 (I found your AWS region in this older post), 200ms is actually pretty fantastic performance.
Storing the filters client-side should theoretically work just as well for my purposes. I hadn’t fully realized the difference between Modules and Server Modules, but your example illustrates it well. I’m going to try to implement the functionality with a Globals module in my project.
One follow up question - can I store functions that call my own API in the client-side Globals module too? Here’s an example function that presently lives in a Server Module that I’d like to move:
I put them in the Server Module following the example in the docs, but I could save a ton of time if these functions live client-side, right? Are there any limitations to client-side Module capabilities that I need to be aware of?
It sounds like your API is entirely under your control, so you should be able to configure it to accept requests from anvil.app (or your custom domain if you’re using a custom domain).
In fact, the procedure for building CORS-able HTTP endpoints in Anvil has improved substantially in the last couple of weeks! I’ve added an update to that forum thread…
Thanks very much, @meredydd, @david.wylie and @shaun. This is exactly the info I needed to get the job done right. Great timing too since you’ve just made improvements! I really appreciate your help.
@david.wylie - The API key is stored as a string in the module for development, but when I push this live, I’m planning to add the Users service and will store the API key in a data_table with a foreign key to user.
@shaun - yes, our API is entirely under my control. It’s used as an interface to our backend database which is not publicly accessible. I hadn’t considered using an App Secret, and I’ll look into it for sure - thanks for the tip!
Beyond that, it’s okay if the API key is in plain text in the browser’s memory, it’s used to throttle/limit requests and determine which datasets to return, but security will be managed by the Users service.
Thanks again! Let me know if anything I’ve said sets off alarm bells for you.