Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import pandas as pd | |
| import plotly.express as px | |
| st.set_page_config(layout="wide") | |
| # Load the dataset | |
| data = pd.read_csv('manafeth_es_output.csv') | |
| # Remove commas and convert to numeric values for all columns except 'Date' | |
| data[data.columns.difference(['Date'])] = data[data.columns.difference(['Date'])].replace(',', '', regex=True).astype(float) | |
| # Convert 'Date' column to datetime | |
| data['Date'] = pd.to_datetime(data['Date']) | |
| # Set 'Date' column as the index | |
| data.set_index('Date', inplace=True) | |
| data.drop(data.index[-1], inplace=True) | |
| # Filter out rows beyond 2025 and drop them | |
| data.index = pd.to_datetime(data.index) | |
| #data = data[data.index.year <= 2025] | |
| #data.index = data.index.date | |
| # Set up the Streamlit app | |
| st.set_option('deprecation.showPyplotGlobalUse', False) | |
| # # Set the title of your Streamlit app | |
| # title = 'Dashboard' | |
| # # Set the URL of the logo image | |
| # logo_url = "https://i.imgur.com/x5OCu61.png" | |
| # # Create the HTML markup to display the logo | |
| # logo_html = f'<img src="{logo_url}" alt="Logo" style="vertical-align:middle; margin-right:10px; width:50px;">' | |
| # # Combine the logo HTML with the title text | |
| # title_html = f'{logo_html}<span style="font-size:32px;">{title}</span>' | |
| # # Render the title with logo using markdown | |
| # st.markdown(title_html, unsafe_allow_html=True) | |
| # Set the title of your Streamlit app | |
| title = 'Dashboard' | |
| # Set the URL of the logo image | |
| logo_url = "https://i.imgur.com/x5OCu61.png" | |
| # Set the URL of the additional image | |
| image_url = "https://i.imgur.com/xsTBjxI.png" | |
| # Create the HTML markup to display the logo | |
| logo_html = f'<img src="{logo_url}" alt="Logo" style="vertical-align:middle; margin-right:10px; width:50px;">' | |
| # Create the HTML markup to display the additional image | |
| image_html = f'<img src="{image_url}" alt="Image" style="vertical-align:middle; margin-left:625px; width:100px;">' | |
| # Combine the logo HTML, title text, and additional image HTML | |
| title_html = f'{logo_html}<span style="font-size:32px;">{title}</span>{image_html}' | |
| # Render the title with logo and additional image using markdown | |
| st.markdown(title_html, unsafe_allow_html=True) | |
| linewidths = 1300 | |
| tab1, tab2, tab3, corrs = st.tabs(["Dataset & Summary", "Plots", "Max Insights",'Borders Comparison & Correlation']) | |
| with tab1: | |
| # Display the dataset | |
| with st.container(): | |
| st.dataframe(data) | |
| # Show summary statistics | |
| summary = data.copy(deep=True) | |
| summary.drop('Month sum',axis=1,inplace=True) | |
| st.subheader('Summary Statistics') | |
| st.write(summary.describe()) | |
| import datetime | |
| status = True | |
| with tab2: | |
| st.title('All borders') | |
| # Year range selection using two dropdown boxes | |
| years = pd.unique(data.index.year) | |
| min_year = min(years) | |
| max_year = max(years) | |
| start_year = st.selectbox("Select Start Year", options=range(min_year, max_year + 1), index=0) | |
| end_year = st.selectbox("Select End Year", options=range(start_year, max_year + 1), index=len(range(start_year, max_year + 1))-1) | |
| # Convert start_year and end_year to datetime objects | |
| start_datetime = datetime.datetime(start_year, 1, 1) | |
| end_datetime = datetime.datetime(end_year, 12, 31) | |
| # Filter the data based on the selected year range | |
| filtered_data = data[(data.index >= start_datetime) & (data.index <= end_datetime)] | |
| with st.expander('All borders sum', expanded=status): | |
| fig = px.line(filtered_data, x=filtered_data.index, y=filtered_data.columns[-1], title='All borders ' + str(start_year) + '-' + str(end_year)) | |
| fig.update_layout(width=linewidths) | |
| #fig = px.line(data, y=data.columns[-1], color_discrete_sequence=['blue']) | |
| #fig.add_vrect(x0="2023-05", x1=data.index[-1], fillcolor="rgba(0, 255, 0, 0.3)", layer="below", line_width=0) | |
| st.plotly_chart(fig) | |
| # Calculate and display the sum of the selected duration | |
| sum_value = filtered_data.loc[start_datetime:end_datetime, filtered_data.columns[-1]].sum() | |
| sum_value = "{:,.0f}".format(sum_value) | |
| st.markdown(f"<p style='color:white;font-size:35px;'>Sum of selected duration: <span style='color:green'>{sum_value}</span></p>", unsafe_allow_html=True) | |
| st.title('Per border') | |
| for column in data.columns[:-1]: | |
| with st.expander(column, expanded=status): | |
| fig = px.line(filtered_data, x=filtered_data.index, y=column, title=column) | |
| fig.update_layout(width=linewidths) | |
| st.plotly_chart(fig) | |
| # Calculate and display the sum of the selected duration | |
| sum_value = filtered_data.loc[start_datetime:end_datetime, column].sum() | |
| #st.write('Sum of selected duration:', "{:,.0f}".format(sum_value)) | |
| sum_value = "{:,.0f}".format(sum_value) | |
| st.markdown(f"<p style='color:white;font-size:35px;'>Sum of selected duration: <span style='color:green'>{sum_value}</span></p>", unsafe_allow_html=True) | |
| #st.write(f"<p style='color:red;font-size:20px;'>Sum of selected duration: {"{:,.0f}".format(sum_value)}</p>", unsafe_allow_html=True) | |
| status = False | |
| with tab3: | |
| col1, col2 = st.columns(2) | |
| with st.container(): | |
| st.subheader('Stacked Borders') | |
| # Group data by year and calculate the sum for each border | |
| yearly_data = summary.groupby(summary.index.year).sum() | |
| # Stacked bar chart showing the difference between borders for each year | |
| fig = px.bar(yearly_data, x=yearly_data.index, y=yearly_data.columns, barmode='stack', | |
| title='Stacked Borders by Year') | |
| fig.update_layout(width=linewidths) | |
| st.plotly_chart(fig) | |
| with st.container(): | |
| max_values = summary.max() | |
| # Converting max_values to a DataFrame with a single column | |
| max_values_df = pd.DataFrame(max_values, columns=['Maximum Value']) | |
| # Sorting the DataFrame in descending order | |
| max_values_sorted = max_values_df.sort_values(by='Maximum Value', ascending=False) | |
| # Plotting the maximum values over time in descending order | |
| st.subheader('Top borders') | |
| fig = px.bar(max_values_sorted, x=max_values_sorted.index, y='Maximum Value', | |
| labels={'x': 'Columns', 'y': 'Maximum Value'}, title='Borders in descending order') | |
| fig.update_layout(height=600, width=650*2) # Adjust the height value as desired | |
| st.plotly_chart(fig) | |
| with corrs: | |
| st.subheader('Monthly Entries by Border') | |
| with st.expander('Toggle',expanded=True): | |
| # Line chart for monthly entries by border | |
| fig = px.line(data, x=data.index, y=data.columns[:-1], title='Monthly Entries by Border') | |
| fig.update_layout(width=linewidths) | |
| st.plotly_chart(fig) | |
| st.subheader('Correlation heat map') | |
| with st.expander('Toggle'): | |
| correlation = summary.corr() | |
| # st.subheader('Correlation Heatmap') | |
| # st.write(correlation) | |
| fig = px.imshow(correlation, color_continuous_scale='RdBu', labels=dict(x='Columns', y='Columns'), | |
| title='Correlation Heatmap') | |
| fig.update_layout(height=600*2,width=600*2) # Adjust the height value as desired | |
| st.plotly_chart(fig) |