-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
110 lines (99 loc) · 3.99 KB
/
app.py
File metadata and controls
110 lines (99 loc) · 3.99 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from typing import Dict
import os
import dash_bootstrap_components as dbc
from dash import Dash, dcc, html, Input, Output
import components as cmp
from config import CREDENTIALS_DICT, PROJECT_ID, REQUIRED_ENV_VARS
from guide.layout import create_layout as create_guide_layout
from market_dashboard.layout import create_layout as create_market_dashboard_layout
from portfolio_dashboard.layout import create_layout as create_portfolio_dashboard_layout
from portfolio_form.layout import create_layout as create_portfolio_form_layout
from market_dashboard.callbacks import register_callbacks as register_market_callbacks
from portfolio_dashboard.callbacks import register_callbacks as register_portfolio_callbacks
from portfolio_form.callbacks import register_callbacks as register_portfolio_form_callbacks
import services.db as db
from utils.google_cloud_utils import get_bigquery_client
def create_app() -> Dash:
# Init Dash app with bootstrap theme
dbc_css = "https://cdn.jsdelivr.net/gh/AnnMarieW/dash-bootstrap-templates/dbc.min.css"
app = Dash(__name__, external_stylesheets=[dbc.themes.LUX, dbc_css])
app.title = 'Stock Market Analytics App'
# Define paths for the sidebar
paths = {
'Market Dashboard': '/',
'My Portfolio': '/portfolio-form',
'Portfolio Dashboard': '/portfolio-dashboard',
'User Guide': '/guide',
}
# Create app main components
sidebar = cmp.create_sidebar(paths)
content = html.Div(
id='page-content',
className='p-4',
style={
'height': '100%',
"overflow-y": "auto",
})
footer = cmp.create_footer()
# Define app layout
app.layout = html.Div([
dcc.Location(id='url'),
dcc.Store(id={'type': 'portfolio-data', 'section': 'global'}, storage_type='local'),
dcc.Store(id={'type': 'price-data', 'section': 'global'}, storage_type='local'),
dbc.Row([
sidebar,
dbc.Col(content, sm=12, md={'size': 10, 'offset': 2}, className='mb-4')
], className='h-100'),
footer
],
className="bg-primary text-light h-100",
)
# Validate all required environment variables
for var in REQUIRED_ENV_VARS:
if var not in os.environ:
raise EnvironmentError(f"Missing required environment variable: {var}")
# Get tickers list from BigQuery
client = get_bigquery_client(CREDENTIALS_DICT, PROJECT_ID)
tickers = db.get_tickers(client)
# Handle page navigation through callbacks
@app.callback(
Output('page-content', 'children'),
Input('url', 'pathname')
)
def display_page(pathname: str) -> html.Div:
if pathname == '/':
return create_market_dashboard_layout(tickers)
elif pathname == '/portfolio-form':
return create_portfolio_form_layout(tickers)
elif pathname == '/portfolio-dashboard':
return create_portfolio_dashboard_layout()
elif pathname == '/guide':
return create_guide_layout()
else:
return html.H1('404 - Page Not Found', className='text-center text-danger fs-4 fw-bold mt-5')
# Fetch stock prices on app load
@app.callback(
Output({"type": "price-data", "section": "global"}, "data"),
Input("url", "pathname"),
prevent_initial_call=True,
)
def fetch_prices_on_load(_) -> Dict[str, float]:
try:
if not tickers:
return {}
prices = db.get_stocks_current_price(client, tickers)
return prices
except Exception as e:
return {"Error during fetch_prices_on_load": str(e)}
finally:
client.close()
register_market_callbacks(app)
register_portfolio_callbacks(app)
register_portfolio_form_callbacks(app)
return app
# Init app and server
app = create_app()
server = app.server
# Run app
if __name__ == '__main__':
app.run_server(debug=True)