How to use session_state to update dataframe?

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

You have a typo: if “click” not in session_sate: should be session_state.

I would also expect something like:
If st.button(text):
st.session_state.click = True

At the end: st.session_state = False should probably be st.session_state.click = False

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

so what is the problem ??

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.

hope that this will solve the misunderstanding.

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?

Your code works for me after fixing the typos and connecting to a suitable database. The problem must be somewhere else.

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.

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