Hi @stu,
So, there are two problems here:
- You’re doing a lot of nested searches, and they’re taking a long time
- You’re doing it all from inside the PDF renderer (which is a headless Chrome browser that’s a bit of a resource hog, so we don’t let it run longer than 30s)
We can address those issues separately. #2 will be the fastest, so let’s start there. You can precalculate the data before you call anvil.pdf.render_form()
, and then pass it as an argument to the form’s constructor - that way, you can calculate what you’re displaying on the server (which is faster). What’s more, you could calculate it in a Background Task (which gives you all the time in the world), and then call the PDF renderer once you’ve assembled your report.
This should get you up and running while you optimise Issue #1, which is a little more tricky, as it relates to your actual search logic. For starters, the search goes faster if you do it from the server (you’re closer to the database!), but also, this seems odd to me:
last_inspection = app_tables.inspection.search(tables.order_by("Date",ascending=False,Kit_Item=item_of_kit))
Did you mean Kit_Item=
to be an argument to tables.order_by()
rather than search()
? It’s possible you’re querying the whole inspection table, every time, for every item of kit, which could explain a bit of the slowness.