Example: Build a form that writes to Snowflake¶
This example walks you through building a Streamlit in Snowflake app that collects user input through a form
and writes it to a Snowflake table. The app also reads the data back to display all
submissions, and uses st.user to track who submitted each entry.
The app uses a container runtime. Before you begin, make sure you’ve completed the prerequisites.
Set up the target table¶
This example uses a database called crud_demo. You can substitute any database
and schema you have access to – just update the references in the SQL and app code to match.
Create a table to store form submissions. Run the following SQL in a worksheet or SQL session:
Write the app code¶
On your local machine, create a file named streamlit_app.py with the following code.
If you plan to use Snowsight, you can paste this code into the editor after creating
the app.
This app uses:
st.formto collect input before submitting, preventing re-runs on every widget interaction.st.connection("snowflake").session()to get a Snowpark session for writing data. For more information, see Manage secrets and configure your Streamlit app.session.sql()instead ofconn.query()to read back the submissions.conn.query()caches results by default, so new entries wouldn’t appear until the cache expires.session.sql()executes a fresh query on every rerun.st.user.user_nameto record who submitted each entry. For more information, see Personalize your Streamlit app with user information.
Declare dependencies¶
This app only uses streamlit and the built-in Snowflake connection, so no additional
dependencies are required.
For more information, see Manage dependencies for your Streamlit app.
Deploy the app¶
Sign in to Snowsight.
In the navigation menu, select Projects » Streamlit.
Select + Streamlit App.
Enter
feedback_appas the app name.Select a database and schema.
Select Run on container, then select a compute pool and query warehouse.
Select Create.
In the editor, replace the starter code with the app code above.
Select Run.
Stage your app files:
Create the Streamlit app:
To view your app, sign in to Snowsight, then In the navigation menu, select Projects » Streamlit, and select your app.
Note
Snowflake CLI version 3.14.0 or later is required. Version 3.14+ uses the modern CREATE STREAMLIT syntax by default.
Create a project directory with the following structure:
Create a
snowflake.ymlfile:Deploy the app:
Try the app¶
Open the app in your browser.
Fill in the form fields and select Submit.
The feedback table below the form updates to show your new submission, including your email address and a timestamp.
Submit a few more entries, then try filtering or sorting the data in the table.
Extend the app¶
Try adding a delete button next to each row, or a chart that shows the average rating by category. For example, add the following after the dataframe:
If you add plotly, declare it in a requirements.txt file:
For more complex dependency scenarios, you can use a pyproject.toml file instead.
For more information, see Manage dependencies for your Streamlit app.
Clean up¶
To remove the resources created in this example, run the following SQL:
What’s next?¶
Create your Streamlit app: Learn about all the options for creating apps.
Personalize your Streamlit app with user information: Explore all the user attributes available through
st.user.Manage secrets and configure your Streamlit app: Access secrets and external services in your app.
Sharing Streamlit in Snowflake apps: Share your app with other users.