SF Data Export
0 0
Read Time:2 Minute, 23 Second

One of the most common tasks is exporting data from cloud platforms like Snowflake and saving it in formats like CSV for further analysis or sharing with stakeholders. While Snowflake offers powerful tools for querying and manipulating data, exporting it in a user-friendly format requires a bit more effort.

In this blog post, we’ll dive into a practical solution that leverages both Snowflake and Streamlit to build an intuitive data export application. This application allows users to easily query a Snowflake database, select the desired data, and download it in CSV format with just a few clicks.

The Use Case

Often, users want to quickly export a set of data from Snowflake, whether it’s a subset of records from a table or a specific selection of columns. While this process can be tedious using traditional SQL queries and exporting methods, Streamlit makes it easy to create a user-friendly interface for Snowflake interactions.

Our goal with this Streamlit app is to:

  1. Select a database, schema, and table from Snowflake.
  2. Preview the data before exporting.
  3. Allow users to filter and choose the columns they want to export.
  4. Download the selected data in CSV format.
  5. Send an email notification upon export completion.
SF Data Export

Let’s break down the Streamlit app’s code that integrates with Snowflake:

  • Setting up the Session with Snowflake.

from snowflake.snowpark.context import get_active_session
session = get_active_session()

  • Selecting the Database and Schema

selected_db = st.selectbox(“Select Database”, databases)
selected_schema = st.selectbox(“Select Schema”, schemas)

  • Selecting a Table and Fetching Record Count

selected_table = st.selectbox("Select Table", tables)
TABLE_NAME = f"{selected_db}.{selected_schema}.{selected_table}"
total_records = session.sql(f"SELECT COUNT(*) FROM {TABLE_NAME}").collect()[0][0]

  • Handling Large Tables with Record Limits

if total_records > 100000:
st.warning("⚠️ This table has more than 100,000 records. Please select up to 100,000 records.")

  • Data Filtering and Exporting

num_records = st.slider("Select the number of records to download:", 10, max_records, 10)
columns = df_snowflake.columns.tolist()
selected_columns = st.multiselect("Select columns to export:", columns, default=columns)
df_filtered = df_snowflake[selected_columns]

  • CSV Download Button

def convert_df_to_csv(df):
return df.to_csv(index=False).encode("utf-8")
st.download_button(
label="📥 Download Data as CSV",
data=csv_data,
file_name=f"{selected_table}_export.csv",
mime="text/csv"
)

  • Email Notification After Export

if st.button("📧 Send Email Notification"):
session.sql(f"""
CALL SYSTEM$SEND_EMAIL(
'FILE_ATTACHMENT',
'{recipient_email}',
'{email_subject}',
'{email_content}',
'text/html'
)

Conclusion

This simple yet powerful application enables users to interact with Snowflake data directly from a user-friendly interface powered by Streamlit. It allows for quick data previews, exporting selected columns, and downloading CSV files with minimal friction.

 

 

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

Your email address will not be published. Required fields are marked *