Getting started with Streamlit in Snowflake¶
This topic describes how to create and run your first Streamlit in Snowflake app.
Prerequisites¶
Before you can create a Streamlit app, ensure that your administrator has completed the essential security setup for Streamlit apps.
Your role must have the following privileges:
Privilege |
Object |
Notes |
|---|---|---|
USAGE |
Database where you create the Streamlit app |
|
CREATE STREAMLIT,
USAGE
|
Schema where you create the Streamlit app |
|
USAGE |
Warehouse that runs queries in the Streamlit app |
For warehouse-runtime apps, the warehouse also runs the app’s Python code. |
USAGE |
Compute pool that runs the Streamlit app |
This privilege is only required if your app uses a container runtime (Preview). |
For more information, see Privileges required to create and use a Streamlit app.
Deploy your first Streamlit in Snowflake app¶
The fastest way to get started is to create a Streamlit app using the default app files. Snowflake automatically provides starter code when you create an app without specifying source files.
Sign in to Snowsight.
In the navigation menu, select Projects » Streamlit.
Select + Streamlit App.
Enter a name for your app.
Select a database and schema to create your app in.
Configure your app.
To create a container-runtime app (Preview), select the following options:
Select Run on container.
Select a compute pool to run your app on. Preferrably, this should be a compute pool that is dedicated to running Streamlit apps.
Select a query warehouse to run your app’s queries on.
To create a warehouse-runtime app, select the following options:
Select Run on warehouse.
Select a warehouse to run your app on. Preferrably, this should be a warehouse that is dedicated to running Streamlit apps.
Select Create.
Snowflake creates a new Streamlit app with example code. Snowsight redirects you to the app editor. Your app will be ready within a few seconds to a few minutes, depending on the runtime type. Then, you can view and edit it immediately.
While using your preferred database and schema in a SQL session, run the following SQL commands:
Create a default Streamlit object.
To create a container-runtime app (Preview):
CREATE STREAMLIT my_first_app RUNTIME_NAME = 'SYSTEM$ST_CONTAINER_RUNTIME_PY3_11' COMPUTE_POOL = my_compute_pool QUERY_WAREHOUSE = my_warehouse;
To create a warehouse-runtime app:
CREATE STREAMLIT my_first_app QUERY_WAREHOUSE = my_warehouse;
These commands create a Streamlit app named
my_first_appwith default starter code.Make the app live:
ALTER STREAMLIT my_first_app ADD LIVE VERSION FROM LAST;
Because Streamlit objects use versioned stages, you must push your app’s code to the live version location before a user with only USAGE privilege on the Streamlit object can view it.
To view your app, sign in to Snowsight.
In the navigation menu, select Projects » Streamlit, and then 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.
Initialize a new Streamlit project:
snow init my_first_app --template example_streamlit
Navigate to the project directory:
cd my_first_app
Configure your app by editing the
snowflake.ymlfile.To create a container-runtime app (Preview), add
compute_poolandruntime_name:definition_version: 2 entities: my_streamlit: type: streamlit identifier: my_first_app query_warehouse: my_warehouse compute_pool: my_compute_pool runtime_name: SYSTEM$ST_CONTAINER_RUNTIME_PY3_11 main_file: streamlit_app.py artifacts: - streamlit_app.py
To create a warehouse-runtime app, use the default configuration:
definition_version: 2 entities: my_streamlit: type: streamlit identifier: my_first_app query_warehouse: my_warehouse main_file: streamlit_app.py artifacts: - streamlit_app.py
Save your
snowflake.ymlfile.Deploy the app to Snowflake and open it in your browser:
snow streamlit deploy --open
Edit your app¶
For more information about editing your app, see Editing a deployed Streamlit app.
After creating your app, you can edit the code to customize it:
Sign in to Snowsight.
In the navigation menu, select Projects » Streamlit, and then select your app.
To open the editor, select Edit.
In the editor pane, modify the code in the
streamlit_app.pyfile.Select Run to see your changes.
If you have one or more edited app files on a stage, you can use COPY FILES to copy them to your app’s source location with the following commands:
Identify your app’s source location:
DESCRIBE STREAMLIT my_first_app;
The
live_version_location_urivalue is the source location for your app. Copy this to a notepad to use in the next step. This is a snow URL (snow://).Copy files from an internal stage to your app’s source location:
COPY FILES INTO '<live_version_location_uri>' FROM @my_stage FILES = ('streamlit_app.py');
Note
Snowflake CLI version 3.14.0 or later is required. Version 3.14+ uses the modern CREATE STREAMLIT syntax by default.
In your local directory, edit and save the
streamlit_app.pyfile in your local project directory.Deploy your changes to Snowflake:
snow streamlit deploy --replace
This will overwrite your entire app with the new version.
What’s next?¶
Now that you’ve created your first app, explore these topics to learn more:
Manage dependencies for your Streamlit app: Add Python packages to your app.
External network access in Streamlit in Snowflake: Connect your app to external services.