Get help from the marimo community

Updated 3 months ago

mo.ui.altair_chart(chart) is not updating based on filtered data

At a glance

The community member is creating a simple Altair bar chart and wants to update it based on filtered data, but the chart is not getting updated. The comments suggest that the code is mutating the bar chart only in the function and not actually updating the global bar chart. The community members recommend removing the update_chart function and letting everything run reactively, as well as declaring the bar chart only once and having it depend on all the filters. One community member suggests removing the callback and letting Marimo handle updating the affected cells based on changing UI elements. Another community member has refactored the code as suggested but is now getting an error related to the filtered_data variable being defined in another cell. The community members suggest renaming the variable to resolve the issue.

I am creating a simple Altair bar chart, want to update it based on a filtered data but it is not getting updated. here is the flow of my code:
S
A
M
9 comments
can anyone please help me witht this
Can you include a minimal example? This notebook is rather long
Please find the reduced version of above code here.
the code is mutating bar_chart only in the function, and not actually updating the global bar_chart.

i would remove the update_chart function and instead let everything run reactively
it would reduce the amount of code too. you should only declare once:
Plain Text
 bar_chart = alt.Chart(df_agg).mark_bar(size=30).encode(

and still have it depend on all of your filters
i won't be able to refactor your code, but you should remove the callback and instead let marimo handle updating the affected cells based on changing ui elements
Hi @Myles Scolnick Thank you for the suggestion. I have now refactored the code as suggested by you but now I am getting "The variable 'filtered_data' was defined by another cell:"
when I do the filteration based on the date range e.g. filtered_data = filtered_data[
(filtered_data["request_date"] >= end_date_str)
& (filtered_data["request_date"] <= start_date_str)
]
you need to rename the variable
Add a reply
Sign up and join the conversation on Discord