|
| 1 | +import dash |
| 2 | +from dash import dcc, html |
| 3 | +from dash.dependencies import Input, Output |
| 4 | +import plotly.graph_objs as go |
| 5 | +import httpx |
| 6 | + |
| 7 | +app = dash.Dash(__name__) |
| 8 | + |
| 9 | +app.layout = html.Div( |
| 10 | + [ |
| 11 | + html.H1("Stock Valuation Dashboard"), |
| 12 | + dcc.Input(id="stock-input", type="text", placeholder="Enter stock symbol"), |
| 13 | + html.Button("Analyze", id="analyze-button"), |
| 14 | + html.Div(id="valuation-output"), |
| 15 | + dcc.Graph(id="growth-chart"), |
| 16 | + ] |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +@app.callback( |
| 21 | + [Output("valuation-output", "children"), Output("growth-chart", "figure")], |
| 22 | + [Input("analyze-button", "n_clicks")], |
| 23 | + [dash.dependencies.State("stock-input", "value")], |
| 24 | +) |
| 25 | +def update_valuation(n_clicks, symbol): |
| 26 | + if n_clicks is None or not symbol: |
| 27 | + return dash.no_update, dash.no_update |
| 28 | + |
| 29 | + # Fetch data from our FastAPI endpoint |
| 30 | + response = httpx.get(f"http://localhost:8000/stock/{symbol}") |
| 31 | + data = response.json() |
| 32 | + |
| 33 | + # Create valuation output |
| 34 | + valuation_output = [ |
| 35 | + html.P( |
| 36 | + f"Is quality dividend growth stock: {data['is_quality_dividend_growth_stock']}" |
| 37 | + ), |
| 38 | + html.P(f"Is undervalued: {data['is_undervalued']}"), |
| 39 | + ] |
| 40 | + |
| 41 | + # Create growth chart |
| 42 | + traces = [] |
| 43 | + for metric, rates in data["growth_rates"].items(): |
| 44 | + trace = go.Bar(x=list(rates.keys()), y=list(rates.values()), name=metric) |
| 45 | + traces.append(trace) |
| 46 | + |
| 47 | + layout = go.Layout(title="Growth Rates", barmode="group") |
| 48 | + figure = go.Figure(data=traces, layout=layout) |
| 49 | + |
| 50 | + return valuation_output, figure |
| 51 | + |
| 52 | + |
| 53 | +if __name__ == "__main__": |
| 54 | + app.run_server(debug=True) |
0 commit comments