-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamlit_app.py
361 lines (253 loc) Β· 9.32 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import numpy as np
import altair as alt
import pandas as pd
import streamlit as st
from datetime import time, datetime
import time as tm
import os
from io import StringIO
# st.set_page_config(layout="wide")
# Day 2
st.header("Day 2: Hello, World!")
# Day 3
st.header('Day 3: st.button')
if st.button('Say hello'):
st.write('Why hello there')
else:
st.write('Goodbye')
# Day 5
st.header('Day 5: st.write')
# Example 1
st.write('Hello, *World!* :sunglasses:')
# Example 2
st.write(1234)
# Example 3
df = pd.DataFrame({
'first column': [1, 2, 3, 4],
'second column': [10, 20, 30, 40]
})
st.write(df)
# Example 4
st.write('Below is a DataFrame:', df, 'Above is a dataframe.')
# Example 5
df2 = pd.DataFrame(
np.random.randn(200, 3),
columns=['x', 'y', 'z'])
st.write('This is the dataframe for graph', df2)
c = alt.Chart(df2).mark_circle().encode(
x='x', y='y', size='z', color='z', tooltip=['x', 'y', 'z'])
st.write(c)
# Day 8
st.header('Day 8: st.slider')
# Example 1
st.subheader('slider')
age = st.slider('How old are you?', 0, 100, 20)
st.write("I'm ", age, 'years old')
# Example 2
st.subheader('Range slider')
values = st.slider(
'Select a range of values',
0.0, 100.0, (25.0, 75.0))
st.write('Values:', values)
# Example 3
st.subheader('Range time slider')
appointment = st.slider(
"Schedule your appointment:",
value=(time(10, 00), time(10, 30)))
st.write("You're scheduled for:", appointment)
# Example 4
st.subheader('Datetime slider')
start_time = st.slider(
"When do you start?",
value=datetime(2020, 1, 1, 9, 30),
format="YYYY/MM/DD - hh:mm")
st.write("Start time:", start_time)
# Example 5
st.subheader('Color slider')
color = st.select_slider(
'Select a color of the rainbow',
options=['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'])
st.write('My favorite color is', color)
start_color, end_color = st.select_slider(
'Select a range of color wavelength',
options=['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'],
value=('red', 'blue'))
st.write('You selected wavelengths between', start_color, 'and', end_color)
st.header('Day 9: Line chart')
chart_data = pd.DataFrame(
np.random.randn(20, 3),
columns=['a', 'b', 'c'])
st.line_chart(chart_data)
st.header('Day 10: st.selectbox')
option = st.selectbox(
'What is your favorite color?',
('Blue', 'Red', 'Green'))
st.write('Your favorite color is ', option)
st.header('Day 11: st.multiselect')
options = st.multiselect(
'What are your favorite colors',
['Green', 'Yellow', 'Red', 'Blue'],
['Yellow', 'Red'])
st.write('You selected:', options)
st.header('Day 12: st.checkbox')
st.write ('What would you like to order?')
icecream = st.checkbox('Ice cream')
coffee = st.checkbox('Coffee')
cola = st.checkbox('Cola')
if icecream:
st.write("Great! Here's some more π¦")
if coffee:
st.write("Okay, here's some coffee β")
if cola:
st.write("Here you go π₯€")
# import pandas_profiling
# from streamlit_pandas_profiling import st_profile_report
# st.header('Day 14: `streamlit_pandas_profiling`')
# df = pd.read_csv('https://raw.githubusercontent.com/dataprofessor/data/master/penguins_cleaned.csv')
# pr = df.profile_report()
# st_profile_report(pr)
st.header('Day 15: st.latex')
st.latex(r'''
a + ar + a r^2 + a r^3 + \cdots + a r^{n-1} =
\sum_{k=0}^{n-1} ar^k =
a \left(\frac{1-r^{n}}{1-r}\right)
''')
st.title('Day 16: Customizing the theme of Streamlit apps')
st.write('Contents of the `.streamlit/config.toml` file of this app')
st.code("""
[theme]
primaryColor="#FE9C12"
backgroundColor="#000000"
secondaryBackgroundColor="#6E6EFE"
textColor="#FFFFFF"
font="monospace"
""")
number = st.sidebar.slider('Select a number:', 0, 10, 5)
st.write('Selected number from slider widget is:', number)
st.title('Day17: st.secrets')
st.write('This is my secret gitHub username:', st.secrets['GitHub_username'])
# st.write(
# "Has environment variables been set:",
# os.environ["gitHub_username"] == st.secrets["gitHub_username"],
# )
st.title('Day18: st.file_uploader')
st.subheader('Input CSV')
uploaded_files = st.file_uploader("Choose a file", accept_multiple_files=True)
for uploaded_file in uploaded_files:
if uploaded_file is not None:
df = pd.read_csv(uploaded_file)
st.subheader('DataFrame')
st.write(df)
st.subheader('Descriptive Statistics')
st.write(df.describe())
else:
st.info('βοΈ Upload a CSV file')
# uploaded_file = st.file_uploader("Choose a file")
# if uploaded_file is not None:
# # To read file as bytes:
# bytes_data = uploaded_file.getvalue()
# st.write(bytes_data)
# # To convert to a string based IO:
# stringio = StringIO(uploaded_file.getvalue().decode("utf-8"))
# st.write(stringio)
# # To read file as string:
# string_data = stringio.read()
# st.write(string_data)
# # Can be used wherever a "file-like" object is accepted:
# dataframe = pd.read_csv(uploaded_file)
# st.write(dataframe)
# uploaded_files = st.file_uploader("Choose a CSV file", accept_multiple_files=True)
# for uploaded_file in uploaded_files:
# bytes_data = uploaded_file.read()
# st.write("filename:", uploaded_file.name)
# st.write(bytes_data)
st.title('Day 19: How to layout your Streamlit app')
with st.expander('About this app'):
st.write('This app shows the various ways on how you can layout your Streamlit app.')
st.image('https://streamlit.io/images/brand/streamlit-logo-secondary-colormark-darktext.png', width=250)
st.sidebar.header('Input')
user_name = st.sidebar.text_input('What is your name?')
user_emoji = st.sidebar.selectbox('Choose an emoji', ['', 'π', 'π', 'π', 'π', 'π΄', 'π', 'π±'])
user_food = st.sidebar.selectbox('What is your favorite food?', ['', 'Tom Yum Kung', 'Burrito', 'Lasagna', 'Hamburger', 'Pizza'])
st.header('Output')
col1, col2, col3 = st.columns(3)
with col1:
if user_name != '':
st.write(f'π Hello {user_name}!')
else:
st.write('π Please enter your **name**!')
with col2:
if user_emoji != '':
st.write(f'{user_emoji} is your favorite **emoji**!')
else:
st.write('π Please choose an **emoji**!')
with col3:
if user_food != '':
st.write(f'π΄ **{user_food}** is your favorite **food**!')
else:
st.write('π Please choose your favorite **food**!')
st.title('Day 21: st.progress')
with st.expander('About this app'):
st.write('You can now display the progress of your calculations in a Streamlit app with the `st.progress` command.')
progress_text = "Operation in progress. Please wait."
my_bar = st.progress(0, text=progress_text)
for percent_complete in range(100):
tm.sleep(0.01)
my_bar.progress(percent_complete + 1, text=progress_text)
tm.sleep(1)
my_bar.empty()
st.balloons()
st.button("Rerun")
st.title('Day 22: st.form')
# Full example of using the with notation
st.header('1. Example of using `with` notation')
st.subheader('Coffee machine')
with st.form('my_form'):
st.subheader('**Order your coffee**')
# Input widgets
coffee_bean_val = st.selectbox('Coffee bean', ['Arabica', 'Robusta'])
coffee_roast_val = st.selectbox('Coffee roast', ['Light', 'Medium', 'Dark'])
brewing_val = st.selectbox('Brewing method', ['Aeropress', 'Drip', 'French press', 'Moka pot', 'Siphon'])
serving_type_val = st.selectbox('Serving format', ['Hot', 'Iced', 'Frappe'])
milk_val = st.select_slider('Milk intensity', ['None', 'Low', 'Medium', 'High'])
owncup_val = st.checkbox('Bring own cup')
# Every form must have a submit button
submitted = st.form_submit_button('Submit')
if submitted:
st.markdown(f'''
β You have ordered:
- Coffee bean: `{coffee_bean_val}`
- Coffee roast: `{coffee_roast_val}`
- Brewing: `{brewing_val}`
- Serving type: `{serving_type_val}`
- Milk: `{milk_val}`
- Bring own cup: `{owncup_val}`
''')
else:
st.write('βοΈ Place your order!')
# Short example of using an object notation
st.header('2. Example of object notation')
form = st.form('my_form_2')
selected_val = form.slider('Select a value')
form.form_submit_button('Submit')
st.write('Selected value: ', selected_val)
# st.title('Day 23: st.experimental_get_query_params')
# with st.expander('About this app'):
# st.write("`st.experimental_get_query_params` allows the retrieval of query parameters directly from the URL of the user's browser.")
# # 1. Instructions
# st.header('1. Instructions')
# st.markdown('''
# In the above URL bar of your internet browser, append the following:
# `?firstname=Jack&surname=Beanstalk`
# after the base URL `http://share.streamlit.io/dataprofessor/st.experimental_get_query_params/`
# such that it becomes
# `http://share.streamlit.io/dataprofessor/st.experimental_get_query_params/?firstname=Jack&surname=Beanstalk`
# ''')
# # 2. Contents of st.experimental_get_query_params
# st.header('2. Contents of st.experimental_get_query_params')
# st.write(st.experimental_get_query_params())
# # 3. Retrieving and displaying information from the URL
# st.header('3. Retrieving and displaying information from the URL')
# firstname = st.experimental_get_query_params()['firstname'][0]
# surname = st.experimental_get_query_params()['surname'][0]
# st.write(f'Hello **{firstname} {surname}**, how are you?')