How can I store data into variables?

Summary

I think I did not understand the mechanism about streamlit

  1. what variables is maintained after I clicked even a buttons ?
  2. Do I need to read data from database every time ?

Steps to reproduce

Code snippet:

@st.cache_data
def search_exe_version(selected_timestamp):
    cur = conn.cursor()
    sql_sytax = f'select * from fm."easy_exeVersion" where "timestamp" between {selected_timestamp - 5 * 60 * 1000} and {selected_timestamp + 5 * 60 * 1000}'
    cur.execute(sql_sytax)
    rows = cur.fetchall()
    df = pd.DataFrame(rows)
    cur.close()
    return df


# Define the home page
def home():
    global df_exeVersion
    st.title("get Balance @ timestamp")

    # Set the default date and time
    default_date = datetime.now().date()
    default_time = datetime(2023, 1, 1, 0, 0, 0).time()

    # Display the date and time input widgets
    selected_date = st.date_input('Select a date', default_date)
    selected_time = st.time_input('Select a time', default_time)

    # Combine the selected date and time
    selected_datetime = datetime.combine(selected_date, selected_time)

    utc_tz = pytz.UTC
    selected_datetime = utc_tz.localize(selected_datetime)
    selected_timestamp = int(selected_datetime.timestamp() * 1000)

    # Display the selected date and time
    st.write('You selected (in UTC) :', selected_datetime.strftime('%Y-%m-%d %H:%M:%S'))
    st.write(f'And its timestamp(ms) is {selected_timestamp}')

    # exe_version_list = []
    if st.button('Search exeVersion'):
        st.write('Search the exe version around the timestamp (+- 5min)')
        df_exeVersion = search_exe_version(selected_timestamp)


    st.write(df_exeVersion)
    exe_version_list = df_exeVersion[0].unique()

    selected_exe_version = st.selectbox('Select exeVersion', exe_version_list)
    st.write(f'Selected exeVersion: {selected_exe_version}')


# Run the app
if __name__ == '__main__':
    home()

Hi @Jason_Lee1 ! Welcome to the community!

If you would like to store variable after interaction you should use session state. Details and tutorial: Add statefulness to apps - Streamlit Docs

1 Like

thanks for reply
I will try it

1question is in streamlit application the datas in variables from stream library is stored? ( means can use it after next event ?)

what is differences between two codes ?

As I understand the variables from st.instruments will be kept their values
after rerun the whole codes right ???

import streamlit as st

if "celsius" not in st.session_state:
    # set the initial default value of the slider widget
    st.session_state.celsius = 50.0

st.slider(
    "Temperature in Celsius",
    min_value=-100.0,
    max_value=100.0,
    key="celsius"
)

# This will get the value of the slider widget
st.write(st.session_state.celsius)
import streamlit as st


abcd = st.slider(
    "Temperature in Celsius",
    min_value=-100.0,
    max_value=100.0,
    value=50.0,
)

# This will get the value of the slider widget
st.write(abcd)

Hi @Jason_Lee1 streamlit always runs code from top to down, in your case you obviously get the same results as abcd has value :slight_smile:

For example:

import streamlit as st

a = 0

if st.button("this is my button"):
    a = a + 1

st.write(a)

in this case you won’t be able to get a to be a number larger than 1 becouse code re-runs every time from top to bottom. However, when you use session state:

import streamlit as st

if "a" not in st.session_state:
    st.session_state.a = 0

if st.button("this is my button"):
    st.session_state.a = st.session_state.a + 1

st.write(st.session_state.a)

value of a is retained betwen runs. Hope this helps!

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