I am on a hobby plan, and I noticed the server takes a bit of time to run. It’s slow. Is there something I can do on the code side, or i must upgraded my plan to speed things up?
What I noticed is slow is the routing aspect of the app. When you click to get to the search page, it takes 30-40 seconds to load.
Here is how i set up the routing.
@anvil.server.route("/search")
def my_page(**p):
return anvil.server.FormResponse("Search_By_Zipcode")
Here is what Is on client side
class Search_By_Zipcode(Search_By_ZipcodeTemplate):
def __init__(self, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)
# Any code you write here will run before the form opens.
self.state = ["CA", "AZ"]
map_html = anvil.server.call('showmap',self.state)
self.html = map_html
def button_check_coverage_click(self, **event_args):
"""This method is called when the button is clicked"""
self.label_results.text = ' '
zipcode = self.text_box_zipcode.text
try:
# Perform a direct equality check in the search
row = app_tables.zipcodes.get(Zipcodes=zipcode)
row_zip = row['Zipcodes']
if zipcode == row_zip:
self.label_results.text = "We cover Area"
print(row_zip)
else:
self.label_results.text = " Unfortunately, we do not have someone for this zipcode. We will be expanding in your area in the next 3-6 months. "
print(row_zip)
except Exception as e:
self.label_results.text = "Unfortunately, we do not have someone for this area. Please try again later."
print(f"Error: {e}") # Log the error for debugging purposes
Clone App
I have added a few prints here and there, tested with both my dedicated and free accounts, and I have noticed that:
- In my computer both starting the app from the home page and from the
/search
route and showing the completed form takes about 6 seconds with the dedicated account, 18 seconds with the free one
- The time required for the
showmap
to do its job is 0.4 seconds with the dedicated account, 4 seconds with the free one
showmap
returns 4MB to the client and it takes about 5 seconds with the dedicated, 13 with the free one
A server call returning 4MB is a very heavy one. It is possible that Anvil throttles resources for the lower level plans when they are abused, and perhaps this large payload passes the abuse threshold for your plan.
I don’t know what kind of improvement upgrading your plan will give you. I have a dedicated plan, that is I have my own server and I’m not sharing it with anyone else. As far as I know higher level plans below the dedicated level still share the resources across multiple users, I assume they simply change the definition of abuse or the amount of throttling.
1 Like
Is there any way I can improve this with code?
A server call returning 4MB is a very heavy one. It is possible that Anvil throttles resources for the lower level plans when they are *abused* , and perhaps this large payload passes the *abuse* threshold for your plan.
Or do I just upgrade to business plan? Maybe someone from Anvil can define what is abuse and how they throttle accounts? I’m curious
Since you’re using Plotly, you can return the figure instead of the HTML. On my timings that takes the time for the showmap
server call from 5 seconds to 1 second (roughly). Then assign the figure to a plot component on the client:
@anvil.server.callable
def showmap(added_state):
state = added_state
fig = px.choropleth(locations=state, locationmode="USA-states", color_continuous_scale=[(0, 'yellow'), (0.5, 'orange'), (1, 'red')], scope="usa")
fig.update_layout(title_text="Coverage Map")
return fig
class Search_By_Zipcode(Search_By_ZipcodeTemplate):
def __init__(self, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)
# Any code you write here will run before the form opens.
self.state = ["CA", "AZ"]
map_fig = anvil.server.call('showmap',self.state)
self.plot_1.figure = map_fig
Since I’m also not seeing the large amount of time you’re seeing, that may or may not have an impact on your situation.
1 Like
This sped things up by a noticeable amount. Can you share you figured this out? I never used plot_1.figure ever. This is just so I can understand how this worked way better than what I did.
Of course if you can point me to the documentation as well, that would be great.
I can’t take any credit for that technique, I’ve just seen it on the forum various times when people have asked Plotly related questions. Plotting on the server and passing the figure back seems to be a common answer to a bunch of Plotly issues.
2 Likes