Filtering data based on drop down selection

I have done it with a tuple because each year will match with different 18 values for my 18 cities.
the first part of your code will display on my dropdown the years correctly! thanks!
But do the app knows which year corresponds to each value correctly? without doing it with a tuple?
Like my chart code is this:

   def build_revenue_graph(self):
    db_data = anvil.server.call('get_iap')
    self.plot_1.data = go.Bar(x = [x['Cidade'] for x in db_data],
                            y = [x['IAP'] for x in db_data],
                            marker=dict(color='#2196f3'))
    self.plot_1.layout.title = "ÍNDICE ARROWPLUS"     

So x axis displays my cities and y axis displays my IAP values.
How can i change this so the chart change values accordingly to the chosen year on the dropdown?

You could filter the data for the selected year. One way to do this would be to add the currently selected year as a parameter to your “get_iap” server function, and in that function, filter the rows you retrieve from the data table.
An alternative would be to retrieve all of the data and build a dictionary of tuples, keyed by year.

My get_iap server function:

def get_iap():
  return app_tables.iap_table.search()

how can i select year in this function to display in chart? i mean i need to choose the year in the dropdown…

You cannot select the year from within a server function, you would have to pass that in as a parameter.
Scrap my previous suggestion. You could add a filter to your list comprehensions to restrict the plot data to whatever year is selected in the drop down.

@tiago.mendes Please see these guidelines on asking a good question:

Asking “how do I do X?” without showing what you’ve tried is not a great approach on the forum.

As a general suggestion since you are just getting started, you may want to go through the introductory tutorials as they will demonstrate how to pass parameters to a server function, among many other common things (dealing with events, etc). In addition, you may want to Google how to filter a list and/or tuple since that may be one viable approach here.

Feel free to post back here when you can show what you’ve tried with some small code examples. This will help others to jump in and help more easily.

@alcampopiano i know that, the question i asked in this post i posted code, i wrote what i did and i even added 2 screenshots! After that is just comments of people trying to help me and i reply back most of the times with code, its not a new question, we are discussing the same question i posted before correctly…
I just need to get that correclty and the question is answered!

No problem. I was referring to the post that was deleted above and the fact that there are some helpful leads here to go on IMO.

1 Like

@ahawes so, i know that to implement something like that i need to get the dropdown selected value like this

    `  year = self.start_year.selected_value`

My code for the chart is this:

def build_revenue_graph(self):
    db_data = anvil.server.call('get_iap')
    self.plot_1.data = go.Bar(x = [x['Cidade'] for x in db_data],
                            y = [x['IAP'] for x in db_data],
                            marker=dict(color='#2196f3'))
    self.plot_1.layout.title = "ÍNDICE ARROWPLUS"

What i need is to know how to filter the chart values by the year i select… in terms of code i dont know how to do that…

Have you tried passing the selected value to your server function, and then using that value as a filter for what is returned from the database?

For example, my_data=app_tables.my_table.search(my_column=foo)

That kind of thing is all over the intro tutorials and documentation. This was suggested already by @ahawes.

If you want client side filtering, as also suggested above, have you tried Googling how to filter a list in Python?

Hello
so that part of the code i have it on server module

def get_iap():
  return app_tables.iap_table.search()

In the search parameters i have to pass my year varialble which is the selected alue from the dropdown but i dont know how to pass that variable which is on client side code to server module function.
Or can i do it all on client side? and if i can, what can i change on my code?
def build_iap_graph(self):
year = self.start_year.selected_value

    db_data = anvil.server.call('get_iap')
    
    
    
    self.plot_1.data = go.Bar(x = [x['Cidade'] for x in db_data],
                            y = [x['IAP'] for x in db_data],
                            marker=dict(color='#2196f3'))
    self.plot_1.layout.title = "ÍNDICE ARROWPLUS"

For filtering on the client side, you can add an if clause to each of your x and y list comprehensions.

[x['Cidade'] for x in db_data if x['Ano'] == year]
[x['IAP'] for x in db_data if x['Ano'] == year]

Hope that helps

1 Like

@ahawes it should work like this right?

def build_iap_graph(self):
    
    year = self.start_year.selected_value
    
    db_data = anvil.server.call('get_iap')
    
    self.plot_1.data = go.Bar(x = [x['Cidade'] for x in db_data if x['Ano'] == year],
                              y = [x['IAP'] for x in db_data if x['Ano'] == year],
                              marker=dict(color='#2196f3'))
    self.plot_1.layout.title = "ÍNDICE ARROWPLUS"

but it only displays the first value on the dropdown when i run the app(which is the year 2011), do i need to do something else to display the other years when selected?
My dropdown code is this:
self.start_year.items = set(row['Ano'] for row in app_tables.iap_table.search())

See @stucork example in the earlier thread regarding handling the dropdown change event

@ahawes so i did this function:

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

and then on my chart function i added this:

year = self.start_year_change()

is this the right way to do it? something is missing though because the chart still does not change when i select a different year…

It looks like you’ve got mixed up with what code calls other code. You wouldn’t normally call event handlers from your custom logic. Event handlers are triggered by user interaction, timers, etc.
The change event should call your chart building function, not the other way around.
I would recommend going through the tutorials (possibly again) to get a better understanding.

1 Like

@ahawes Thank you for your help, i am almost at the solution!
I did read the tutorials but the examples they give are different than what i need you know? so i just need a little bit more help and i will be done :smiley:

So, as you said, the change event should call my chart building function like this right?

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

and now i dont use the “year” variable on the chart building function since its here, and i get this error:

NameError: name ‘year’ is not defined

In the init functions i deleted the one that builds the chart and added this one, the start_year_change function, is this correct?
If you need more information i post here my app so you can clone:

Thank you for all the help!

You are getting the NameError because the year variable only exists locally within the start_year_change event. Try adding a year parameter to your build_iap_graph function and then pass that in when you call it.

1 Like