Get the values from several particular columns in Data Table

Hi,

It is possible to get values from ONE particular column, however it is not clear how to get values from several ones. Any help is greatly appreciated.

names = [r['name'] for r in app_tables.people.search()]

https://anvil.works/docs/data-tables/data-tables-in-code

Adapted from an actual application:

    result = [
        {
            'user_descr':       row["user_descr"],
            'report_type_name': row["report_type_name"],
            'has_csv_data':     row["has_csv_data"],
        }
        for row in app_tables.reports.search()
    ]

Result is a list of dicts, compatible with Anvilā€™s items.

3 Likes

Awesome, itā€™s such an elegant code. Thanks so much @p.colbert

You might also upvote this feature request: Client-readable views on a subset of columns

The feature request would allow us to select individual columns to be returned from a search. This would prevent the need to do a list comprehension over the entire results.

1 Like

You could also have a look at my serialiser:

1 Like

Thank @jshaffstall and @owen.campbell for the suggestion, I will definitely check it out.

Yes, nice solution. This actually helped me out when stuck on returning some rows for a few hours, so it is in my code now. Thank you.

I was wondering if using this query makes multiple calls to the server?

Iā€™m currently working on adding that serialiser to anvil-extras. Almost thereā€¦

1 Like

If you run this code on the client, Anvil will do its best trying to load chunks of database rather than a little bit at a time.

For example it could decide to read 10 rows on each round trip leaving behind the simple object columns to save bandwidth. This could be good for you, but if you need simple object values or you need the 11th row, you would trigger more round trips.

I never return rows to the client for this reason, because I donā€™t have control over the number of round trips. I always do my dictionary comprehensions on the server side and return a list of dictionaries to the client, so Iā€™m sure I do only one round trip which includes all and only the data I need. Now the problem is that I return a list, not a generator, so I need to return all the rows I need in one round trip.

This feature request addresses this very problem, you can upvote it.