Pushing st.data_editor into postgres database?

Below is a simple way for streamlit to display a table from a postgres database. However, I am having issues finding a way to push the edited data in edited_df back into the postgres table.

Additionally, even though it should be possible to push the edited_df back into the table, is there a way to safely do this with ~7 people on the app updating different parts of the table?

import streamlit as st

conn = st.connection("postgresql", type="sql")

df = conn.query('SELECT * FROM "table1";')

edited_df = st.data_editor(df)

Hey @jstp,

Thanks for sharing this question! If you set a key for st.data_editor, any changes made to your dataframe will be stored in Session State – here’s an example of how to access edited data using Session State:

st.data_editor(df, key="my_key", num_rows="dynamic") # 👈 Set a key
st.write("Here's the value in Session State:")
st.write(st.session_state["my_key"]) # 👈 Show the value in Session State

(Docs link)

Is the concern with concurrency that users would be updating the table without seeing updates recently made by other users? Or is it more about avoiding having multiple users writing to your database at the exact same time?

Hi @jstp

And if you’re looking to push the updates back to the database, you can see this thread for some ideas:

1 Like

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