i have streamlit app connected with database where user can update values in the dataframe.
The problem is that once the user update values the values are updated in the database but the displayed dataframe shows the old values and not the updated one.
I tried to use session_state but it did not work as it should.
so where is my error in the code below.
code:
import streamlit as st.
import pandas as pd
def main():
df = read_sql_query("""
select * from testdb
""",con)
text = "out session"
if "click" not in session_sate:
st.session_state.click =False
st.write(df)
else:
if st.session_state.click = False:
text = "IN session"
st.session_state.click =True
# here it suppose to display the updated data after the user update values.
st.write(df)
else:
text = "OUT session"
st.session_state = False
st.write(df)
st.button(text)
the Button text is changed each time the app run means on each event the button text changed.
So it seams that i am not understanding how and where to use the session_state
yes you are right this was a typo error while i was creating this post but i the app code its written in the correct way session_state. st.session_state.click = False
Can you explain exactly what the code is supposed to be doing?
Here’s my attempt to get a fully functional example, and I don’t see any obvious issues, but I’m probably not understanding exactly how it’s supposed to function.
import pandas as pd
import streamlit as st
# Make dummy function
def read_sql_query(sql_query, con):
return pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
con = None
def main():
df = read_sql_query(
"""
select * from testdb
""",
con,
)
text = "out session"
if "click" not in st.session_state:
st.session_state.click = False
st.write(df)
else:
if st.session_state.click == False:
text = "IN session"
st.session_state.click = True
df["c"] = df["a"] + df["b"]
# here it suppose to display the updated data after the user update values.
st.write(df)
else:
text = "OUT session"
st.session_state.click = False
st.write(df)
st.button(text)
if __name__ == "__main__":
main()
sorry for the confusing question but what i am asking is once i update the data in the database i want to display the updated data and not the original data (old data).
so once the user click on the button first the text and the displayed dataframe will refresh and display the updated data.
Would it be possible to move the code that reads from the database into a function, then when the button is pressed, call that function again so that it gets whatever the current version is in the database?
You can use st.experimental_rerun(), a new feature I discovered the past month. It will rerun all your code, re-connecting to the database and extracting the updated values.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.