i am new to streamlit and am using it via snowflake. i am trying to create an application where a user selects their category and a table shows their selection. They will then be able to edit the some of the fields which will flow into another table in snowflake. My code is below but i do not know how i can make the table reflect the selections, the whole table (blah) is displaying.
in SQL i would use something like ‘where country=‘USA’ and Line=2’ but i’m not sure what to do here. Apologies for my ignorance, i’m new to this!
if write_to_snowflake:
with st.spinner(“Updating…”):
snowpark_df.write.mode(“overwrite”).save_as_table(‘DEV_PROJECT.MANUAL_blah’)
st.success(“Wrote to Snowflake successfully ”)
To create a Streamlit application that interacts with Snowflake and allows users to select and edit data, you need to implement a few key functionalities:
You need to filter your Snowflake table based on the user’s selection of country and line.
Allow users to edit the filtered data and reflect those changes back in Snowflake.
import streamlit as st
import pandas as pd
def get_distinct_values(table, column):
query = f"SELECT DISTINCT {column} FROM {table}"
return pd.read_sql(query, session)
def get_filtered_data(table, country, line):
query = f"SELECT * FROM {table} WHERE Country = '{country}' AND Line = {line}"
return pd.read_sql(query, session)
# Fetch distinct values for filtering
distinct_countries = get_distinct_values('DEV_PROJECT.blah', 'Country')
distinct_lines = get_distinct_values('DEV_PROJECT.blah', 'Line')
# User selection
select_country = st.selectbox("Select Country", distinct_countries['Country'])
select_line = st.selectbox("Select Line", distinct_lines['Line'])
# Filter data based on selection
filtered_df = get_filtered_data('DEV_PROJECT.blah', select_country, select_line)
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")
# Write changes back to Snowflake
if write_to_snowflake:
with st.spinner("Updating…"):
snowpark_df = session.create_dataframe(df_edited)
snowpark_df.write.mode("overwrite").save_as_table('DEV_PROJECT.MANUAL_blah')
st.success("Wrote to Snowflake successfully :tada:")
Let me know if you might require any further assistance!
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.