Created
December 12, 2024 16:01
-
-
Save izderadicka/890f64251c883c6bd2105f45b3c7444f to your computer and use it in GitHub Desktop.
More fancy streamlit example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import streamlit as st | |
import pandas as pd | |
import numpy as np | |
import plotly.express as px | |
st.set_page_config(layout="wide") | |
DATE_COLUMN = 'date/time' | |
DATA_URL = ('https://s3-us-west-2.amazonaws.com/' | |
'streamlit-demo-data/uber-raw-data-sep14.csv.gz') | |
@st.cache_data | |
def load_data(nrows): | |
data = pd.read_csv(DATA_URL, nrows=nrows) | |
lowercase = lambda x: str(x).lower() | |
data.rename(lowercase, axis='columns', inplace=True) | |
data[DATE_COLUMN] = pd.to_datetime(data[DATE_COLUMN]) | |
return data | |
with st.spinner(text="In progress"): | |
data = load_data(10000) | |
min_date = data[DATE_COLUMN].min() | |
max_date = data[DATE_COLUMN].max() | |
st.title('Uber pickups in NYC from %s to %s' % (min_date, max_date)) | |
if st.checkbox('Show raw data'): | |
st.subheader('Raw data') | |
st.write(data) | |
hist_values = np.histogram( | |
data[DATE_COLUMN].dt.hour, bins=24, range=(0,24))[0] | |
col1, col2 = st.columns(2) | |
with col1: | |
st.subheader('Histogram of pickups by hour') | |
st.write('Click on bar to select, click again to unselect') | |
fig = px.bar(y=hist_values, labels={'y':'Count', 'x': 'Hour'}) | |
fig.update_layout(clickmode='event+select') | |
fig.update_xaxes(dtick=1) | |
selection=st.plotly_chart( | |
fig, | |
on_select="rerun", | |
selection_mode='points' | |
) | |
hour_to_filter=None | |
if selection and selection['selection']['points']: | |
hour_to_filter=selection['selection']['points'][0]['x'] | |
# st.write(f"You selected: {selection['selection']['points'][0]['x']}") | |
with col2: | |
if hour_to_filter is None: | |
st.subheader('Map of all pickups') | |
filtered_data = data | |
else: | |
filtered_data = data[data[DATE_COLUMN].dt.hour == hour_to_filter] | |
st.subheader(f'Map of all pickups at {hour_to_filter}:00') | |
st.map(filtered_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment