Example: Build a personalized data dashboard¶
This example walks you through building a Streamlit in Snowflake app that queries Snowflake data, adds a third-party charting library, and personalizes the display for each viewer. By the end, you’ll understand the core development cycle: create, deploy, edit, and redeploy.
The app uses a container runtime. Before you begin, make sure you’ve completed the prerequisites.
Set up sample data¶
This example uses a database called dashboard_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 with sample revenue data. Run the following SQL in a worksheet or SQL session:
Write the app code¶
On your local machine, in a project directory of your choice, 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:
conn.query()to query data from Snowflake. Results are cached automatically, so the query only runs once until the cache expires. For more information, see Manage secrets and configure your Streamlit app.st.user.user_nameto greet the current viewer. For more information, see Personalize your Streamlit app with user information.plotlyfor interactive charts, which is an external dependency that you declare in the next step.
Declare dependencies¶
Container runtimes install packages listed in a requirements.txt file. Create a
requirements.txt file alongside your streamlit_app.py:
When the app starts, the container runtime automatically installs the declared packages.
For more complex dependency scenarios, you can use a pyproject.toml file instead.
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
revenue_dashboardas 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.
Upload or create the
requirements.txtfile by selecting + (Add) » Create new file, enteringrequirements.txt, and pasting the contents.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:
Make a change¶
Try editing your app to see the development cycle in action. Add a summary metric by
inserting the following two lines into streamlit_app.py, between the
filtered = ... line and the fig = px.bar(...) line:
If you’re editing in the browser, paste the lines into the editor and select Run.
Stage the updated file, then copy it to your app’s live version location:
Save the file locally and redeploy:
For more information about the editing workflow, see Edit your Streamlit app.
Clean up¶
To remove the resources created in this example, run the following SQL:
What’s next?¶
Example: Build a form that writes to Snowflake: Build an app with a form that writes data back to Snowflake.
Personalize your Streamlit app with user information: Learn more about personalizing apps with
st.user.External network access in Streamlit in Snowflake: Connect your app to external APIs.