ShadisClassExample / prep /pages /2_Widget_Exploration.py
jnaiman's picture
prep
adf30a2
raw
history blame
1.82 kB
import streamlit as st
st.set_page_config(page_title="Widget Exploration", page_icon=":1234:")
st.sidebar.header("Widget Exploration")
st.header("Simple Widgets")
st.write("How great are you feeling right now?")
sentiment_mapping = ["one", "two", "three", "four", "five"] # map to these numers
selected = st.feedback("stars")
if selected is not None: # make sure we have a selection
st.markdown(f"You selected {sentiment_mapping[selected]} star(s).")
if selected < 1:
st.markdown('Sorry to hear you are so sad :(')
elif selected < 3:
st.markdown('A solid medium is great!')
else:
st.markdown('Fantastic you are having such a great day!')
st.header("Tying Widgets to Pandas/Matplotlib Plots")
# we can also tie widget interaction to plots
# first, let's just try a simple plot with pandas
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("https://raw.githubusercontent.com/UIUC-iSchool-DataViz/is445_data/main/mobility.csv")
# let's first make a plot
fig,ax = plt.subplots() # this changed
df.groupby('State')['Mobility'].mean().plot(kind='bar',ax=ax)
st.pyplot(fig) # this is different
# now, let's remake this plot, but allow for some interactivity by only
# selecting a subset of states to plot
# multi-select
states_selected = st.multiselect('Which states do you want to view?', df['State'].unique())
if len(states_selected) > 0: # if selected
df_subset = df[df['State'].isin(states_selected)]
fig2,ax2 = plt.subplots() # this changed
df_subset.groupby('State')['Mobility'].mean().plot(kind='bar',ax=ax2)
st.pyplot(fig2) # this is different
else: # otherwise plot all states
fig2,ax2 = plt.subplots() # this changed
df.groupby('State')['Mobility'].mean().plot(kind='bar',ax=ax2)
st.pyplot(fig2) # this is different