Hi there, this is my first post here, so have mercy! I was having a problem in which I don’t know if the solution that I’ve reached could be considered as a best practice or not.
Disclaimer: English is not my native language, so sorry for unwanted silly mistakes…
The problem: Combine data from three tables with different columns to populate a single repeating panel. This is more explicitly:
- Having three tables (T1, T2 and T3) with columns:
- A, B, C, D for T1
- A, B, C for T2 and
- A, B, C, E for T3
- I need to generate a table like:
A | B | C |
row 1 ...
row 2 ...
...
The Question: Is my solution in the end a best practice? (You can jump to Attempt 3 if you want to see the result)
Attempt 1 - First error
Following the logic for populating a repeating panel I make the following
server side
def compile_tables():
table_t1 = app_tables.table_one.search(something)
table_t2 = app_tables.table_two.search(something)
table_t3 = app_tables.table_three.search(something)
return [table_t1, table_t2, table_t3]
client side
tables= anvil.server.call(compile_tables)
self.repeating_elements.items = tables
This won’t work, it simply populates three empty rows and I think that populating the table three times, so worthless and inefficient.
Attempt 2: Works but really slow
server side
def compile_tables():
table_t1 = app_tables.table_one.search(something)
table_t2 = app_tables.table_two.search(something)
table_t3 = app_tables.table_three.search(something)
data= {'table_t1': table_t1,
'table_t2': table_t2, ... etc }
return data
client side
tables= anvil.server.call(compile_tables)
# Populating table
self.repeating_elements.items = []
for k, v in tables.items():
if v:
self.repeating_elements.items += v
This works but as I said, it was slow (Maybe because my speed is a bit slow here in Argentina during this COVID days due to the quarantine)
Attempt 3 - Final result
server side
import itertools
def compile_tables():
table_t1 = app_tables.table_one.search(something)
table_t2 = app_tables.table_two.search(something)
table_t3 = app_tables.table_three.search(something)
combine_1= itertools.chain(table_t1, table_t2)
combine_2= itertools.chain(combine_1, table_t3) # Maybe here I could've
# combined in one line but the idea is the same
result = [x for x in combine_2] # Returning combine_2 raise a Serialization
# error
return result
client side
tables= anvil.server.call(compile_tables)
# Populating table
self.repeating_elements.items = table
Which was like 5s faster than the previous attempt and much more cleaner on the client side
PS… If this could not be well understood then I will try to write a small app to show this then. But thanks anyway for taking time reading it!