Create and deploy Streamlit apps using SQL¶
This topic describes how to deploy a Streamlit app in Snowflake by using SQL commands. Starting from a local development environment, you can copy your Streamlit app files to a named stage in Snowflake and create a Streamlit object from those files.
Create a Streamlit app by using SQL¶
Before creating a Streamlit app by using SQL, ensure that you meet the required prerequisites.
To create a Streamlit app in Snowflake by using SQL commands, perform each of the following tasks:
Optional: Create the Streamlit files on your local file system¶
This section describes how to create a local set of app source files and stage them in Snowflake. In the next section, if you don’t have staged source files when you create your STREAMLIT object, a default set of source files is copied into your STREAMLIT object instead.
On your local file system, create your main Streamlit app.
Optional: To configure your deployment environment and specify dependencies, create an
environment.ymlfile.If you don’t include this file, your app will run on the latest supported versions of Python and Streamlit in Streamlit in Snowflake. For information about app dependencies, see Manage packages by using the environment.yml file.
After you create the Streamlit app, your directory structure should look similar to this:
project_directory/
├── .streamlit/
│ └── config.toml
├── environment.yml
└── streamlit_app.py
Note
Streamlit in Snowflake supports multipage Streamlit apps. This example only shows a single-page app, but you can add a pages/ directory or use
st.navigation to create a multipage app. To learn about multipage apps, see the
Overview of multipage apps in the Streamlit open-source
documentation.
Upload your Streamlit files to a named stage¶
To create a Streamlit app in Streamlit in Snowflake, you must upload your application files to a named stage.
To upload application files, do one of the following:
Upload the application files by using Snowsight, as described in Staging files using Snowsight.
Upload the application files by using SnowSQL, as shown in the following example:
PUT file:///<path_to_your_project_directory>/streamlit/streamlit_app.py @streamlit_db.streamlit_schema.streamlit_stage overwrite=true auto_compress=false; PUT file:///<path_to_your_project_directory>/streamlit/environment.yml @streamlit_db.streamlit_schema.streamlit_stage overwrite=true auto_compress=false; PUT file:///<path_to_your_project_directory>/streamlit/.streamlit/config.toml @streamlit_db.streamlit_schema.streamlit_stage/.streamlit/ overwrite=true auto_compress=false;
Create a STREAMLIT object¶
A STREAMLIT object is a database object in Snowflake that encapsulates the files required by your Streamlit app.
To create a STREAMLIT object, run the CREATE STREAMLIT command, as shown in the following example:
CREATE STREAMLIT hello_streamlit FROM '@streamlit_db.streamlit_schema.streamlit_stage' MAIN_FILE = 'streamlit_app.py' QUERY_WAREHOUSE = my_warehouse;
This command creates a STREAMLIT object named
hello_streamlitbased on the path and file specified in FROM and MAIN_FILE.Note
Although the QUERY_WAREHOUSE clause is optional, you must specify a query warehouse to be able to run the Streamlit app in Snowflake.
Optional: To verify that the Streamlit object was created, run the SHOW STREAMLITS command:
SHOW STREAMLITS;
To complete initializing the app, the owner role must either view the app in Snowsight, or run the following command:
ALTER STREAMLIT hello_streamlit ADD LIVE VERSION FROM LAST;
Manage packages by using the environment.yml file¶
To install additional Python packages in your Streamlit app:
Create an
environment.ymlfile on your local file system.To upload the file to the stage location specified by the
ROOT_LOCATIONparameter of the STREAMLIT object, run the PUT command.Packages listed in the
environment.ymlare installed from the Snowflake Anaconda Channel.
The following sample environment.yml shows how to install scikit-learn within the Streamlit environment:
name: sf_env
channels:
- snowflake
dependencies:
- scikit-learn
The name and channels properties are required. Also, the - snowflake key is required under the
channels property.
Note
You can only install packages listed in the Snowflake Anaconda Channel. Streamlit in Snowflake does not support external Anaconda channels.
Pin the Streamlit version in the environment.yml file¶
To pin the Streamlit version in the
environment.ymlfile, include astreamlitdependency as shown in the following example:name: sf_env channels: - snowflake dependencies: - scikit-learn - streamlit=1.31.1
View a Streamlit app¶
To view information about the STREAMLIT object, run the DESCRIBE STREAMLIT command, as shown in the following example:
DESC STREAMLIT hello_streamlit;
To view your Streamlit app in Snowsight, see View a Streamlit app.
Manage STREAMLIT objects¶
After creating a STREAMLIT object, use the ALTER STREAMLIT command to modify different properties as described in the following sections.
Rename a STREAMLIT object¶
To rename a STREAMLIT object, use the RENAME TO clause of the ALTER STREAMLIT command, as shown in the following example:
ALTER STREAMLIT hello_streamlit RENAME TO hello_snowflake;
Change the stage or main file in a STREAMLIT object¶
To change the path to the stage for a STREAMLIT object, use the ALTER STREAMLIT command to set the ROOT_LOCATION property of the object, as shown in the following example:
ALTER STREAMLIT hello_streamlit SET ROOT_LOCATION = '@snowflake_db.snowflake_schema.snowflake_stage'
To change the main Streamlit file in a STREAMLIT object, use the ALTER STREAMLIT command to set the MAIN_FILE property of the object, as shown in the following example:
ALTER STREAMLIT hello_streamlit SET MAIN_FILE = 'snowflake_main.py'
Change the query warehouse assigned to a STREAMLIT object¶
To add a query warehouse or change the current query warehouse for a STREAMLIT object, use the ALTER STREAMLIT command to set the QUERY_WAREHOUSE property of the object, as shown in the following example:
ALTER STREAMLIT hello_streamlit SET QUERY_WAREHOUSE = my_new_warehouse;
List available STREAMLIT objects¶
To list the Streamlit apps that are available to your current role, run the SHOW STREAMLITS command, as shown in the following example:
SHOW STREAMLITS;
Delete a STREAMLIT object¶
To delete a STREAMLIT object, run the DROP STREAMLIT command, as shown in the following example:
DROP STREAMLIT hello_streamlit;