What I’m trying to do:
I’m tring to optimize the speed of populating a RepeatingPanel with a large number of items. Specifically, I am trying to optimize the code in the __init__ of the item template.
What I’ve tried and what’s not working:
I have the following form called “item” that is used as the item_template in a repeating panel:
from ._anvil_designer import itemTemplate
from anvil import *
from anvil_extras.storage import local_storage
class item(itemTemplate):
def __init__(self, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)
pref_language = local_storage['preferred_language']
item_name = self.item['titles'][pref_language]
# rest of code
...
Accessing the local_storage (pref_language = local_storage['preferred_language']
) takes a substantial amount of time when assigning a large number of items to the repeater (as it is accessed once for each item)
I’ve tried to modify the code by moving pref_language = local_storage['preferred_language']
before the class declaration, in the following manner:
from ._anvil_designer import itemTemplate
from anvil import *
from anvil_extras.storage import local_storage
pref_language = local_storage['preferred_language']
class item(itemTemplate):
def __init__(self, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)
item_name = self.item['titles'][pref_language]
# rest of code
...
The problem is that the code before the class declaration is executed only on app startup. Hence, any changes to local_storage['preferred_language']
would not be reflected to pref_language
within the same session.
What I want to do is add pref_language
as a parameter to the __init__ so that the local_storage does not have to be accessed for each item:
from ._anvil_designer import itemTemplate
from anvil import *
from anvil_extras.storage import local_storage
class item(itemTemplate):
def __init__(self, pref_language:str, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)
item_name = self.item['titles'][pref_language]
# rest of code
...
The problem I am facing is - with a RepeatingPanel I don’t call the item_template’s form constructor directly, it is automatically called N times whenever the .items property changes.
At what point (and how) can I pass an argument to the pref_language
parameter of the constructor? Or could I maybe do something with data bindings?
Any ideas or pointers would be much appreciated!