top of page

Streamlit Integration With Snowpark

Updated: Mar 14

Author: Firoj Kumar Patnaik



Recently Snowflake has acquired Streamlit, which is a big move towards creating and sharing data applications. In this blog, we will see how to integrate Streamlit and Snowflake using Snowpark for Python.


What is Streamlit?

Streamlit is an application framework that is open-source, python-based and provides the fastest way to build and share data apps. You do not need to have knowledge of HTML, CSS, or any JavaScript framework in order to start with Streamlit. It is as simple as writing code in python.


What is Snowpark?

Snowpark is an API that you can use to create Data Frames that are executed lazily on Snowflake’s data cloud. Using Snowpark you can write code in a language like python and can use the computation of snowflake warehouses to make operations faster and more secure. All the python code is translated into SQL code in the backend.


Prerequisite
  • Install python version 3.8 on your system because Snowpark API requires python version 3.8.

  • You can use your favorite IDE to write code.

  • Install the Snowpark Python package into your system.


pip install snowflake-snowpark-python


  • Install Streamlit on your system.


pip install streamlit


Steps
  • First, let’s see how to establish the connection with the snowflake. We need to create a session object for it. So, to create a Session object do the following steps.


#import libraries

import os

from snowflake.snowpark import Session

#store connection parameters to a dictionary

connection_parameters = {

"account": os.environ["snowflake_account"],

"user": os.environ["snowflake_user"],

"password": os.environ["snowflake_password"],

"role": os.environ["snowflake_user_role"],

"warehouse": os.environ["snowflake_warehouse"],

"database": os.environ["snowflake_database"],

"schema": os.environ["snowflake_schema"]

}

#create session object by passing connection parameters

test_session = Session.builder.configs(connection_parameters).create()


  • Now let’s write a piece of code that will query the data from snowflake and snow it as a data frame in the Streamlit website.

  • Fetching data from tables is very easy using the table() method of the session object.


ev_sales = test_session.table('EV_SALES').sort('Year')


  • As mentioned earlier, Snowpark evaluated data frames lazily. So we need to trigger an action method to execute SQL on the server side.

ev_sales_df = ev_sales.to_pandas()


Here to_pandas() is an action method.


  • Now we have data as a panda’s data frame. So, let’s show it in Streamlit.


st.dataframe(ev_sales_df)


  • Now we will try to present the data in the form of a chart.


elv_sales = test_session.sql('SELECT MAKE, SUM(UNIT_SOLD) as "UNIT SOLD" from "DEMO_DB"."PUBLIC"."EV_SALES" GROUP BY MAKE').to_pandas()

st.bar_chart(elv_sales.set_index('MAKE'), width=850, height=500)


Here we have written a SQL query to find the total sales EV for every manufacturer/make. Instead of writing SQL, you can also write code in data frame-like syntaxes.

Now we have completed all the steps. Let’s see the output by running the app.


streamlit run app.py



Conclusion

This was a quick introduction to give you a taste of how you can connect Streamlit with Snowflake using Snowpark. Hope this blog was informative. Thank You!


References
459 views0 comments
bottom of page