Streamlit to upload files to Snowflake stages
Disclaimer: The views expressed in this article are mine alone and do not necessarily reflect the view of my current, former, or future employers.
I love Snowflake and I love Streamlit. I love Snowflake because it makes easy to manage my data and I love Streamlit because it makes simple to build beautiful data intensive web applications.
This article describes how to build a Streamlit application to let users upload a file into a Snowflake stage.
The article assumes that you have access to a Snowflake account and Python installed in your machine.
The problem
It is simple to build an application that let users upload files in Streamlit. You just need to use the "file_uploader" API to display a file uploader widget like in the example below:
import streamlit as st
uploaded_file = st.file_uploader("Choose a file")
if uploaded_file is not None:
...
The API"file_uploader" returns a subclass of BytesIO. This is normally not a problem. It is a problem when you plan to use the put command inside a Snowflake Python cursor because you need first to save the file like in the following code:
import streamlit as st
import snowflake.connector as sc
import os@st.experimental_singleton
def sf_con():
return sc.connect(
user=st.secrets["sf_usr"],
account=st.secrets["sf_account"],
password…