import streamlit as st import plotly.express as px import pandas as pd import plotly.graph_objects as go from plotly.subplots import make_subplots from metrics_calculations import ( users, monthly_budget, monthly_expenses, savings_rate, debt_overview, cash_flow, portfolio_value, asset_allocation, dividend_income, performance_chart, proj_retirement_savings ) monthly_budget = monthly_budget() monthly_expenses = monthly_expenses() savings_rate = savings_rate() debt_overview = debt_overview() cash_flow = cash_flow() portfolio_value = portfolio_value() asset_allocation = asset_allocation() dividend_income = dividend_income() performance_chart = performance_chart() proj_retirement_savings = proj_retirement_savings() st.set_page_config( page_title="Finetech Dashboard", layout="wide" ) st.title("Finetech Dashboard") selected_user = st.selectbox("Select User", users) with st.container(): fig1, fig2 = st.columns(2) with fig1: st.header("Budget Breakdown") selected_user_budget = monthly_budget[selected_user] fig1 = px.bar( selected_user_budget, x=selected_user_budget.index, y=selected_user_budget.values, labels={'x': 'Categories', 'y': 'Spendings'}, title=f'User {selected_user}' ) fig1.update_xaxes(title='Categories', tickangle=45) fig1.update_layout( height=500, width=500 ) st.plotly_chart(fig1) with fig2: st.header("Monthly Expenses Over Time") user_data = monthly_expenses user_data = user_data[user_data['UserId'] == selected_user] user_data['Month'] = user_data['Month'].astype(str) fig2 = px.line( user_data, x='Month', y='MonthlyExpenses', markers=True, line_shape='linear', title=f'User {selected_user}', labels={'MonthlyExpenses': 'Total Monthly Expenses (USD)'} ) fig2.update_xaxes(title='Date', tickangle=45) fig2.update_yaxes(title='Total Monthly Expenses (USD)') fig2.update_layout( height=500, width=500 ) st.plotly_chart(fig2) with st.container(): fig1, fig2 = st.columns(2) with fig1: st.header("Savings Rate Over Time") user_data = savings_rate user_data = user_data[user_data['UserId'] == selected_user] user_data['date'] = user_data['date'].dt.strftime('%Y-%m') fig1 = px.line( user_data, x='date', y='SavingsRate', markers=True, line_shape='linear', title=f'User {selected_user}', labels={'SavingsRate': 'Savings Rate (%)'} ) fig1.update_xaxes(title='Date', tickangle=45) fig1.update_yaxes(title='Savings Rate (%)') fig1.update_layout( height=500, width=500 ) st.plotly_chart(fig1) with fig2: st.header("Debt Overview") user_data = debt_overview user_data = user_data[selected_user] fig2 = px.bar( user_data, x='debtType', y='amount', title=f'User {selected_user}', labels={'amount': 'Debt Amount (USD)'} ) fig2.update_xaxes(title='Debt Type', tickangle=45) fig2.update_yaxes(title='Debt Amount (USD)') fig2.update_layout( height=500, width=500 ) st.plotly_chart(fig2) with st.container(): fig1, fig2 = st.columns(2) with fig1: st.header("Cash Flow Over Time") user_data = cash_flow user_data = user_data[user_data['UserId'] == selected_user] fig1 = px.line( user_data, x='date', y='CashFlow', markers=True, line_shape='linear', title=f'User {selected_user}', labels={'CashFlow': 'Cash Flow (USD)'} ) fig1.update_xaxes(title='Date', tickangle=45) fig1.update_yaxes(title='Cash Flow (USD)') fig1.update_layout( height=500, width=500 ) st.plotly_chart(fig1) with fig2: st.header("Portfolio Value Over Time") user_data = portfolio_value user_data = user_data[user_data['UserId'] == selected_user] fig2 = px.line( user_data, x='date', y='balance', markers=True, line_shape='linear', title=f'User {selected_user}', labels={'balance': 'Total Portfolio Value (USD)'} ) fig2.update_xaxes(title='Date', tickangle=45) fig2.update_yaxes(title='Total Portfolio Value (USD)') fig2.update_layout( height=500, width=500 ) st.plotly_chart(fig2) with st.container(): fig1, fig2 = st.columns(2) with fig1: st.header("Asset Allocation") user_data = asset_allocation user_data = user_data[user_data['UserId'] == selected_user] fig1 = px.pie( user_data, values='Allocation', names='Asset', title=f'User {selected_user}', labels={'Allocation': 'Allocation (USD)'} ) fig1.update_layout( height=500, width=500 ) st.plotly_chart(fig1) with fig2: st.header("Dividend Income Over Time") user_data = dividend_income user_data = user_data[user_data.index.get_level_values('UserId') == selected_user] user_data.index = pd.to_datetime([str(idx[1]) for idx in user_data.index]) fig2 = px.line( user_data, x=user_data.index, y='dividends', markers=True, line_shape='linear', title=f'User {selected_user}' ) fig2.update_xaxes(title='Date', tickangle=45) fig2.update_yaxes(title='Dividend Income (USD)') fig2.update_layout(showlegend=False) fig2.update_layout( height=500, width=500 ) st.plotly_chart(fig2) with st.container(): st.header("Performance Chart") user_data = performance_chart user_data = user_data[user_data['UserId'] == selected_user] fig1, fig2 = st.columns(2) with fig1: fig1 = go.Figure() fig1.add_trace(go.Scatter(x=user_data['date'], y=user_data['daily_balance'], mode='lines', name='Daily Balance')) fig1.update_xaxes(title_text='Date') fig1.update_yaxes(title_text='Daily Balance') fig1.update_layout( height=500, width=500 ) st.plotly_chart(fig1) with fig2: one_month_return = user_data['daily_balance'].pct_change(periods=4) # Assuming 20 trading days in a month one_year_return = user_data['daily_balance'].pct_change(periods=252) # Assuming 252 trading days in a year five_year_return = user_data['daily_balance'].pct_change(periods=252 * 5) # Assuming 252 trading days in a year for 5 years # Create a DataFrame for the returns returns_df = pd.DataFrame({ 'Return Period': ['1-Month', '1-Year', '5-Year'], 'Return Value': [one_month_return.iloc[-1], one_year_return.iloc[-1], five_year_return.iloc[-1]] }) # Create a bar plot for returns fig2 = px.bar(returns_df, x='Return Period', y='Return Value', text='Return Value', labels={'Return Value': 'Return'}) fig2.update_traces(texttemplate='%{text:.2%}', textposition='outside') fig2.update_xaxes(title_text='Return Period') fig2.update_yaxes(title_text='Return') fig2.update_layout( height=500, width=500 ) # Display both charts st.plotly_chart(fig2) with st.container(): st.header("Projected Retirement Savings") user_data = proj_retirement_savings user_data = user_data[user_data["UserId"] == selected_user] fig = px.bar(user_data, x='UserId', y='ProjectedSavings', labels={'UserId': 'User ID', 'ProjectedSavings': 'Total Projected Retirement Savings'}, title='Total Projected Retirement Savings by User') fig.update_xaxes(tickmode='linear', dtick=1) fig.update_yaxes(tickformat=',f') fig.update_yaxes(tickformat=',.2f') fig.update_layout( height=600, width=400 ) st.plotly_chart(fig)