File size: 1,822 Bytes
adf30a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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