Spaces:
Sleeping
Sleeping
words
Browse files- Hello.py +85 -0
- app.py +0 -432
- images/clone_the_repo.png +0 -0
- images/duplicateSpace_p1.png +0 -0
- images/duplicateSpace_p2.png +0 -0
- images/gitclone_hf.png +0 -0
Hello.py
CHANGED
|
@@ -8,3 +8,88 @@ st.set_page_config(
|
|
| 8 |
|
| 9 |
st.sidebar.success("Select a Page")
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
st.sidebar.success("Select a Page")
|
| 10 |
|
| 11 |
+
st.title('Getting Setup for Streamlit Spaces')
|
| 12 |
+
|
| 13 |
+
st.markdown("""
|
| 14 |
+
## Step 1: Install what you need for this notebook
|
| 15 |
+
|
| 16 |
+
It is recommended you install into a conda environment:
|
| 17 |
+
|
| 18 |
+
```
|
| 19 |
+
conda create -n DataVizClass python=3.10
|
| 20 |
+
conda activate DataVizClass
|
| 21 |
+
```
|
| 22 |
+
|
| 23 |
+
Then you can install the correct packages.
|
| 24 |
+
|
| 25 |
+
```
|
| 26 |
+
pip install streamlit==1.39.0 altair numpy pandas matplotlib
|
| 27 |
+
```
|
| 28 |
+
|
| 29 |
+
To work with the VSCode interface, be sure that `jupyter` is also installed:
|
| 30 |
+
|
| 31 |
+
```
|
| 32 |
+
pip install jupyter
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
Or you can install with `conda`.
|
| 36 |
+
|
| 37 |
+
Note that the package [Streamlit](https://streamlit.io/) that we will be working with requires we use Python scripts, so JupyterLab and/or Jupyter Notebooks won't work for this process. """)
|
| 38 |
+
|
| 39 |
+
st.markdown("""
|
| 40 |
+
## Step 2: Clone the App files -- Option 1 (HuggingFace)
|
| 41 |
+
""")
|
| 42 |
+
|
| 43 |
+
st.markdown("""
|
| 44 |
+
### Step 2.1: Duplicate the HuggingFace App
|
| 45 |
+
""")
|
| 46 |
+
|
| 47 |
+
st.markdown("""
|
| 48 |
+
If you want to be able to deploy your own app on your own HuggingFace account,
|
| 49 |
+
you first need to duplicate this space.
|
| 50 |
+
|
| 51 |
+
To duplicate, right click on the 3 dots to the left of your profile icon: """)
|
| 52 |
+
|
| 53 |
+
st.image("images/duplicateSpace_p1.png")
|
| 54 |
+
|
| 55 |
+
st.markdown("""
|
| 56 |
+
Make sure your user name is selected, and it is set to `Public` for visibility. Name the space whatever you want:
|
| 57 |
+
""")
|
| 58 |
+
|
| 59 |
+
st.image("images/duplicateSpace_p2.png")
|
| 60 |
+
|
| 61 |
+
st.markdown("""
|
| 62 |
+
### Step 2.2: Clone *your duplicated* Space
|
| 63 |
+
""")
|
| 64 |
+
|
| 65 |
+
st.markdown("""
|
| 66 |
+
First, be sure that `git-lfs` is installed, and then clone *your* duplicated space repo.
|
| 67 |
+
|
| 68 |
+
You can find instructions for this by once again right-clicking the 3 dots and clicking on "Clone Repository":
|
| 69 |
+
""")
|
| 70 |
+
|
| 71 |
+
st.image("images/clone_the_repo.png")
|
| 72 |
+
|
| 73 |
+
st.markdown("""
|
| 74 |
+
Then you will be given this set of instructions (note your repo name will be different!):
|
| 75 |
+
""")
|
| 76 |
+
|
| 77 |
+
st.image("images/gitclone_hf.png")
|
| 78 |
+
|
| 79 |
+
st.markdown("""
|
| 80 |
+
""")
|
| 81 |
+
|
| 82 |
+
st.markdown("""
|
| 83 |
+
Now you should have a repository folder on your local computer that has your repo name and the files for this app.
|
| 84 |
+
""")
|
| 85 |
+
|
| 86 |
+
st.markdown("""
|
| 87 |
+
## Step 2: Clone the App files -- Option 2 (GitHub)
|
| 88 |
+
""")
|
| 89 |
+
|
| 90 |
+
st.markdown("""
|
| 91 |
+
If you don't want to mess around with HuggingFace and just want to play around with Streamlit locally,
|
| 92 |
+
you can also download these files from GitHub by [cloning this repository right here](https://github.com/jnaiman/shadisClassApp).
|
| 93 |
+
|
| 94 |
+
For info about using git commands to clone a repository, [see this link right here](https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository).
|
| 95 |
+
""")
|
app.py
DELETED
|
@@ -1,432 +0,0 @@
|
|
| 1 |
-
|
| 2 |
-
#######################################################
|
| 3 |
-
# 1. Getting setup -- using our HF template
|
| 4 |
-
#######################################################
|
| 5 |
-
|
| 6 |
-
# We have a few options for how to proceed. I'll start by showing the process in
|
| 7 |
-
# PL and then I'll move to my local installation of my template so that I can make
|
| 8 |
-
# sure I am pushing code at various intervals so folks can check that out.
|
| 9 |
-
|
| 10 |
-
# NOTE: during this process, you can click on "Always Rerun" for automatic updates.
|
| 11 |
-
|
| 12 |
-
# See the class notes on this with some photos for reference!
|
| 13 |
-
# **this has to be implemented!**
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
###################################################################
|
| 17 |
-
# 2. Review of where we got to last time, in template app.py file
|
| 18 |
-
###################################################################
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
# Let's start by copying things we did last time
|
| 22 |
-
import streamlit as st
|
| 23 |
-
import altair as alt
|
| 24 |
-
|
| 25 |
-
# Let's recall a plot that we made with Altair in Jupyterlab:
|
| 26 |
-
# Make sure we copy the URL as well!
|
| 27 |
-
mobility_url = 'https://raw.githubusercontent.com/UIUC-iSchool-DataViz/is445_data/main/mobility.csv'
|
| 28 |
-
|
| 29 |
-
st.title('This is my fancy app for HuggingFace!!')
|
| 30 |
-
|
| 31 |
-
scatters = alt.Chart(mobility_url).mark_point().encode(
|
| 32 |
-
x='Mobility:Q', # "Q for quantiative"
|
| 33 |
-
#y='Population:Q',
|
| 34 |
-
y=alt.Y('Population:Q', scale=alt.Scale(type='log')),
|
| 35 |
-
color=alt.Color('Income:Q', scale=alt.Scale(scheme='sinebow'),bin=alt.Bin(maxbins=5))
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
-
st.header('More complex Dashboards')
|
| 39 |
-
|
| 40 |
-
brush = alt.selection_interval(encodings=['x','y'])
|
| 41 |
-
|
| 42 |
-
chart1 = alt.Chart(mobility_url).mark_rect().encode(
|
| 43 |
-
alt.X("Student_teacher_ratio:Q", bin=alt.Bin(maxbins=10)),
|
| 44 |
-
alt.Y("State:O"),
|
| 45 |
-
alt.Color("count()")
|
| 46 |
-
).properties(
|
| 47 |
-
height=400
|
| 48 |
-
).add_params(
|
| 49 |
-
brush
|
| 50 |
-
)
|
| 51 |
-
|
| 52 |
-
chart2 = alt.Chart(mobility_url).mark_bar().encode(
|
| 53 |
-
alt.X("Mobility:Q", bin=True,axis=alt.Axis(title='Mobility Score')),
|
| 54 |
-
alt.Y('count()', axis=alt.Axis(title='Mobility Score Distribution'))
|
| 55 |
-
).transform_filter(
|
| 56 |
-
brush
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
chart = (chart1.properties(width=300) | chart2.properties(width=300))
|
| 60 |
-
|
| 61 |
-
tab1, tab2 = st.tabs(["Mobility interactive", "Scatter plot"])
|
| 62 |
-
|
| 63 |
-
with tab1:
|
| 64 |
-
st.altair_chart(chart, theme=None, use_container_width=True)
|
| 65 |
-
with tab2:
|
| 66 |
-
st.altair_chart(scatters, theme=None, use_container_width=True)
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
################################################
|
| 70 |
-
# 3. Adding features, Pushing to HF
|
| 71 |
-
################################################
|
| 72 |
-
|
| 73 |
-
st.header('Requirements, README file, Pushing to HuggingFace')
|
| 74 |
-
|
| 75 |
-
### 3.1 Make a plot ###
|
| 76 |
-
|
| 77 |
-
# Let's say we want to add in some matplotlib plots from some data we read
|
| 78 |
-
# in with Pandas.
|
| 79 |
-
|
| 80 |
-
import pandas as pd
|
| 81 |
-
df = pd.read_csv(mobility_url)
|
| 82 |
-
|
| 83 |
-
# There are a few ways to show the dataframe if we want our viewer to see the table:
|
| 84 |
-
#df
|
| 85 |
-
st.write(df)
|
| 86 |
-
|
| 87 |
-
# Now, let's plot with matplotlib:
|
| 88 |
-
import matplotlib.pyplot as plt
|
| 89 |
-
|
| 90 |
-
fig, ax = plt.subplots()
|
| 91 |
-
df['Seg_income'].plot(kind='hist', ax=ax)
|
| 92 |
-
#plt.show() # but wait! this doesn't work!
|
| 93 |
-
|
| 94 |
-
# We need to use the streamlit-specific way of showing matplotlib plots: https://docs.streamlit.io/develop/api-reference/charts/st.pyplot
|
| 95 |
-
st.pyplot(fig)
|
| 96 |
-
|
| 97 |
-
### 3.2 Push these changes to HF -- requirements.txt ###
|
| 98 |
-
# In order to push these changes to HF and have things actually show up we need to
|
| 99 |
-
# add the packages we've added to our requirements.txt file.
|
| 100 |
-
|
| 101 |
-
st.write('''The requirements.txt file contains all the packages needed
|
| 102 |
-
for our app to run. These include (for our application):''')
|
| 103 |
-
st.code('''
|
| 104 |
-
streamlit==1.39.0
|
| 105 |
-
altair
|
| 106 |
-
numpy
|
| 107 |
-
pandas
|
| 108 |
-
matplotlib
|
| 109 |
-
''')
|
| 110 |
-
|
| 111 |
-
# NOTE: for any package you want to use in your app.py file, you must include it in
|
| 112 |
-
# the requirements.txt file!
|
| 113 |
-
|
| 114 |
-
# Note #2: we specified a version of streamlit so we can use some specific widgets
|
| 115 |
-
|
| 116 |
-
### 3.3 Push these changes to HF -- README.md ###
|
| 117 |
-
|
| 118 |
-
# While we're doing this, let's also take a look at the README.md file!
|
| 119 |
-
|
| 120 |
-
st.header('Build in HF: README.md & requirements.txt files')
|
| 121 |
-
|
| 122 |
-
st.code('''
|
| 123 |
-
---
|
| 124 |
-
title: Prep notebook -- My Streamlit App
|
| 125 |
-
emoji: 🏢
|
| 126 |
-
colorFrom: blue
|
| 127 |
-
colorTo: gray
|
| 128 |
-
sdk: streamlit
|
| 129 |
-
sdk_version: 1.39.0
|
| 130 |
-
app_file: app.py
|
| 131 |
-
pinned: false
|
| 132 |
-
license: mit
|
| 133 |
-
---
|
| 134 |
-
''')
|
| 135 |
-
st.write("Note: the sdk version has to match what is in your requirements.txt (and with whatever widgets you want to be able to use).")
|
| 136 |
-
|
| 137 |
-
# Some important things to note here:
|
| 138 |
-
|
| 139 |
-
st.write('Some important items to note about these:')
|
| 140 |
-
st.markdown('''
|
| 141 |
-
* the "emoji" is what will show up as an identifier on your homepage
|
| 142 |
-
* the sdk *must* be streamlit
|
| 143 |
-
* the "app_file" *must* link to the app file you are developing in
|
| 144 |
-
''')
|
| 145 |
-
|
| 146 |
-
################################################
|
| 147 |
-
# 4. TODO Quick intro to widgets
|
| 148 |
-
################################################
|
| 149 |
-
|
| 150 |
-
st.header('Widgets in Streamlit apps')
|
| 151 |
-
|
| 152 |
-
### 4.1 Widget basics: A few widget examples ###
|
| 153 |
-
|
| 154 |
-
st.markdown("""
|
| 155 |
-
These will be very similar to how we used the `ipywidgets` package in Jupyter notebooks.
|
| 156 |
-
""")
|
| 157 |
-
|
| 158 |
-
st.markdown("""
|
| 159 |
-
We won't go over all of them, but you can check out the [list of widgets](https://docs.streamlit.io/develop/api-reference/widgets)
|
| 160 |
-
linked.
|
| 161 |
-
""")
|
| 162 |
-
|
| 163 |
-
st.markdown("""Let's try a few!""")
|
| 164 |
-
|
| 165 |
-
st.subheader('Feedback Widget')
|
| 166 |
-
|
| 167 |
-
st.markdown("""
|
| 168 |
-
For example, we could try the [feedback widget](https://docs.streamlit.io/develop/api-reference/widgets/st.feedback).
|
| 169 |
-
"""
|
| 170 |
-
)
|
| 171 |
-
st.markdown("""
|
| 172 |
-
If we check out the docs for this widget, we see some familiar looking functions like
|
| 173 |
-
`on_change` and the example they give looks very similar to an
|
| 174 |
-
"observation" function that we built before using widgets:
|
| 175 |
-
""")
|
| 176 |
-
|
| 177 |
-
st.code(
|
| 178 |
-
"""
|
| 179 |
-
sentiment_mapping = ["one", "two", "three", "four", "five"]
|
| 180 |
-
selected = st.feedback("stars")
|
| 181 |
-
if selected is not None:
|
| 182 |
-
st.markdown(f"You selected {sentiment_mapping[selected]} star(s).")
|
| 183 |
-
""")
|
| 184 |
-
|
| 185 |
-
# Let's give this a shot!
|
| 186 |
-
|
| 187 |
-
st.write("How great are you feeling right now?")
|
| 188 |
-
sentiment_mapping = ["one", "two", "three", "four", "five"] # map to these numers
|
| 189 |
-
selected = st.feedback("stars")
|
| 190 |
-
if selected is not None: # make sure we have a selection
|
| 191 |
-
st.markdown(f"You selected {sentiment_mapping[selected]} star(s).")
|
| 192 |
-
if selected < 1:
|
| 193 |
-
st.markdown('Sorry to hear you are so sad :(')
|
| 194 |
-
elif selected < 3:
|
| 195 |
-
st.markdown('A solid medium is great!')
|
| 196 |
-
else:
|
| 197 |
-
st.markdown('Fantastic you are having such a great day!')
|
| 198 |
-
|
| 199 |
-
st.subheader('Radio Buttons')
|
| 200 |
-
|
| 201 |
-
st.markdown("""
|
| 202 |
-
Let's try out a [radio button](https://docs.streamlit.io/develop/api-reference/widgets/st.radio) example.
|
| 203 |
-
""")
|
| 204 |
-
|
| 205 |
-
favoriteViz = st.radio(
|
| 206 |
-
"What's your visualization tool so far?",
|
| 207 |
-
[":rainbow[Streamlit]", "vega-lite :sparkles:", "matplotlib :material/Home:"],
|
| 208 |
-
captions=[
|
| 209 |
-
"New and cool!",
|
| 210 |
-
"So sparkly.",
|
| 211 |
-
"Familiar and comforting.",
|
| 212 |
-
],
|
| 213 |
-
)
|
| 214 |
-
|
| 215 |
-
if favoriteViz == ":rainbow[Streamlit]":
|
| 216 |
-
st.write("You selected Streamlit!")
|
| 217 |
-
else:
|
| 218 |
-
st.write("You didn't select Streamlit but that's ok, Data Viz still likes you :grin:")
|
| 219 |
-
|
| 220 |
-
st.markdown("""
|
| 221 |
-
Note here that we made use of text highlight [colors](https://docs.streamlit.io/develop/api-reference/text/st.markdown)
|
| 222 |
-
and [emoji's](https://streamlit-emoji-shortcodes-streamlit-app-gwckff.streamlit.app/)
|
| 223 |
-
and [icons](https://fonts.google.com/icons?icon.set=Material+Symbols&icon.style=Rounded).
|
| 224 |
-
""")
|
| 225 |
-
|
| 226 |
-
### 4.2 Connecting widgets with plots ###
|
| 227 |
-
|
| 228 |
-
st.subheader('Connecting Widgets and Plots')
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
st.markdown("""
|
| 232 |
-
There are actually [many types of charts](https://docs.streamlit.io/develop/api-reference/charts)
|
| 233 |
-
supported in Streamlit (including the Streamlit-based "Simple Charts"),
|
| 234 |
-
though we will just mainly be focusing on [Altair-related](https://docs.streamlit.io/develop/api-reference/charts/st.altair_chart) plots
|
| 235 |
-
and their interactivity options since we'll also be making use of these when
|
| 236 |
-
we move to building Jekyll webpages.
|
| 237 |
-
""")
|
| 238 |
-
|
| 239 |
-
st.markdown("""Since `matplotlib` is relatively familiar though, let's do a quick
|
| 240 |
-
example using `pandas` and `matplotlib` to plot as
|
| 241 |
-
Streamlit [does support `matplotlib`](https://docs.streamlit.io/develop/api-reference/charts/st.pyplot)
|
| 242 |
-
as a plotting engine. """)
|
| 243 |
-
|
| 244 |
-
st.markdown("""First, let's just make a simple plot with `pandas` and `matplotlib`.
|
| 245 |
-
Let's re-do the matplotlib plots we did before with the mobility dataset
|
| 246 |
-
with some interactivity. """)
|
| 247 |
-
|
| 248 |
-
import pandas as pd
|
| 249 |
-
import numpy as np
|
| 250 |
-
|
| 251 |
-
# first, let's make a static plot:
|
| 252 |
-
st.write("We'll start with a static plot:")
|
| 253 |
-
# read in dataset
|
| 254 |
-
df = pd.read_csv("https://raw.githubusercontent.com/UIUC-iSchool-DataViz/is445_data/main/mobility.csv")
|
| 255 |
-
|
| 256 |
-
# make bins along student-teacher ratio
|
| 257 |
-
bins = np.linspace(df['Student_teacher_ratio'].min(),df['Student_teacher_ratio'].max(), 10)
|
| 258 |
-
|
| 259 |
-
# make pivot table
|
| 260 |
-
table = df.pivot_table(index='State', columns=pd.cut(df['Student_teacher_ratio'], bins), aggfunc='size')
|
| 261 |
-
|
| 262 |
-
# our plotting code before was:
|
| 263 |
-
st.code("""
|
| 264 |
-
import matplotlib.pyplot as plt
|
| 265 |
-
|
| 266 |
-
fig,ax = plt.subplots(figsize=(10,8))
|
| 267 |
-
ax.imshow(table.values, cmap='hot', interpolation='nearest')
|
| 268 |
-
ax.set_yticks(range(len(table.index)))
|
| 269 |
-
ax.set_yticklabels(table.index)
|
| 270 |
-
plt.show()
|
| 271 |
-
""")
|
| 272 |
-
|
| 273 |
-
st.write("Let's translate it into something that will work with Streamlit:")
|
| 274 |
-
|
| 275 |
-
import matplotlib.pyplot as plt
|
| 276 |
-
|
| 277 |
-
fig,ax = plt.subplots() # this changed
|
| 278 |
-
ax.imshow(table.values, cmap='hot', interpolation='nearest')
|
| 279 |
-
ax.set_yticks(range(len(table.index)))
|
| 280 |
-
ax.set_yticklabels(table.index)
|
| 281 |
-
|
| 282 |
-
st.pyplot(fig) # this is different
|
| 283 |
-
|
| 284 |
-
st.markdown("""But this is too big! The trick is that we can save this as a buffer: """)
|
| 285 |
-
|
| 286 |
-
from io import BytesIO
|
| 287 |
-
|
| 288 |
-
fig,ax = plt.subplots(figsize=(4,8)) # this changed
|
| 289 |
-
ax.imshow(table.values, cmap='hot', interpolation='nearest')
|
| 290 |
-
ax.set_yticks(range(len(table.index)))
|
| 291 |
-
ax.set_yticklabels(table.index)
|
| 292 |
-
|
| 293 |
-
buf = BytesIO()
|
| 294 |
-
fig.tight_layout()
|
| 295 |
-
fig.savefig(buf, format="png")
|
| 296 |
-
st.image(buf, width = 500) # can mess around with width, figsize/etc
|
| 297 |
-
|
| 298 |
-
st.write("Now, let's make this interactive.")
|
| 299 |
-
st.markdown("""We'll first use the [multiselect](https://docs.streamlit.io/develop/api-reference/widgets/st.multiselect)
|
| 300 |
-
tool in order to allow for multiple state selection. """)
|
| 301 |
-
|
| 302 |
-
# vertical alignment so they end up side by side
|
| 303 |
-
fig_col, controls_col = st.columns([2,1], vertical_alignment='center')
|
| 304 |
-
|
| 305 |
-
# multi-select
|
| 306 |
-
states_selected = controls_col.multiselect('Which states do you want to view?', table.index.values)
|
| 307 |
-
|
| 308 |
-
if len(states_selected) > 0:
|
| 309 |
-
df_subset = df[df['State'].isin(states_selected)] # changed
|
| 310 |
-
|
| 311 |
-
# make pivot table -- changed
|
| 312 |
-
table_sub = df_subset.pivot_table(index='State',
|
| 313 |
-
columns=pd.cut(df_subset['Student_teacher_ratio'], bins),
|
| 314 |
-
aggfunc='size')
|
| 315 |
-
|
| 316 |
-
base_size = 4
|
| 317 |
-
# this resizing doesn't 100% work great
|
| 318 |
-
#factor = len(table.index)*1.0/df['State'].nunique()
|
| 319 |
-
#if factor == 0: factor = 1 # for non-selections
|
| 320 |
-
#fig,ax = plt.subplots(figsize=(base_size,2*base_size*factor)) # this changed too for different size
|
| 321 |
-
fig,ax = plt.subplots(figsize=(base_size,2*base_size)) # this changed too for different size
|
| 322 |
-
# extent is (xmin, xmax, ymax (buttom), ymin (top))
|
| 323 |
-
extent = [bins.min(), bins.max(), 0, len(table_sub.index)]
|
| 324 |
-
ax.imshow(table_sub.values, cmap='hot', interpolation='nearest',
|
| 325 |
-
extent=extent)
|
| 326 |
-
ax.set_yticks(range(len(table_sub.index)))
|
| 327 |
-
ax.set_yticklabels(table_sub.index)
|
| 328 |
-
#ax.set_xticklabels(bins)
|
| 329 |
-
|
| 330 |
-
buf = BytesIO()
|
| 331 |
-
fig.tight_layout()
|
| 332 |
-
fig.savefig(buf, format="png")
|
| 333 |
-
fig_col.image(buf, width = 400) # changed here to fit better
|
| 334 |
-
else:
|
| 335 |
-
fig,ax = plt.subplots(figsize=(4,8)) # this changed
|
| 336 |
-
extent = [bins.min(), bins.max(), 0, len(table.index)]
|
| 337 |
-
ax.imshow(table.values, cmap='hot', interpolation='nearest', extent=extent)
|
| 338 |
-
ax.set_yticks(range(len(table.index)))
|
| 339 |
-
ax.set_yticklabels(table.index)
|
| 340 |
-
#ax.set_xticklabels(bins)
|
| 341 |
-
|
| 342 |
-
buf = BytesIO()
|
| 343 |
-
fig.tight_layout()
|
| 344 |
-
fig.savefig(buf, format="png")
|
| 345 |
-
fig_col.image(buf, width = 500) # can mess around with width, figsize/etc
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
st.markdown("""
|
| 349 |
-
Now let's add more in by including a [range slider](https://docs.streamlit.io/develop/api-reference/widgets/st.slider)
|
| 350 |
-
widget.
|
| 351 |
-
""")
|
| 352 |
-
|
| 353 |
-
# vertical alignment so they end up side by side
|
| 354 |
-
fig_col2, controls_col2 = st.columns([2,1], vertical_alignment='center')
|
| 355 |
-
|
| 356 |
-
# multi-select
|
| 357 |
-
states_selected2 = controls_col2.multiselect('Which states do you want to view?',
|
| 358 |
-
table.index.values, key='unik1155')
|
| 359 |
-
# had to pass unique key to have double widgets with same value
|
| 360 |
-
|
| 361 |
-
# range slider -- added
|
| 362 |
-
student_teacher_ratio_range = controls_col2.slider("Range of student teacher ratio:",
|
| 363 |
-
df['Student_teacher_ratio'].min(),
|
| 364 |
-
df['Student_teacher_ratio'].max(),
|
| 365 |
-
(0.25*df['Student_teacher_ratio'].mean(),
|
| 366 |
-
0.75*df['Student_teacher_ratio'].mean()))
|
| 367 |
-
|
| 368 |
-
# note all the "2's" here, probably will just update the original one
|
| 369 |
-
if len(states_selected2) > 0: # here we set a default value for the slider, so no need to have a tag
|
| 370 |
-
min_range = student_teacher_ratio_range[0] # added
|
| 371 |
-
max_range = student_teacher_ratio_range[1] # added
|
| 372 |
-
|
| 373 |
-
df_subset2 = df[(df['State'].isin(states_selected2)) & (df['Student_teacher_ratio'] >= min_range) & (df['Student_teacher_ratio']<=max_range)] # changed
|
| 374 |
-
|
| 375 |
-
# just 10 bins over the full range --> changed
|
| 376 |
-
bins2 = 10 #np.linspace(df['Student_teacher_ratio'].min(),df['Student_teacher_ratio'].max(), 10)
|
| 377 |
-
|
| 378 |
-
# make pivot table -- changed
|
| 379 |
-
table_sub2 = df_subset2.pivot_table(index='State',
|
| 380 |
-
columns=pd.cut(df_subset2['Student_teacher_ratio'], bins2),
|
| 381 |
-
aggfunc='size')
|
| 382 |
-
|
| 383 |
-
base_size = 4
|
| 384 |
-
fig2,ax2 = plt.subplots(figsize=(base_size,2*base_size)) # this changed too for different size
|
| 385 |
-
extent2 = [df_subset2['Student_teacher_ratio'].min(),
|
| 386 |
-
df_subset2['Student_teacher_ratio'].max(),
|
| 387 |
-
0, len(table_sub2.index)]
|
| 388 |
-
ax2.imshow(table_sub2.values, cmap='hot', interpolation='nearest', extent=extent2)
|
| 389 |
-
ax2.set_yticks(range(len(table_sub2.index)))
|
| 390 |
-
ax2.set_yticklabels(table_sub2.index)
|
| 391 |
-
#ax2.set_xticklabels()
|
| 392 |
-
|
| 393 |
-
buf2 = BytesIO()
|
| 394 |
-
fig2.tight_layout()
|
| 395 |
-
fig2.savefig(buf2, format="png")
|
| 396 |
-
fig_col2.image(buf2, width = 400) # changed here to fit better
|
| 397 |
-
else:
|
| 398 |
-
min_range = student_teacher_ratio_range[0] # added
|
| 399 |
-
max_range = student_teacher_ratio_range[1] # added
|
| 400 |
-
|
| 401 |
-
df_subset2 = df[(df['Student_teacher_ratio'] >= min_range) & (df['Student_teacher_ratio']<=max_range)] # changed
|
| 402 |
-
|
| 403 |
-
# just 10 bins over the full range --> changed
|
| 404 |
-
bins2 = 10 #np.linspace(df['Student_teacher_ratio'].min(),df['Student_teacher_ratio'].max(), 10)
|
| 405 |
-
|
| 406 |
-
# make pivot table -- changed
|
| 407 |
-
table_sub2 = df_subset2.pivot_table(index='State',
|
| 408 |
-
columns=pd.cut(df_subset2['Student_teacher_ratio'], bins2),
|
| 409 |
-
aggfunc='size')
|
| 410 |
-
|
| 411 |
-
base_size = 4
|
| 412 |
-
fig2,ax2 = plt.subplots(figsize=(base_size,2*base_size)) # this changed too for different size
|
| 413 |
-
extent2 = [df_subset2['Student_teacher_ratio'].min(),
|
| 414 |
-
df_subset2['Student_teacher_ratio'].max(),
|
| 415 |
-
0, len(table_sub2.index)]
|
| 416 |
-
ax2.imshow(table_sub2.values, cmap='hot', interpolation='nearest', extent=extent2)
|
| 417 |
-
ax2.set_yticks(range(len(table_sub2.index)))
|
| 418 |
-
ax2.set_yticklabels(table_sub2.index)
|
| 419 |
-
#ax2.set_xticklabels()
|
| 420 |
-
|
| 421 |
-
buf2 = BytesIO()
|
| 422 |
-
fig2.tight_layout()
|
| 423 |
-
fig2.savefig(buf2, format="png")
|
| 424 |
-
fig_col2.image(buf2, width = 400) # changed here to fit better
|
| 425 |
-
|
| 426 |
-
st.header('Push final page to HF')
|
| 427 |
-
st.markdown("""When ready, do:""")
|
| 428 |
-
st.code("""
|
| 429 |
-
git add -A
|
| 430 |
-
git commit -m "final push of day 1"
|
| 431 |
-
git push
|
| 432 |
-
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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