-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamlit_app.py
176 lines (149 loc) · 5.37 KB
/
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
"""
****************************************************************************************************
__ _ _ _
/ _| | | | | | |
| |_ _ __ ___ ___| |_| |__ _ _| |_ ___
| _| '__/ _ \/ __| __| '_ \| | | | __/ _ \
| | | | | (_) \__ \ |_| |_) | |_| | || __/
|_| |_| \___/|___/\__|_.__/ \__, |\__\___|
__/ |
|___/
Quickstart: Tasty Bytes - Snowpark 101 for Data Science
Script: streamlit_app.py
Create Date: 2023-05-19
Author: Marie Coolsaet
Copyright(c): 2023 Snowflake Inc. All rights reserved.
****************************************************************************************************
Description:
A Streamlit app for surfacing predicted shift sales for locations where truck drivers can park.
****************************************************************************************************
SUMMARY OF CHANGES
Date(yyyy-mm-dd) Author Comments
------------------- ------------------- ------------------------------------------------------------
2023-05-19 Marie Coolsaet Initial Quickstart Release
****************************************************************************************************
"""
# Import Python packages
import streamlit as st
import plotly.express as px
import json
# Import Snowflake modules
from snowflake.snowpark import Session
import snowflake.snowpark.functions as F
from snowflake.snowpark import Window
# Set Streamlit page config
st.set_page_config(
page_title="Streamlit App: Snowpark 101",
page_icon=":truck:",
layout="wide",
)
# Add header and a subheader
st.header("Predicted Shift Sales by Location")
st.subheader("Data-driven recommendations for food truck drivers.")
# Refresh Snowflake session after 60 minutes
@st.cache_resource(ttl=3600)
def init_connection():
# Get account credentials from a json file
with open("data_scientist_auth.json") as f:
data = json.load(f)
username = data["username"]
password = data["password"]
account = data["account"]
# Specify connection parameters
connection_parameters = {
"account": account,
"user": username,
"password": password,
"role": "accountadmin",
"warehouse": "tasty_dsci_wh",
"database": "frostbyte_tasty_bytes_dev",
"schema": "analytics",
}
# Create Snowpark session
return Session.builder.configs(connection_parameters).create()
# Connect to Snowflake
session = init_connection()
# Create input widgets for cities and shift
with st.container():
col1, col2 = st.columns(2)
with col1:
# Drop down to select city
city = st.selectbox(
"City:",
session.table("frostbyte_tasty_bytes_dev.analytics.shift_sales_v")
.select("city")
.distinct()
.sort("city"),
)
with col2:
# Select AM/PM Shift
shift = st.radio("Shift:", ("AM", "PM"), horizontal=True)
# Get predictions for city and shift time
def get_predictions(city, shift):
# Get data and filter by city and shift
snowpark_df = session.table(
"frostbyte_tasty_bytes_dev.analytics.shift_sales_v"
).filter((F.col("shift") == shift) & (F.col("city") == city))
# Get rolling average
window_by_location_all_days = (
Window.partition_by("location_id")
.order_by("date")
.rows_between(Window.UNBOUNDED_PRECEDING, Window.CURRENT_ROW - 1)
)
snowpark_df = snowpark_df.with_column(
"avg_location_shift_sales",
F.avg("shift_sales").over(window_by_location_all_days),
).cache_result()
# Get tomorrow's date
date_tomorrow = (
snowpark_df.filter(F.col("shift_sales").is_null())
.select(F.min("date"))
.collect()[0][0]
)
# Filter to tomorrow's date
snowpark_df = snowpark_df.filter(F.col("date") == date_tomorrow)
# Impute
snowpark_df = snowpark_df.fillna(value=0, subset=["avg_location_shift_sales"])
# Encode
snowpark_df = snowpark_df.with_column("shift", F.iff(F.col("shift") == "AM", 1, 0))
# Define feature columns
feature_cols = [
"MONTH",
"DAY_OF_WEEK",
"LATITUDE",
"LONGITUDE",
"CITY_POPULATION",
"AVG_LOCATION_SHIFT_SALES",
"SHIFT",
]
# Call the inference user-defined function
snowpark_df = snowpark_df.select(
"location_id",
"latitude",
"longitude",
"avg_location_shift_sales",
F.call_udf(
"udf_linreg_predict_location_sales", [F.col(c) for c in feature_cols]
).alias("predicted_shift_sales"),
)
return snowpark_df.to_pandas()
# Update predictions and plot when the "Update" button is clicked
if st.button("Update"):
# Get predictions
with st.spinner("Getting predictions..."):
predictions = get_predictions(city, shift)
# Plot on a map
predictions["PREDICTED_SHIFT_SALES"].clip(0, inplace=True)
fig = px.scatter_mapbox(
predictions,
lat="LATITUDE",
lon="LONGITUDE",
hover_name="LOCATION_ID",
size="PREDICTED_SHIFT_SALES",
color="PREDICTED_SHIFT_SALES",
zoom=8,
height=800,
width=1000,
)
fig.update_layout(mapbox_style="open-street-map")
st.plotly_chart(fig, use_container_width=True)