CoolFace
Apppublic

dharmilgandhi007/sample_space

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes
app.py79 linesDownload Raw Back to root
1from datetime import datetime2import pandas as pd3import plotly.express as px4from faicons import icon_svg5from shinywidgets import render_plotly6from shiny import reactive7from shiny.express import input, render, ui8 9# Load both original and updated datasets10data_original = pd.read_csv("C:/Users/admin/Desktop/Sem4/Sample_shiny_project/dashboard2/32100245.csv")11data_forecast = pd.read_csv("C:/Users/admin/Desktop/Sem4/Sample_shiny_project/dashboard2/updated_expense_data.csv")12 13# Convert REF_DATE to datetime14data_original['REF_DATE'] = pd.to_datetime(data_original['REF_DATE'])15data_forecast['REF_DATE'] = pd.to_datetime(data_forecast['REF_DATE'])16 17# Combine both datasets18data = pd.concat([data_original, data_forecast], ignore_index=True)19 20def string_to_date(date_str):21    return datetime.strptime(date_str, "%Y-%m-%d").date()22 23def filter_by_date(df: pd.DataFrame, date_range: tuple, expense: str, location: str):24    rng = sorted(date_range)25    dates = pd.to_datetime(df["REF_DATE"]).dt.date26    return df[(dates >= rng[0]) & (dates <= rng[1]) & 27              (df["Expenses"] == expense) & (df["GEO"] == location)]28 29ui.page_opts(title="Expense Analysis Dashboard with Forecast")30 31with ui.sidebar():32    ui.input_select("expense", "Filter by Expense Type", choices=data["Expenses"].unique().tolist()),33    ui.input_select("location", "Filter by Location", choices=data["GEO"].unique().tolist()),34    ui.input_slider("date_range", "Filter by Date Range",35                    min=string_to_date("2007-01-01"),36                    max=string_to_date("2028-12-31"),37                    value=[string_to_date("2007-01-01"), string_to_date("2028-12-31")])38 39with ui.layout_column_wrap():40    with ui.value_box(showcase=icon_svg("dollar-sign")):41        "Total Expense Value"42 43        @render.ui44        def total_expense():45            filtered_data = filter_by_date(data, input.date_range(), input.expense(), input.location())46            total_value = filtered_data["VALUE"].sum()47            return f"${total_value:,.0f}"48 49    with ui.value_box(showcase=icon_svg("chart-line")):50        "Average Expense Value"51 52        @render.ui53        def average_expense():54            filtered_data = filter_by_date(data, input.date_range(), input.expense(), input.location())55            average_value = filtered_data["VALUE"].mean()56            return f"${average_value:,.0f}"57 58with ui.navset_card_underline(title="Expense Analysis"):59    with ui.nav_panel("Plot", icon=icon_svg("chart-line")):60        @render_plotly61        def expense_plot():62            filtered_data = filter_by_date(data, input.date_range(), input.expense(), input.location())63            plot_data = filtered_data.copy()64            plot_data['REF_DATE'] = plot_data['REF_DATE'].dt.strftime('%Y')65            fig = px.line(66                plot_data,67                x="REF_DATE",68                y="VALUE",69                labels={"VALUE": "Expense Value ($)", "REF_DATE": "Year"},70                title="Expense Over Time with Forecast"71            )72            return fig73    74    with ui.nav_panel("Table", icon=icon_svg("table")):75        @render.data_frame76        def expense_table():77            filtered_data = filter_by_date(data, input.date_range(), input.expense(), input.location())78            return render.DataGrid(filtered_data)79