Deploying Streamlit Applications in Snowflake¶
Previous Chapter Recap:
In the last chapter, we developed an interactive Streamlit application for predicting penguin species using our Random Forest model. We focused on creating an intuitive user interface through:
- Visualization of prediction probabilities using Streamlit's progress bars
- Strategic content organization with columns and containers for enhanced layout
- Implementation of success messages to display clear prediction results
Moving forward, we'll explore how to deploy this application within Snowflake's ecosystem using Streamlit in Snowflake (SiS). This integration combines our interactive prediction capabilities with Snowflake's enterprise-grade data platform features.
In this chapter, we will:
- Modify our existing Streamlit application for seamless Snowflake integration
- Establish and configure secure Snowflake connections
- Deploy and validate our application using Streamlit in Snowflake
- Explore critical distinctions between local development and Snowflake deployment
Preparing for Deployment¶
Database¶
Important
It is assumed that Snowflake CLI has been installed and you have test your snowflake connection.
Set your default connection name using
snow connection set-default <your snowflake connection>
- Set an environment variable named
SNOWFLAKE_DEFAULT_CONNECTION_NAME
And then for quick check try:
Database¶
For this demo all our Snowflake objects will be housed in a DB called st_ml_app
.
Create the Database
snow object create database \
name='st_ml_app' \
comment='Database used for Streamlit ML App Tutorial'
Schema¶
Let us create one schema to hold the notebooks.
# notebooks
snow object create schema \
name='notebooks' comment='Will hold all notebooks' \
--database='st_ml_app'
Download and import the notebook and follow the instructions on the notebook to prepare the environment for deployment.
Deploying the App¶
Create Streamlit Project¶
Create a Snowflake project from a template, to make things easy and clean we will create the application in $TUTORIAL_HOME/sis
.
Note
The application init uses the example_streamlit
template from https://github.com/snowflakedb/snowflake-cli-templates
Update the App¶
TODO: Note on Copy
Edit and update the $TUTORIAL_HOME/sis/streamlit_app.py
with,
streamlit_app.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
|
Verify Python Packages¶
Edit and update $TUTORIAL_HOME/sis/environment.yml
to be like, ensuring that the packages used locally and in Snowflake are same.
environment.yml | |
---|---|
Verify Project Manifest¶
Edit and update the Project manifest $TUTORIAL_HOME/sis/snowflake.yml
to be inline with your settings, especially
name
main_file
query_warehouse
artifacts
IMPORTANT
If you have followed along the tutorial as is without any changes the only that you might need to change is query_warehouse
. If you are using trial account then update it to COMPUTE_WH
.
snowflake.yml | |
---|---|
Navigate to the application(sis) folder,
Run the following command to deploy the Streamlit application to Snowflake,Note
The output of the command displays the URL to access the application. In case you missed to note run the following command,
There you go we have seamlessly deployed the application to SiS with a very little effort.
Summary¶
This chapter guided you through the process of transforming a locally running Streamlit application into a production-ready deployment within Snowflake. You learned the essential modifications needed for Snowflake compatibility, understood the configuration requirements, and mastered the deployment process. You now have a fully functional Streamlit application running in Snowflake's secure environment, accessible to your organization's users through Snowflake's interface.
Pro Tip: You can run this entire application using Snowflake Notebook. Download and import the environment.yml and application notebook and experience the Snowflake Notebook magic! 🚀