I need to reformat data retrieved from a table before sending it back to the client for display in a repeating panel. I haven’t found a worked example in the docs or tutorials that does this though, and I’m not sure I understand how to create the dictionaries in the correct format.
Hi there. Just in case it helps, you can return the row objects to the client; however, to reduce server call overhead, you can return a list of dicts instead. You would, as usual, have to bind those to your UI properties when displaying of course.
Here is an example of making a list of dicts. The linked posts have helpful info too.
Thanks for the link. That gives me a better understanding, but for my application I’d like to reformat some of the fields as the rows / dicts are being built. For example, converting a datetime field to a text string. I know how write the reformatting code, just not how to selectively do it to certain dict fields like in the rows = [dict(x) for x in rows] example.
In general, I return the rows to the client and do the formatting on the client side in the data bindings. But, if you want to do that on the server and return a list of dicts, you’ve got the basic structure of it already. Here’s an example from my project.
Hi @alcampopiano
I was reading your answer and I take the opportunity to ask and learn something new.
You say:
you can return the row objects to the client; however, to reduce server call overhead, you can return a list of dicts instead
I thought passing the rows object, being an iterator, prevents from fetching all the rows at once hence reducing the call overhead. Then anvil Datagrid component handles nicely the pagination and everything.
Passing a list of dicts might be lighter for searches returning few rows, but not if the search returns a lot of rows, isnt’it?
Hello. There is no “one size fits all” solution. There are trade offs to consider (speed of running app, speed of prototyping, etc…). The linked posts in my comment lead to many posts that describe the trade offs between using the row objects vs returning a list of dicts.
Of course, just because you return a list of dicts, doesn’t mean you have to return all rows at once. You will always have the power to roll up your own pagination and/or limit the number of returned rows. There are pros and cons to each approach and it is worth understanding the linked posts so that you can choose the right option for you.