Edit cell values and save changes back to table

I’m using a Snowflake Streamlit native app. I aim to show a table where users can edit cell values and save changes back to the Snowflake table. However, my current code overwrites the entire table when applying a filter and updating a cell value. I want the filter to only affect data filtering, while cell updates should be the only changes reflected in the table.

Can someone help pls. Thanks

import pandas as pd
import streamlit as st
from snowflake.snowpark.context import get_active_session
import time

session = get_active_session()

st.set_page_config(layout=“wide”, page_title=“Data Editor”, page_icon=“:abacus:”, initial_sidebar_state=“expanded”)

snowflake_table_name = “table”
df = session.table(snowflake_table_name)

st.sidebar.header(“Filters”)

Country = st.sidebar.selectbox(
label=“Filter Market”,
options=df.to_pandas()[“COUNTRY”].unique(), index=1
)

df_selection = df.filter(df[“COUNTRY”] == Country)

with st.form(“data_editor_form”):
st.caption(“Edit the dataframe below”)
edited = st.experimental_data_editor(df_selection, use_container_width=True, num_rows=“dynamic”)
submit_button = st.form_submit_button(“Submit”)

if submit_button:
try:
session.write_pandas(edited, “table”, overwrite=True, quote_identifiers=False)
st.success(“Table updated”)
time.sleep(5)
except:
st.warning(“Error updating table”)
st.experimental_rerun()

Can you format your code?

@s_Sadhees Were you able to find out what went wrong with your code? I have the same issue as well.

@hellokatechan - it seems that is the default behavior, and didn’t get any support from the community - I follow a different approach now. I have created a dropdown with create, updated records option. when selected, app will show required fields, and user can update the fields and submit and the table gets updated accordingly

1 Like

Do you have a sample code that you would be comfortable in sharing? Or a screenshot of the app?

Hi @hellokatechan

I modified this code for my need. I added more functionalities to this base code.

1 Like

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