Loading time too long

So my app is quite long and complex and it takes a long time to load various things. Here’s an example of one of them

def button_1_click(self, **event_args):
    if self.button_1.text=='Follow':
       self.button_1.text='Following'
       self.button_1.icon='fa:check'
       self.button_1.background='#007bff'
       user = anvil.users.get_user()['username']
       li=app_tables.users.get(username=Module1.author)['Followers']
       li.append(user)
       li=app_tables.users.get(username=Module1.author)['Followers']=li
       ui=app_tables.users.get(username=user)['Following']
       ui.append(Module1.author)
       ui=app_tables.users.get(username=user)['Following']=ui
       no=app_tables.users.get(username=Module1.author)['Notifications']
       no.append([user,'started following','you'])
       print(no)
       app_tables.users.get(username=Module1.author)['Notifications']=no
       open_form('Profile')
       
    else:
      self.button_1.text='Follow'
      self.button_1.icon='fa:user-plus'
      self.button_1.background='red'
      user = anvil.users.get_user()['username']
      li=app_tables.users.get(username=Module1.author)['Followers']
      li.remove(user)
      app_tables.users.get(username=Module1.author)['Followers']=li
      ui=app_tables.users.get(username=user)['Following']
      ui.remove(user)
      print(ui)
      app_tables.users.get(username=user)['Following']=ui
      open_form('Profile')
      

I know there’s a server module but I’m confused about how I can use it with codes such as this.

Also, I’m still on the free plan. If the loading time is too long only because of this, it’s completely okay with me as I intend to upgrade my plan as per the needs once my app is ready.

You should absolutely be using server functions to consolidate the data table accesses. Each time that you do something like this on the client:

app_tables.users.get(username=Module1.author)

It’s making a call out to the server. Each interaction with the server has a certain overhead dealing with roundtrip transmission time, serialization and deserialization time, etc. You want to have that overhead happen as few times as possible (ideally once to setup a form). You’ve got it happening a lot.

Also, having data tables be readable from the client is a huge security risk. The users table has hashed passwords in it, and by making it readable from the client anyone can download the entire users table. Limiting access to the server side prevents that.

For your code you’d have a single server function that returns all the data table results in a dictionary, and then use those results on the client.

If you want to continue the way you’re doing it now for simplicity for right now, at least cache the results of the same data table access. You’re interacting with the server to fetch the same user 4 separate times. Do something like:

author = app_tables.users.get(username=Module1.author)

And then use the author variable in the 4 places you need that result.

4 Likes

Okay, I’ll try that. Also, will upgrading plans increase the speed further?

Upgrading plans gives you access to more Python libraries, to more data table storage, etc, but should not affect the overall speed. As far as I know there’s nothing in the free plan that artificially limits the speed at which your app runs.

I understand the at if you upgrade to the business plan you can run the server persistently which means you don’t have the over head of booting the server up each call.

If you go further to dedicated plan the app has doer the access to the Postgres database which speeds things up again. Obviously hosting the app closer to your user base will save on some of the geographical overhead.

All this is paraphrased from an Anvil support email chain but I think I have the details right.

2 Likes

There was mention of low traffic so I assumed that it was also possible for loading speed to be slow