Hello,
I am trying to create a Steamlit app in Snowflake that allows our business users to edit a table in Snowflake via the app.
The app needs to have at least 2 functionalities below:
- You need to filter your Snowflake table based on the user’s selection of aggregator and country.
- Allow users to edit the filtered data and reflect those changes back in Snowflake.
Bonus: I would also like to add a Download button to allow business users to download the updated data after the edits.
Below is what I currently have from my research. However, when I ran the code, I got the following error:
AttributeError: ‘Session’ object has no attribute ‘cursor’
Here is the code:
import pandas as pd
import streamlit as st
import time
from snowflake.snowpark import Session
from snowflake.snowpark.context import get_active_session
session = get_active_session()
def get_distinct_values(table, column):
query = f"SELECT DISTINCT {column} FROM {table}"
return pd.read_sql(query, session)
def get_filtered_data(table, aggregator, country):
query = f"SELECT * FROM {table} WHERE AGGREGATOR = {aggregator} AND COUNTRY_NAME = {country}"
return pd.read_sql(query, session)
# Fetch distinct values for filtering
distinct_aggregators = get_distinct_values('STORE_MAPPING', 'AGGREGATOR')
distinct_countries = get_distinct_values('STORE_MAPPING', 'COUNTRY_NAME')
# User selection
select_aggregator = st.selectbox("Select Aggregator", distinct_aggregators['AGGREGATOR'])
select_country = st.selectbox("Select Country", distinct_countries['COUNTRY_NAME'])
# Filter data based on selection
filtered_df = get_filtered_data('STORE_MAPPING', select_aggregator, select_country)
st.write(filtered_df) # Display the filtered dataframe
# Editable form
st.header(":pencil2: Edit Data")
with st.form("Table write"):
df_edited = st.experimental_data_editor(filtered_df, use_container_width=True)
write_to_snowflake = st.form_submit_button("Update")
Could you please let me know what I did wrong? Thank you in advance!