Adding Widgets With Button

I would like to know if there is a way to add widgets by clicking a button.
In my app, I would like to build charts.
I have a filter section to filter the dataframe before drawing the charts.
I have a function to render filter, but I would like to have a ADD FILTER button, which will call the same function to add the filters (without duplicate the widgets).
The idea is to add the filters values in query : df.query(filter1 & filter2 & filter3)…

Thank you !

Just keep a widget counter that increments each time the button is pressed. Then create as many widgets as the counter says.

if "num_filters" not in st.session_state:
    st.session_state["num_filters"] = 0

if st.button("Add filter"):
    st.session_state["num_filters"] += 1

for num in range(1, st.session_state["num_filters"] + 1):
    st.text_input(label=f"Filter #{num}")

It’s working. Great!
Thank you very much it’s very helpful

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.