Get startedGet started for free

Apps - Streamlit in Snowflake - Part I

1. Apps - Streamlit in Snowflake - Part I

At Snowflake, we have a photography Slack channel where Snowflake employees share beautiful photographs they’ve taken – most of them of landscapes or wildlife, some of architecture. One of my colleagues, Dash, frequently shares photographs in that Slack channel. Actually, you’ve sort of seen a bunch of Dash in this course because this is a sticker of him on the back of my laptop. He’s famous for giving Snowflake demos while wearing ski goggles. Anyway, I have this tradition of periodically updating my desktop background with one of Dash’s photos when a new one comes along that I really like – a picture of the aurora borealis, a picture of a snow-covered mountain, a picture of a dock jutting into a lake with mountains in the background. I’m starting this video talking about the Snowflake photography Slack channel because – and I’m totally serious about this – this is legitimately what comes to mind when I think about building apps with Streamlit, which is the subject of this video. Looking at a beautiful Streamlit app feels like looking at a photograph of Mount Rainier. Saying that makes me feel like I’m on a 2:00 AM infomercial for a must-have electric whisk, but that’s how I feel. Streamlit apps are beautiful. But instead of talking to you about how beautiful they are, I should just show you some from the gallery on their website. If we go to [https://streamlit.io/gallery](https://streamlit.io/gallery), and we click on Geography and Society, we can click on the “Prettymapp” Streamlit app, which lets you enter the name of a location, and generate a beautiful map. Let’s click on the city Heerhugowaard, in the Netherlands. We can change the Color theme. [As it’s changing] While this is loading, let me tell you a bit about Streamlit. It's an open source framework that lets you create dynamic data applications with just a few lines of Python code. Okay – that’s done – For fun, let’s type in “JFK Airport, NY,” which is like three miles from where I grew up. So cool. [As it’s changing] Streamlit is available natively within Snowflake's Snowsight UI. We call this Streamlit in Snowflake, and it lets you easily connect to any dataset you already have access to within your warehouse, and deploy securely to the rest of your organization in one click. Now what’s most impressive to me about Streamlit isn’t that the final products are beautiful – there are lots of sleek apps in the world – but how easy they are to make. You don’t have to go to a coding bootcamp and learn React to make an app this crisp. You can do it with a simple Python script. Okay, JFK is an intense-looking airport. It looks like a beetle with giant pincers. Anyway, to see how easy these are to make, let’s take our Tasty Bytes data, and make a simple Streamlit app using Streamlit in Snowflake. To do this we can go over to Snowsight, click on Projects, and click on “Streamlit.” Then we click “+ Streamlit App,” and I’ll copy in some code. A few things to notice right away about the user interface – There’s a “RUN” button on the top right that we can use to manually trigger the app to rerun after we’ve made changes. There’s also a “Share” button in the top right that makes it easy to share your app with other Snowflake users in your account. Then at the top, there’s a little dropdown that lets you specify some app settings, like which warehouse you’re using. Okay, this is probably obvious, but on the right you’re seeing the rendered app, and you can scroll up and down and interact with it. Before we look at the code, let’s see what this app is doing. Let’s make it full screen for this part. So here we’re looking at daily Tasty Bytes food truck revenue for the city of San Mateo from 2020 to 2023. If we hover over the line chart, we can see the total sum of the orders for that day. We can also toggle the chart to see the raw data instead, so total order amounts per day, or we can see the SQL query that providing this data. You’ll also notice that we can add in other cities through the City dropdown. Let’s add in Berlin and Boston. We can also update the date range. Now everything we’re doing here is just very standard Streamlit functionality, and there’s so, so much more we could do to customize this. For example, we could build multi-page apps, or make the look of the app match your company’s branding. But if you’ve ever built even an app as straightforward as this one, you know there’s actually a lot going on here behind the scenes to get this to work so smoothly. If we instead look at the code, we can see the magic happening. At the top, we’re importing the libraries we’ll use to make the app work. I want to call attention to the “import streamlit as st” line, since you’ll see st functions peppered throughout the script. The first thing the app does is create a session object using “get_active_session().” Among other things, this lets us interact with our Snowflake data in the app, and one reason to use Streamlit in Snowflake is the low latency you get from bringing your code so close to the data. After that we see our first ST dot something – In this case, st.title. This creates a markdown-style title. And if you haven’t heard of markdown, it’s a markup language that lets you format text. So let’s change the title a bit so we can see how the app re-renders quickly to let us see the effect of the change. Let’s make “tasty,” “tastiest,” and let’s change the first emoji to a smiley face. You can see that the next command is ST dot write, and that corresponds to the text we’re seeing in the app below the title. And then we have ST dot divider, which creates this little line, which is there for styling. If we delete that, the line will go away. I like it better with the line, so let’s put that back. Next, we’ll see how the different pieces of the app come together to make the final product.

2. Let's practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.