user8
November 23, 2021, 12:49pm
1
What I’m trying to do:
I would like to get a group of rows from the data table that match the users selected ‘project’ (my data table has a project column that I am using to filter the search results)
What I would like to do is sort the rows with another column so that the rows are in order.
What I’ve tried and what’s not working:
For filtering:
df = app_tables.my_datatable.search(Project= project)
Which is returning a <LiveObject: anvil.tables.SearchIterator>
I was thinking it would return a list of dictionaries that I use to sort (based on the docs)
Using Data Tables from Python Anvil represents Data Tables in code as Python objects. You can imagine a Data Table in Python as a list of dictionaries, although they have many extra methods as described on this page, and they have caching and lazy...
For sorting, I have tried:
df_ordered = sorted(df, key= lambda c: c['row'])
I am aware of the order_by() function but don’t know how to combine it with search().
You can use this to return list of dics:
On the server you can do:
rows = list(dict(r) for r in app_tables.my_table.search())
on the client (at present) you will get a suspension error - so do
rows = list(dict(list(r)) for r in app_tables.my_table.search())
1 Like
user8
November 23, 2021, 1:33pm
3
Thank you so much Tony, that solved it.
The order_by has to be before the keyword parameters for search, e.g.:
df = app_tables.my_datatable.search(tables.order_by('row'), Project= project)
2 Likes
user8
November 23, 2021, 2:26pm
5
Hey Jay,
I was hoping there was a one liner to do what I was looking for, I just didn’t know how to structure it and ended up looking other methods.
my first attempt was:
df = app_tables.my_datatable.search(Project= project).tables.order_by('row')
which didn’t work…
Thank you for clarifying that.