How to reset output's interaction

Hi,

Let’s suppose that we have the equation Y = A.X

The user can give the value he wants for A and X by a Streamlit interaction, and then he wants to see the value of Y by selecting a checkbox. We can also assume that a graph of this function is generated afterwards.

Now the purpose is, at the end of the utilization, to reset :

  • the output of the checkbox (the value of Y given by the values of A and X the user selected),
  • and the graph.

How can I implement this reset in my code ?

Thank you

One way to implement the reset is to use session_state to set the default values for the inputs, and then delete those entries in session state when you want to reset the inputs

Here’s one way you might do it:

import streamlit as st

if "a" not in st.session_state:
    st.session_state["a"] = 1

if "x" not in st.session_state:
    st.session_state["x"] = 1

a = st.number_input("a", 0, 100, key="a")
x = st.number_input("x", 0, 100, key="x")

y = a * x

if st.checkbox("Show y"):
    st.write(f"{y=}")

if st.button("Reset"):
    del st.session_state["a"]
    del st.session_state["x"]
    st.experimental_rerun()

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