Search code examples
pythonstreamlit

How to access the variable inside a button in a Streamlit frame work?


I have three variables, days, date and name; days is a slider which takes only integer value, date is a date picker and name is a string variable.

There is a function called some_function() that takes these three variables and returns result as output. I am using Streamlit framework and I have defined a button called Submit, once the Submit button is clicked, the function is invoked and returns result.

My problem is that I can access the output of the function within the button but I need to access this output outside the button as well for further calculation.

Any help will be much appreciated.

days = st.slider("No of days", min_value=1, max_value=15, value=1)
date = st.date_input("Select a date: ")
today = datetime.today().date()
name = st.text_input("Enter the name of the country: ")

if st.button("Submit"):
    if date < today:
        st.error("Don't enter past date")
    else:
        result = some_function(name, days, date)
        st.write(result)

Solution

  • You can use it this way:

    days = st.slider("No of days", min_value=1, max_value=15, value=1)
    date = st.date_input("Select a date: ")
    today = datetime.today().date()
    name = st.text_input("Enter the name of the country: ")
    result = ""
    
    if st.button("Submit"):
        if date < today:
            st.error("Don't enter past date")
        else:
            result = some_function(name, days, date)
            st.write(result)
    print(result)