Spaces:
Sleeping
Sleeping
# Code Generated by Sidekick is for learning and experimentation purposes only. | |
import streamlit as st | |
from datetime import date, timedelta | |
import matplotlib.pyplot as plt | |
import matplotlib.patches as patches | |
from matplotlib.ticker import PercentFormatter | |
import plotly.graph_objs as go | |
def main(): | |
st.set_page_config(page_title="Event", layout="wide") | |
st.title("When is my Event") | |
# Session state management | |
if "sidebar_submitted" not in st.session_state: | |
st.session_state.sidebar_submitted = False | |
if "start_date" not in st.session_state: | |
st.session_state.start_date = date.today() | |
if "event_day" not in st.session_state: | |
st.session_state.event_day = date.today() + timedelta(days=30) | |
# Sidebar inputs | |
if not st.session_state.sidebar_submitted: | |
with st.sidebar: | |
st.header("Setup Event Tracking") | |
start_date = st.date_input( | |
"Select a date from when you want to start tracking", | |
value=st.session_state.start_date, | |
min_value=date(2020, 1, 1), | |
max_value=date(2030, 12, 31) | |
) | |
event_day = st.date_input( | |
"Event Day", | |
value=st.session_state.event_day, | |
min_value=date(2020, 1, 1), | |
max_value=date(2030, 12, 31) | |
) | |
if st.button("Submit"): | |
st.session_state.start_date = start_date | |
st.session_state.event_day = event_day | |
st.session_state.sidebar_submitted = True | |
# Main app logic here... | |
if st.session_state.sidebar_submitted: | |
start_date = st.session_state.start_date | |
event_day = st.session_state.event_day | |
col_a, col_b, col_c, col_d, col_e = st.columns(5) | |
with col_a: | |
use_today = st.button("Today", key="today_btn") | |
with col_b: | |
selected_date = st.date_input( | |
"", | |
value=date.today(), | |
min_value=start_date, | |
max_value=event_day, | |
label_visibility="collapsed", | |
key="main_date_input" | |
) | |
# Determine which date to use | |
if use_today: | |
calc_date = date.today() | |
else: | |
calc_date = selected_date | |
# Only proceed if a date is selected or Today is pressed | |
if use_today or selected_date: | |
total_day_duration = (event_day - start_date).days | |
today_date = calc_date | |
today_formatted_date = today_date.strftime("%d-%b-%Y") | |
new_year_formatted_date = event_day.strftime("%d-%b-%Y") | |
total_day_duration_remain = (event_day - today_date).days | |
monday1 = (today_date - timedelta(days=today_date.weekday())) | |
monday2 = (event_day - timedelta(days=event_day.weekday())) | |
weeks_remain = (monday2 - monday1).days / 7 | |
total_remain = round((total_day_duration_remain / total_day_duration) * 100, 2) | |
percent_complete = ((total_day_duration - total_day_duration_remain) / total_day_duration) * 100 | |
percent_remaining = 100 - percent_complete | |
with st.container(): | |
col1, col2 = st.columns(2) | |
with col1: | |
st.metric(label="Selected Date", value=str(today_formatted_date)) | |
with col2: | |
st.metric(label="Event Date", value=str(new_year_formatted_date)) | |
with st.container(): | |
fig, ax = plt.subplots(figsize=(10, 1)) | |
ax.barh(0, percent_complete, color='green', label='Elapsed') | |
border = patches.Rectangle( | |
(0, -0.4), 100, 0.8, linewidth=2, edgecolor='white', facecolor='none' | |
) | |
ax.add_patch(border) | |
ax.set_xticks(range(0, 101, 10)) | |
ax.xaxis.set_major_formatter(PercentFormatter()) | |
ax.tick_params(axis='x', colors='white', labelsize=8) | |
ax.get_yaxis().set_visible(False) | |
ax.spines['left'].set_visible(False) | |
ax.spines['right'].set_visible(False) | |
ax.spines['top'].set_visible(False) | |
ax.spines['bottom'].set_visible(False) | |
ax.tick_params(axis='x', which='both', length=0) | |
fig.patch.set_alpha(0) | |
ax.patch.set_alpha(0) | |
st.pyplot(fig) | |
with st.container(): | |
col2, col3, col4 = st.columns(3) | |
with col2: | |
st.metric(label="Total Days left is", value=int(total_day_duration_remain)) | |
with col3: | |
st.metric(label="Total Week left is", value=int(weeks_remain)) | |
with col4: | |
st.metric(label="Total Days left is %", value=float(total_remain)) | |
with st.container(): | |
col1, col2 = st.columns(2) | |
with col1: | |
color = "green" | |
bmi_class = 'Days Remaining' | |
fig1 = go.Figure(go.Indicator( | |
mode="gauge+number", | |
value=total_day_duration_remain, | |
domain={'x': [0, 1], 'y': [0, 1]}, | |
title={'text': "Days Remaining in No of Days"}, | |
number={'font': {'color': color}, 'suffix': f"<br>{bmi_class}"}, | |
gauge={'axis': {'range': [total_day_duration, 0]}, | |
'bar': {'color': "green", 'thickness': 1}, | |
'threshold': {'line': {'color': "black", 'width': 10}, 'thickness': 1, 'value': total_day_duration_remain}} | |
)) | |
fig1.update_layout(plot_bgcolor='rgba(0,0,0,0)', showlegend=False, | |
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False), | |
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)) | |
col1.plotly_chart(fig1) | |
with col2: | |
color = "green" | |
bmi_class = '% Remaining' | |
fig2 = go.Figure(go.Indicator( | |
mode="gauge+number", | |
value=total_remain, | |
domain={'x': [0, 1], 'y': [0, 1]}, | |
title={'text': "Days Remaining in %"}, | |
number={'font': {'color': color}, 'suffix': f"<br>{bmi_class}"}, | |
gauge={'axis': {'range': [100, 0]}, | |
'bar': {'color': "green", 'thickness': 1}, | |
'threshold': {'line': {'color': "black", 'width': 10}, 'thickness': 1, 'value': total_remain}} | |
)) | |
fig2.update_layout(plot_bgcolor='rgba(0,0,0,0)', showlegend=False, | |
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False), | |
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)) | |
col2.plotly_chart(fig2) | |
if __name__ == '__main__': | |
main() | |