-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamlit_deploy.py
165 lines (138 loc) · 5.58 KB
/
streamlit_deploy.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
import altair as alt
import pandas as pd
import streamlit as st
st.title("Interactive CommVis by Jeff Cardwell")
all_recipes_df = pd.read_csv('allrecipes.csv')
all_recipes_df = all_recipes_df.drop(columns=['Unnamed: 0', 'footnotes', 'error', 'url', 'photo_url', 'instructions'])
ingredient_dist_df = pd.read_csv('ingredient_dist.csv')
ingredient_dist_df = ingredient_dist_df.drop(columns='Unnamed: 0')
adaptations_df = pd.read_csv('adaptations.csv')
adaptations_df= adaptations_df.drop(columns='Unnamed: 0')
def make_vis1():
ingredient = []
count = []
trad = []
top_non_trad_ingredients = []
non_trad_df = ingredient_dist_df.loc[ingredient_dist_df['Traditional']==0]
for i in non_trad_df.sort_values('Count', ascending=False)['Ingredient'][:41]:
top_non_trad_ingredients.append(i)
top_non_trad_ingredients.remove('saffron thread')
top_non_trad_ingredients.remove('onion')
top_non_trad_ingredients.remove('lemon wedges')
top_non_trad_ingredients.remove('kosher salt')
top_non_trad_ingredients.remove('black pepper')
top_non_trad_ingredients.remove('green peas')
for i, r in ingredient_dist_df.iterrows():
if r['Traditional'] == 1:
ingredient.append(r['Ingredient'])
count.append(r['Count'])
trad.append('Yes')
else:
if r['Ingredient'] in top_non_trad_ingredients:
ingredient.append(r['Ingredient'])
count.append(r['Count'])
trad.append('No')
limited_ing_dist_df = pd.DataFrame()
limited_ing_dist_df['Ingredient'] = ingredient
limited_ing_dist_df['Count'] = count
limited_ing_dist_df['Traditional'] = trad
trad = list(limited_ing_dist_df['Traditional'].unique())
radio_sel = alt.selection_multi(
fields = ['Traditional'],
bind = 'legend')
int_sel = alt.selection_interval(empty = 'all')
display_cond = alt.condition(
int_sel,
alt.SizeValue(30),
alt.SizeValue(0)
)
bar = alt.Chart(limited_ing_dist_df, title = {'text':"Traditional Paella Is About What's NOT There", 'subtitle':"Select a Value in the Legend to Filter Chart"}, width = 400).mark_bar().encode(
x=alt.X('Count',
title= 'Number of Recipes with Ingredient'),
y=alt.Y('Ingredient',
sort = alt.SortField(field='Count', order='descending')),
color=alt.Color(
'Traditional:N',
scale=alt.Scale(
domain=['Yes', 'No'],
range=["red", "gold"]
))
).add_selection(
radio_sel
).transform_filter(radio_sel)
return bar
def make_vis2():
pts = alt.selection(type="single", encodings=['x'])
point = alt.Chart(all_recipes_df).mark_point().transform_filter(
alt.datum['review_count'] > 0
).encode(
alt.X('rating_stars:Q', bin=False, title='Rating'),
alt.Y('review_count:Q', bin=False, title='Number of Reviews'),
color = alt.value('red'),
tooltip = ['title', 'Paella type', 'rating_stars', 'review_count']
).properties(
width=600,
height=300
).transform_filter(pts)
bar = alt.Chart(all_recipes_df, title = {'text':"Which Types of Paella are the Most Popular?", 'subtitle':"Select a Paella Type to Filter Chart"}).mark_bar().encode(
x=alt.X('Paella type:N',
title=None,
axis=alt.Axis(labelAngle=0, labelFontStyle='Bold')),
y=alt.Y('mean_rating:Q',
title='Average Rating'),
color=alt.condition(pts, alt.ColorValue("red"), alt.ColorValue("gold"))
).transform_aggregate(
mean_rating = 'mean(rating_stars)',
groupby=['Paella type']
).properties(
width=600,
height=300
).add_selection(pts)
concat = alt.vconcat(
bar,
point
).resolve_legend(
color="independent",
size="independent"
).configure_point(size=100)
return concat
def make_vis3():
brush = alt.selection(type='interval', encodings = ['x'])
points = alt.Chart(all_recipes_df, title={'text':"Does Quality Require More Patience?", 'subtitle':"How does average cook time change for different rating groups?"}, width=600, height = 400).mark_point(filled = True).transform_filter(
(alt.datum['rating_stars'] > 0) & (alt.datum['total_time_minutes']>0)
).encode(
x=alt.X('rating_stars:Q',
title='Rating'),
y=alt.Y('total_time_minutes:Q',
title='Total Cook Time'),
color = alt.condition(brush, alt.value('gold'), alt.value('grey')),
tooltip = ['title', 'Paella type', 'rating_stars', 'review_count'],
opacity=alt.condition(brush, alt.OpacityValue(1), alt.OpacityValue(0.7)),
).add_selection(
brush
)
line = alt.Chart(all_recipes_df).mark_rule(color='red').transform_filter(
(alt.datum['rating_stars'] > 0) & (alt.datum['total_time_minutes']>0)
).encode(
y='mean(total_time_minutes):Q',
tooltip = 'mean(total_time_minutes):Q',
size=alt.SizeValue(3)
).transform_filter(
brush
)
layer = alt.layer(points, line).configure_point(size=70)
return layer
viz1 = make_vis1()
viz2 = make_vis2()
viz3 = make_vis3()
selectbox_options = ['vis1', 'vis2', 'vis3']
selectbox = st.sidebar.selectbox(
label='Select Visualization',
options = selectbox_options
)
if selectbox == 'vis1':
viz1
elif selectbox == 'vis2':
viz2
elif selectbox == 'vis3':
viz3