Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import plotly.graph_objects as go | |
| from src.utils import fetch_financials, clean_financials | |
| def plot_income(income_df, ticker_symbol): | |
| fig = go.Figure() | |
| # List of metrics we want to plot β change as per availability | |
| metrics = [ | |
| "Total Revenue", | |
| "Net Income Available to Common Shares" | |
| ] | |
| for m in metrics: | |
| if m in income_df.columns: | |
| fig.add_trace(go.Bar( | |
| x=income_df.index, | |
| y=income_df[m] / 1e9, # convert to billions | |
| name=m | |
| )) | |
| fig.update_layout( | |
| title=f"{ticker_symbol} Income Statement", | |
| xaxis_title="Date", | |
| yaxis_title="Value (in billions)", | |
| barmode='group' | |
| ) | |
| return fig | |
| def plot_cashflow(cash_df, ticker_symbol): | |
| fig = go.Figure() | |
| metrics = [ | |
| "Total Cash From Operating Activities", | |
| "Capital Expenditures", | |
| ] | |
| for m in metrics: | |
| if m in cash_df.columns: | |
| vals = cash_df[m] | |
| # If CapEx, often negative -> make positive for bar | |
| if "Capital Expenditures" in m: | |
| vals = -vals | |
| fig.add_trace(go.Bar( | |
| x=cash_df.index, | |
| y=vals / 1e9, | |
| name=m | |
| )) | |
| fig.update_layout( | |
| title=f"{ticker_symbol} Cash Flow Statement", | |
| xaxis_title="Date", | |
| yaxis_title="Value (in billions)", | |
| barmode='group' | |
| ) | |
| return fig | |
| def main(): | |
| st.title("Financial Dashboard") | |
| ticker_symbol = st.text_input("Ticker Symbol", "AAPL") | |
| if st.button("Refresh Data"): | |
| with st.spinner("Fetching data..."): | |
| income, cash = fetch_financials(ticker_symbol, freq="annual") | |
| if income.empty and cash.empty: | |
| st.error("No financial data returned. Check ticker or try again.") | |
| return | |
| income_clean = clean_financials(income) | |
| cash_clean = clean_financials(cash) | |
| st.subheader("Income Statement") | |
| fig_income = plot_income(income_clean, ticker_symbol) | |
| st.plotly_chart(fig_income, use_container_width=True) | |
| st.subheader("Cash Flow") | |
| fig_cash = plot_cashflow(cash_clean, ticker_symbol) | |
| st.plotly_chart(fig_cash, use_container_width=True) | |
| st.write("### Raw Data β Income Statement") | |
| st.dataframe(income_clean) | |
| st.write("### Raw Data β Cash Flow") | |
| st.dataframe(cash_clean) | |
| if __name__ == "__main__": | |
| main() | |