Spaces:
Sleeping
Sleeping
NAME
commited on
Commit
•
c62c311
1
Parent(s):
679a628
in class multipage
Browse files- Home.py +40 -0
- README.md +1 -1
- pages/1_Data_Introduction.py +35 -0
- pages/2_Widget_Exploration.py +130 -0
- inClass_script_week10.py → pages/3_Altair_Plots.py +8 -52
- pages/4_Other_Tools.py +0 -0
- requirements.txt +2 -1
Home.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# we will often times use an app.py instead of a workbook so here is just a placeholder for that kind of file as well!
|
2 |
+
import streamlit as st
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import pandas as pd
|
5 |
+
|
6 |
+
st.set_page_config(
|
7 |
+
page_title='Hello',
|
8 |
+
page_icon=":anchor:"
|
9 |
+
)
|
10 |
+
|
11 |
+
st.title('This is my fancy app!')
|
12 |
+
|
13 |
+
st.header("This is a header")
|
14 |
+
st.subheader('This is a subheader')
|
15 |
+
|
16 |
+
st.text('This is some text.')
|
17 |
+
|
18 |
+
|
19 |
+
# 1. Layout elements
|
20 |
+
col1,col2 = st.columns(2)
|
21 |
+
col1.write('This is thing 1')
|
22 |
+
col2.write('This is thing 2')
|
23 |
+
|
24 |
+
# 2. Images
|
25 |
+
st.subheader('Images')
|
26 |
+
st.image('https://i.redd.it/on-a-scale-of-1-10-how-derpy-is-she-v0-z8gtdwu5n5zb1.jpg?width=3024&format=pjpg&auto=webp&s=345e7e1d5b45f20c733e497a9f746f4cbd3a61da',
|
27 |
+
width=200,
|
28 |
+
caption='A thinly veiled excuse for a cute corg!')
|
29 |
+
|
30 |
+
|
31 |
+
import numpy as np
|
32 |
+
|
33 |
+
img_data = np.random.random((200,200))
|
34 |
+
st.image(img_data,
|
35 |
+
caption='Random numpy data')
|
36 |
+
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
|
README.md
CHANGED
@@ -5,7 +5,7 @@ colorFrom: green
|
|
5 |
colorTo: gray
|
6 |
sdk: streamlit
|
7 |
sdk_version: 1.39.0
|
8 |
-
app_file:
|
9 |
pinned: false
|
10 |
license: mit
|
11 |
---
|
|
|
5 |
colorTo: gray
|
6 |
sdk: streamlit
|
7 |
sdk_version: 1.39.0
|
8 |
+
app_file: Home.py
|
9 |
pinned: false
|
10 |
license: mit
|
11 |
---
|
pages/1_Data_Introduction.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
st.set_page_config(
|
4 |
+
page_title='Introduction to Data',
|
5 |
+
page_icon=":1234:"
|
6 |
+
)
|
7 |
+
|
8 |
+
st.title('Info about our Dataset')
|
9 |
+
|
10 |
+
mobility_url = 'https://raw.githubusercontent.com/UIUC-iSchool-DataViz/is445_data/main/mobility.csv'
|
11 |
+
|
12 |
+
|
13 |
+
# Read in data with pandas
|
14 |
+
import pandas as pd
|
15 |
+
|
16 |
+
df = pd.read_csv(mobility_url)
|
17 |
+
|
18 |
+
#df
|
19 |
+
st.write(df)
|
20 |
+
|
21 |
+
import matplotlib.pyplot as plt
|
22 |
+
fig, ax = plt.subplots()
|
23 |
+
df['Seg_income'].plot(kind='hist',ax=ax)
|
24 |
+
#plt.show() # need to use streamlit infrastructure for this
|
25 |
+
st.pyplot(fig)
|
26 |
+
|
27 |
+
st.write("""Note I have added some things to
|
28 |
+
the requirements.txt file. """)
|
29 |
+
st.code("""
|
30 |
+
streamlit==1.39.0
|
31 |
+
altair
|
32 |
+
numpy
|
33 |
+
matplotlib
|
34 |
+
pandas
|
35 |
+
""")
|
pages/2_Widget_Exploration.py
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
|
6 |
+
|
7 |
+
st.set_page_config(
|
8 |
+
page_title='Widgets',
|
9 |
+
page_icon=":1234:"
|
10 |
+
)
|
11 |
+
|
12 |
+
mobility_url = 'https://raw.githubusercontent.com/UIUC-iSchool-DataViz/is445_data/main/mobility.csv'
|
13 |
+
df = pd.read_csv(mobility_url)
|
14 |
+
|
15 |
+
|
16 |
+
|
17 |
+
st.title("Widgets in Streamlit apps")
|
18 |
+
|
19 |
+
st.markdown("""Using markdown for a reminder, we can use
|
20 |
+
widgets in Streamlit (similar to `ipywidgets`).
|
21 |
+
""")
|
22 |
+
|
23 |
+
st.subheader('Feedback Widget')
|
24 |
+
|
25 |
+
st.markdown("""We could try the
|
26 |
+
[feedback widget](https://docs.streamlit.io/develop/api-reference/widgets/st.feedback).
|
27 |
+
""")
|
28 |
+
|
29 |
+
st.markdown("Using the following code:")
|
30 |
+
st.code("""
|
31 |
+
sentiment_mapping = ["one", "two", "three", "four", "five"]
|
32 |
+
selected = st.feedback("stars")
|
33 |
+
if selected is not None:
|
34 |
+
st.markdown(f"You selected {sentiment_mapping[selected]} star(s).")
|
35 |
+
""")
|
36 |
+
|
37 |
+
# sentiment_mapping = ["one", "two", "three", "four", "five"]
|
38 |
+
# selected = st.feedback("stars")
|
39 |
+
# if selected is not None:
|
40 |
+
# st.markdown(f"You selected {sentiment_mapping[selected]} star(s).")
|
41 |
+
|
42 |
+
st.write("How are you feeling right now?")
|
43 |
+
sentiment_mapping = ["one", "two", "three", "four", "five"]
|
44 |
+
selected = st.feedback("stars")
|
45 |
+
if selected is not None: # only run if a star is selected
|
46 |
+
if selected < 1:
|
47 |
+
st.markdown("Sorry to hear you are so sad :(")
|
48 |
+
elif selected < 3:
|
49 |
+
st.markdown("A solid medium is great!")
|
50 |
+
else:
|
51 |
+
st.markdown("Fantastic to hear you are having a great day!")
|
52 |
+
|
53 |
+
st.subheader("Connecting Widgets to Plots")
|
54 |
+
|
55 |
+
st.markdown("We'll start with a static plot:")
|
56 |
+
|
57 |
+
# bins along student to teacher ratio
|
58 |
+
bins = np.linspace(df['Student_teacher_ratio'].min(),
|
59 |
+
df['Student_teacher_ratio'].max(), 10)
|
60 |
+
#bins # note -- this will be "pandas-like" in view
|
61 |
+
|
62 |
+
table = df.pivot_table(index='State',
|
63 |
+
columns=pd.cut(df['Student_teacher_ratio'],bins),
|
64 |
+
aggfunc='size')
|
65 |
+
|
66 |
+
fig, ax = plt.subplots()
|
67 |
+
extent = [bins.min(),bins.max(), 0, len(table.index)] # xmin, xmax, ymin, ymax
|
68 |
+
ax.imshow(table.values, cmap='hot', interpolation='nearest',extent=extent)
|
69 |
+
ax.set_yticks(range(len(table.index)))
|
70 |
+
ax.set_yticklabels(table.index)
|
71 |
+
st.pyplot(fig)
|
72 |
+
|
73 |
+
# trick for imshow command -- save as buffer so that its not so
|
74 |
+
# big and we can format the size the way we want
|
75 |
+
# (might not need to do this for all plots)
|
76 |
+
|
77 |
+
from io import BytesIO
|
78 |
+
fig, ax = plt.subplots(figsize=(4,8))
|
79 |
+
extent = [bins.min(),bins.max(), 0, len(table.index)] # xmin, xmax, ymin, ymax
|
80 |
+
ax.imshow(table.values, cmap='hot', interpolation='nearest',extent=extent)
|
81 |
+
ax.set_yticks(range(len(table.index)))
|
82 |
+
ax.set_yticklabels(table.index)
|
83 |
+
#st.pyplot(fig)
|
84 |
+
|
85 |
+
buf = BytesIO()
|
86 |
+
fig.tight_layout()
|
87 |
+
fig.savefig(buf, format='png')
|
88 |
+
st.image(buf, width=500)
|
89 |
+
|
90 |
+
st.markdown("""Let's make use of a
|
91 |
+
[multiselect widget](https://docs.streamlit.io/develop/api-reference/widgets/st.multiselect) """)
|
92 |
+
|
93 |
+
fig_col, controls_col = st.columns([2,1],
|
94 |
+
vertical_alignment='center')
|
95 |
+
|
96 |
+
states_selected = controls_col.multiselect("Which states do you want to view", table.index.values)
|
97 |
+
|
98 |
+
if len(states_selected) > 0:
|
99 |
+
df_subset = df[df['State'].isin(states_selected)]
|
100 |
+
#st.write(df_subset) # used to debug our selection
|
101 |
+
table_subset = df_subset.pivot_table(index='State',
|
102 |
+
columns=pd.cut(df_subset['Student_teacher_ratio'],bins),
|
103 |
+
aggfunc='size')
|
104 |
+
fig, ax = plt.subplots(figsize=(4,8))
|
105 |
+
extent = [bins.min(),bins.max(), 0, len(table_subset.index)] # xmin, xmax, ymin, ymax
|
106 |
+
ax.imshow(table_subset.values, cmap='hot', interpolation='nearest',extent=extent)
|
107 |
+
ax.set_yticks(range(len(table_subset.index)))
|
108 |
+
ax.set_yticklabels(table_subset.index)
|
109 |
+
#st.pyplot(fig)
|
110 |
+
|
111 |
+
buf = BytesIO()
|
112 |
+
fig.tight_layout()
|
113 |
+
fig.savefig(buf, format='png')
|
114 |
+
#st.image(buf, width=500)
|
115 |
+
fig_col.image(buf, width=400)
|
116 |
+
else:
|
117 |
+
#st.write(df) # used to debug selection
|
118 |
+
#pass
|
119 |
+
fig, ax = plt.subplots(figsize=(4,8))
|
120 |
+
extent = [bins.min(),bins.max(), 0, len(table.index)] # xmin, xmax, ymin, ymax
|
121 |
+
ax.imshow(table.values, cmap='hot', interpolation='nearest',extent=extent)
|
122 |
+
ax.set_yticks(range(len(table.index)))
|
123 |
+
ax.set_yticklabels(table.index)
|
124 |
+
#st.pyplot(fig)
|
125 |
+
|
126 |
+
buf = BytesIO()
|
127 |
+
fig.tight_layout()
|
128 |
+
fig.savefig(buf, format='png')
|
129 |
+
#st.image(buf, width=500)
|
130 |
+
fig_col.image(buf, width=400)
|
inClass_script_week10.py → pages/3_Altair_Plots.py
RENAMED
@@ -1,37 +1,18 @@
|
|
1 |
-
# we will often times use an app.py instead of a workbook so here is just a placeholder for that kind of file as well!
|
2 |
import streamlit as st
|
|
|
|
|
3 |
|
4 |
-
st.
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
st.text('This is some text.')
|
10 |
-
|
11 |
-
|
12 |
-
# 1. Layout elements
|
13 |
-
col1,col2 = st.columns(2)
|
14 |
-
col1.write('This is thing 1')
|
15 |
-
col2.write('This is thing 2')
|
16 |
-
|
17 |
-
# 2. Images
|
18 |
-
st.subheader('Images')
|
19 |
-
st.image('https://i.redd.it/on-a-scale-of-1-10-how-derpy-is-she-v0-z8gtdwu5n5zb1.jpg?width=3024&format=pjpg&auto=webp&s=345e7e1d5b45f20c733e497a9f746f4cbd3a61da',
|
20 |
-
width=200,
|
21 |
-
caption='A thinly veiled excuse for a cute corg!')
|
22 |
-
|
23 |
-
|
24 |
-
import numpy as np
|
25 |
-
|
26 |
-
img_data = np.random.random((200,200))
|
27 |
-
st.image(img_data,
|
28 |
-
caption='Random numpy data')
|
29 |
|
30 |
st.header('Altair in Streamlit')
|
31 |
-
import altair as alt
|
32 |
|
33 |
mobility_url = 'https://raw.githubusercontent.com/UIUC-iSchool-DataViz/is445_data/main/mobility.csv'
|
34 |
|
|
|
|
|
35 |
scatters = alt.Chart(mobility_url).mark_point().encode(
|
36 |
x = 'Mobility:Q',
|
37 |
y=alt.Y('Population:Q', scale=alt.Scale(type='log')),
|
@@ -51,29 +32,4 @@ col2.markdown("Here is some text on the side of the plot.")
|
|
51 |
col2.image('https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQAXjp6mzNlkMEEiMomUfTEMiX8NHpopoyyXg&s')
|
52 |
|
53 |
|
54 |
-
st.header("Day 2 (Week 11)")
|
55 |
-
|
56 |
-
# Read in data with pandas
|
57 |
-
import pandas as pd
|
58 |
-
df = pd.read_csv(mobility_url)
|
59 |
-
|
60 |
-
#df
|
61 |
-
st.write(df)
|
62 |
-
|
63 |
-
import matplotlib.pyplot as plt
|
64 |
-
fig, ax = plt.subplots()
|
65 |
-
df['Seg_income'].plot(kind='hist',ax=ax)
|
66 |
-
#plt.show() # need to use streamlit infrastructure for this
|
67 |
-
st.pyplot(fig)
|
68 |
-
|
69 |
-
st.write("""Note I have added some things to
|
70 |
-
the requirements.txt file. """)
|
71 |
-
st.code("""
|
72 |
-
streamlit==1.39.0
|
73 |
-
altair
|
74 |
-
numpy
|
75 |
-
matplotlib
|
76 |
-
pandas
|
77 |
-
""")
|
78 |
|
79 |
-
st.header("Widgets in Streamlit apps")
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import altair as alt
|
3 |
+
import pandas as pd
|
4 |
|
5 |
+
st.set_page_config(
|
6 |
+
page_title='Altair',
|
7 |
+
page_icon=":1234:"
|
8 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
st.header('Altair in Streamlit')
|
|
|
11 |
|
12 |
mobility_url = 'https://raw.githubusercontent.com/UIUC-iSchool-DataViz/is445_data/main/mobility.csv'
|
13 |
|
14 |
+
df = pd.read_csv(mobility_url)
|
15 |
+
|
16 |
scatters = alt.Chart(mobility_url).mark_point().encode(
|
17 |
x = 'Mobility:Q',
|
18 |
y=alt.Y('Population:Q', scale=alt.Scale(type='log')),
|
|
|
32 |
col2.image('https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQAXjp6mzNlkMEEiMomUfTEMiX8NHpopoyyXg&s')
|
33 |
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
|
pages/4_Other_Tools.py
ADDED
File without changes
|
requirements.txt
CHANGED
@@ -2,4 +2,5 @@ streamlit==1.39.0
|
|
2 |
altair
|
3 |
numpy
|
4 |
matplotlib
|
5 |
-
pandas
|
|
|
|
2 |
altair
|
3 |
numpy
|
4 |
matplotlib
|
5 |
+
pandas
|
6 |
+
io
|