Different values on dropdown

Hello, i am building a web app and i am having trouble on building a dropdown to show different values from a table.
For example, i have a table with 3 columns: city names, IAP(which is a value like 1.11 that changes for every city) and year column. So each city has a value for every year. I have 18 cities with 18 different values for 2011, the same 18 cities with 18 different values for 2012 etc etc. The value i want to display on the dropdown is all the years from 2011 to 2020.
My code right now for the dropdown is
self.start_year.items = [(row[“Ano”], row[‘IAP’]) for row in app_tables.iap_table.search()]
(“Ano” is year in portuguese)
But this code displays values on my dropdown like this:


Instead of showing 2011 eighteen times like that i want to display it only once, and 2012 only once and so on.
The final goal is to pick a year in the dropdown in order to show data on the chart on the right side of that screenshot.
Can anybody help me do that? i am new to Anvil but every detail you need to further help me i can try to share.
Thanks! :smiley:

My data table is something like this:

Try using Python “sets”, they can reduce a collection to unique values. Then use those unique values to populate your drop down menu.

2 Likes

I know what sets are on python. But in this case on anvil where can i implement this and can i use sets instead of something i have? Im new with anvil so i need a little bit more “light” on this matter

for drop downs you can provide any iterable of strings or, like you’ve done, an iterable of tuples where each tuple is a key, value pair.

the following code will give you just the years in the dropdown items

self.start_year.items = set(row['Ano'] for row in app_tables.iap_table.search())

You’d then want to handle the change event to display the graph correctly

def start_year_change(self, **event_args):
  year = self.start_year.selected_value
  ...

1 Like

10 posts were split to a new topic: Filtering data based on drop down selection