File size: 2,571 Bytes
592a23a
 
227ed13
0977ccb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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()