Untitled

 avatar
unknown
python
2 years ago
2.5 kB
6
Indexable
import streamlit as st
import pandas as pd
import altair as alt
import numpy as np
from datetime import datetime
from datetime import date
from vega_datasets import data

st.header("Our First Application -- 2")
st.write("Hello World!")

@st.cache(allow_output_mutation=True)
def load_data():
    return pd.read_csv("covidcast-jhu-csse-confirmed_7dav_incidence_num-2020-03-01-to-2022-10-10.csv")

df = load_data()

df.loc[df['value'] < 0, 'value'] = 0

df_dates = df['time_value'].apply(lambda x: datetime.strptime(x, '%Y-%m-%d').date()).tolist()
df_dates = np.array(df_dates)

if st.checkbox("Show Raw Data"):
    st.write(df)

states = st.multiselect('State', df['geo_value'].unique())

labels = pd.Series([1] * len(df), index=df.index)

if states:
    labels &= df['geo_value'].isin(states)
    #st.write(df[labels])

    #value_chart = alt.Chart(df).mark_bar().encode(  
    #    y = 'value',
    #    x = alt.Y('time_value'),
        # color = alt.condition(race_brush, alt.value('steelblue'), alt.value('lightgray')),
    #)
    #st.altair_chart(value_chart)


min_date = min(df_dates)
max_date = max(df_dates)
chosen_time_period = st.slider('Date',
                       min_value=min_date,
                       max_value=max_date,
                       value=(min_date, max_date),
                       )
st.write("Start time:", chosen_time_period)


labels &= (df_dates > chosen_time_period[0]) & (df_dates < chosen_time_period[1])

value_chart = alt.Chart(df[labels]).mark_line().encode(  
        x = alt.X('time_value'),
        y = alt.Y('value'),
        color = 'geo_value',
    ).interactive().properties(
        width=1000,
        height=500
    )
st.altair_chart(value_chart)


states = alt.topo_feature(data.us_10m.url, feature='states')
map_chart = alt.Chart(states).mark_geoshape(
    fill='lightgray',
    stroke='white'
).project('albersUsa').properties(
    width=500,
    height=300
)
st.write(map_chart)
#data_period = df[date(df['time_value']) >= chosen_time_period[0]]


#if st.checkbox("Show periodic data"):
#    st.write(data_period)

# brush = alt.selection_interval(encodings=["x"])

# scatter = alt.Chart(df).mark_point().encode(
#     alt.X("Flipper Length (mm)", scale=alt.Scale(zero=False)),
#     alt.Y("Body Mass (g)", scale=alt.Scale(zero=False)),
#     alt.Color("Species")
# ).add_selection(brush)

# hist = alt.Chart(df).mark_bar().encode(
#     alt.X("Body Mass (g)", bin=True),
#     alt.Y("count()"),
#     alt.Color("Species")
# ).transform_filter(brush)

# st.write(scatter & hist)

Editor is loading...