Getting Started with Streamlit Apps in Snowflake

Stefano Ray
Target Reply | Insights Hub
4 min readNov 18, 2023

Hey folks,

In this article, we are going to explore how it is possible to build and run a Streamlit Application natively in Snowflake, in just a few minutes.

Introduction

Streamlit is an open-source Python library and framework that is used for building Data, ML and AI applications. It is designed to make it easy for developers to create interactive and visually appealing Web Applications, without requiring expertise in other popular web development frameworks and languages (such as Angular, Javascript and Typescript).

Recently, Snowflake announced a new feature that allows to create and to host Streamlit applications directly within Snowflake Data Cloud, by leveraging the platform’s robust cloud infrastructure and access control mechanisms.

Initial Setup

Currently, the Streamlit integration in Snowflake is a Preview Feature, which is only available to AWS accounts in all non-government AWS regions and in AWS accounts not using AWS PrivateLink.

Before getting started, review the official Snowflake guide for the prerequisites for using Streamlit in Snowflake and for applying the proper grants to your Snowflake Role.

Let’s start by creating a sample Snowflake Database, Schema and Virtual Warehouse for our application and by applying the required grants and permissions. Make sure to use a Snowflake Role allowed to perform these actions and with the “CREATE STREAMLIT” permission set.

Create a Snowflake Virtual Warehouse for running the application:

CREATE WAREHOUSE IF NOT EXISTS STREAMLIT_WH
WITH
WAREHOUSE_TYPE = STANDARD
WAREHOUSE_SIZE = XSMALL
MAX_CLUSTER_COUNT = 1
MIN_CLUSTER_COUNT = 1
AUTO_SUSPEND = 60
AUTO_RESUME = TRUE
INITIALLY_SUSPENDED = TRUE
COMMENT = 'Snowflake Virtual Warehouse for hosting sample Streamlit Application.'
;

USE WAREHOUSE STREAMLIT_WH;

Create a Snowflake Database, Schema and Stage for Streamlit objects:

CREATE DATABASE IF NOT EXISTS STREAMLIT_DB
COMMENT = 'Snowflake Database for hosting sample Streamlit Application.';

CREATE SCHEMA IF NOT EXISTS STREAMLIT_DB.STREAMLIT_SCHEMA;

CREATE STAGE STREAMLIT_DB.STREAMLIT_SCHEMA.STREAMLIT_STAGE;
USE SCHEMA STREAMLIT_DB.STREAMLIT_SCHEMA;
Snowflake Streamlit Objects

Creating a Sample Streamlit Application

On your local computer, create a folder named “streamlit” and create a new Python file named “hello_snowflake.py” in it.

Add the following lines to the previously created Python file:

import streamlit as st

st.title("Hello Snowflake! ❄️")

This is the standard way in Streamlit to print a simple title to screen.

Upload Files to Snowflake Stage

Snowflake recently also introduced a feature to upload files into Stages directly from the Snowflake UI (Snowsight).

Click on the “STREAMLIT_STAGE” object and open the stage details in a new tab:

Now, click on the add files button on the top right of the opened tab:

Specify “streamlit” as the folder path to be created in the Snowflake Stage and upload the Python file:

Run a LIST command to double check that the file has been correctly uploaded to the Snowflake Internal Stage:

LIST @STREAMLIT_STAGE;

Create Streamlit Application

Execute the following command to create the Streamlit application based on the root path/location on the staged files:

CREATE STREAMLIT IF NOT EXISTS HELLO_SNOWFLAKE
ROOT_LOCATION = '@STREAMLIT_DB.STREAMLIT_SCHEMA.STREAMLIT_STAGE/streamlit'
MAIN_FILE = '/hello_snowflake.py'
QUERY_WAREHOUSE = STREAMLIT_WH
COMMENT = 'Hello Snowflake Streamlit Application!';

View the Streamlit Application

The Streamlit app has been created and now it is possible to view it. From the main Snowsight menu, select “Streamlit” and click on the “HELLO_SNOWFLAKE” app:

That’s it! We created our first Streamlit application in Snowflake and we are now ready to enrich it with additional libraries and features :)

Cleaning

Run the following script to remove all the previously created Snowflake resources and objects:

DROP STREAMLIT HELLO_SNOWFLAKE;
DROP STAGE STREAMLIT_DB.STREAMLIT_SCHEMA.STREAMLIT_STAGE;
DROP SCHEMA STREAMLIT_DB.STREAMLIT_SCHEMA;
DROP DATABASE STREAMLIT_DB;
DROP WAREHOUSE STREAMLIT_WH;

--

--