I’m trying to build a fairly basic app. That fetches new data periodically in a dataframe, and displays it on the dashboard.
Everytime I select something on the dashboard, the entire page reloads. How do i fix this?
import datetime as dt
import numpy as np
import streamlit as st
import plotly.express as px
import pandas as pd
st.set_page_config(layout='wide')
def streamlit_placeholder():
st.title('some title')
st.text("write something cool here")
left, right = st.columns((0.5, 0.5))
with left:
station_selector = st.empty()
with right:
x = st.date_input(label='foobar',
min_value=dt.datetime.today() + dt.timedelta(days=-1),
max_value=dt.datetime.today() + dt.timedelta(days=5*365),
value=(dt.datetime.today(), dt.datetime.today() + dt.timedelta(days=5*365 - 1))
)
required_start, required_end= x
placeholder = st.empty()
st.markdown('***')
return placeholder, left, station_selector, required_start, required_end
def fake_dataset():
columns=['station-1','station-2','yr-station-1','yr-station-2']
# Fake Data
df = pd.DataFrame({
"sensor_name": columns*25,
"readings": np.random.randint(20, 30, size=100),
"reading_date": np.repeat(pd.date_range(end='8/8/2022', periods=25), 4)
})
return df
def placeholder_fix(fake_data, placeholder, required_start, required_end):
with placeholder.container():
sensor_mask = fake_data['sensor_name'].str.contains(pat=st.session_state['station-selector'])
date_mask = (fake_data['reading_date'].dt.date >= required_start) & (fake_data['reading_date'].dt.date <= required_end)
data_to_plot = fake_data[sensor_mask & date_mask]
fig = px.line(data_to_plot, x="reading_date", y="readings", color="sensor_name", title="Temperature (\u00B0C) readings")
fig.update_traces(mode="markers+lines", hovertemplate=None)
fig.update_layout(hovermode="x unified")
fig['layout']['xaxis']['title']='Date'
fig['layout']['yaxis']['title']="Temperature (\u00B0C)"
st.plotly_chart(fig, use_container_width=True)
st.dataframe(data_to_plot)
def set_station_selector(left, station_selector, stations):
with left:
with station_selector.container():
select_station = st.selectbox('station', stations, key='station-selector')
placeholder, left, station_selector, required_start, required_end= streamlit_placeholder()
options = ['1','2']
set_station_selector(left, station_selector, options)
def main():
options = ['1','2']
fake_data = fake_dataset()
print ("why does it come here again!?")
if set(options) != set(fake_data['sensor_name'].unique()):
print ("here")
set_station_selector(left, station_selector, fake_data['sensor_name'].unique())
options = fake_data['sensor_name'].unique()
placeholder_fix(fake_data, placeholder, required_start, required_end)