Create and deploy Streamlit apps using SQL

This topic describes how to deploy a Streamlit app in Snowflake by using SQL commands. It describes how to deploy single-page and multi-page Streamlit apps.

Create a Streamlit app by using SQL

Before creating a Streamlit app by using SQL, ensure you meet the required prerequisites.

To create a Streamlit app in Snowflake by using SQL commands, perform each of the following tasks:

Create the Streamlit files on your local file system

This section describes how to create a single-page or multi-page Streamlit app.

To create the files for your Streamlit app, do the following:

  1. On your local file system, create your main Streamlit app.

  2. Optional: If you are creating a multi-page Streamlit app, create a folder called pages on your local file system, and add the additional Streamlit pages to this subfolder.

  3. Optional: To include external packages in your Streamlit app, create an environment.yml file. For details on including external packages, see Manage packages by using the environment.yml file.

After you create the Streamlit pages, your directory structure should look similar to this:

└── streamlit/
    └── environment.yml
    └── streamlit_main.py
    └── pages/
         └── data_frame_demo.py
         └── plot_demo.py
Copy

Note

The environment.yml and the optional pages subfolder must be at the same level as your main Streamlit file. Any additional Streamlit pages must be included within the pages subfolder.

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_root_folder>/streamlit/streamlit_main.py @streamlit_db.streamlit_schema.streamlit_stage overwrite=true auto_compress=false;
    PUT file:///<path_to_your_root_folder>/streamlit/environment.yml @streamlit_db.streamlit_schema.streamlit_stage overwrite=true auto_compress=false;
    PUT file:///<path_to_your_root_folder>/streamlit/pages/streamlit_page_2.py @streamlit_db.streamlit_schema.streamlit_stage/pages/ overwrite=true auto_compress=false;
    PUT file:///<path_to_your_root_folder>/streamlit/pages/streamlit_page_3.py @streamlit_db.streamlit_schema.streamlit_stage/pages/ overwrite=true auto_compress=false;
    
    Copy

Create a STREAMLIT object

A STREAMLIT object is a database object in Snowflake that encapsulates the files required by your Streamlit app.

  1. To create a STREAMLIT object, run the CREATE STREAMLIT command, as shown in the following example:

    CREATE STREAMLIT hello_streamlit
    ROOT_LOCATION = '@streamlit_db.streamlit_schema.streamlit_stage'
    MAIN_FILE = 'streamlit_main.py'
    QUERY_WAREHOUSE = my_warehouse;
    
    Copy

    This command creates a STREAMLIT object named hello_streamlit based on the path and file specified in ROOT_LOCATION 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.

  2. To verify that the Streamlit object was created, run the SHOW STREAMLITS command, as shown in the following example:

    SHOW STREAMLITS;
    
    Copy

Manage packages by using the environment.yml file

To install additional Python packages in your Streamlit app:

  1. Create an environment.yml file on your local file system.

  2. To upload the file to the stage location specified by the ROOT_LOCATION parameter of the STREAMLIT object, run the PUT command.

    Packages listed in the environment.yml are 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
Copy

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.yml file, include a streamlit dependency as shown in the following example:

name: sf_env
channels:
- snowflake
dependencies:
- scikit-learn
- streamlit=1.31.1
Copy

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;
Copy

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;
Copy

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'
Copy

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'
Copy

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;
Copy

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;
Copy

Delete a STREAMLIT object

To delete a STREAMLIT object, run the DROP STREAMLIT command, as shown in the following example:

DROP STREAMLIT hello_streamlit;
Copy