-
Notifications
You must be signed in to change notification settings - Fork 1
/
stock_dashboard.py
139 lines (116 loc) · 4.3 KB
/
stock_dashboard.py
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Output, Input
import plotly.express as px
import dash_bootstrap_components as dbc
import pandas as pd
import pandas_datareader.data as web
import datetime
# https://stooq.com/
start = datetime.datetime(2020, 1, 1)
end = datetime.datetime(2020, 12, 3)
df = web.DataReader(['AMZN','GOOGL','FB','PFE','MRNA','BNTX'],
'stooq', start=start, end=end)
# df=df.melt(ignore_index=False, value_name="price").reset_index()
df = df.stack().reset_index()
print(df[:15])
# df.to_csv("mystocks.csv", index=False)
# df = pd.read_csv("mystocks.csv")
# print(df[:15])
# https://www.bootstrapcdn.com/bootswatch/
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP],
meta_tags=[{'name': 'viewport',
'content': 'width=device-width, initial-scale=1.0'}]
)
# Layout section: Bootstrap (https://hackerthemes.com/bootstrap-cheatsheet/)
# ************************************************************************
app.layout = dbc.Container([
dbc.Row(
dbc.Col(html.H1("Stock Market Dashboard",
className='text-center text-primary mb-4'),
width=12)
),
dbc.Row([
dbc.Col([
dcc.Dropdown(id='my-dpdn', multi=False, value='AMZN',
options=[{'label':x, 'value':x}
for x in sorted(df['Symbols'].unique())],
),
dcc.Graph(id='line-fig', figure={})
],# width={'size':5, 'offset':1, 'order':1},
xs=12, sm=12, md=12, lg=5, xl=5
),
dbc.Col([
dcc.Dropdown(id='my-dpdn2', multi=True, value=['PFE','BNTX'],
options=[{'label':x, 'value':x}
for x in sorted(df['Symbols'].unique())],
),
dcc.Graph(id='line-fig2', figure={})
], #width={'size':5, 'offset':0, 'order':2},
xs=12, sm=12, md=12, lg=5, xl=5
),
], no_gutters=True, justify='start'), # Horizontal:start,center,end,between,around
dbc.Row([
dbc.Col([
html.P("Select Company Stock:",
style={"textDecoration": "underline"}),
dcc.Checklist(id='my-checklist', value=['FB', 'GOOGL', 'AMZN'],
options=[{'label':x, 'value':x}
for x in sorted(df['Symbols'].unique())],
labelClassName="mr-3"),
dcc.Graph(id='my-hist', figure={}),
], #width={'size':5, 'offset':1},
xs=12, sm=12, md=12, lg=5, xl=5
),
dbc.Col([
dbc.Card(
[
dbc.CardBody(
html.P(
"We're better together. Help each other out!",
className="card-text")
),
dbc.CardImg(
src="https://media.giphy.com/media/Ll0jnPa6IS8eI/giphy.gif",
bottom=True),
],
style={"width": "24rem"},
)
], #width={'size':5, 'offset':1},
xs=12, sm=12, md=12, lg=5, xl=5
)
], align="center") # Vertical: start, center, end
], fluid=True)
# Callback section: connecting the components
# ************************************************************************
# Line chart - Single
@app.callback(
Output('line-fig', 'figure'),
Input('my-dpdn', 'value')
)
def update_graph(stock_slctd):
dff = df[df['Symbols']==stock_slctd]
figln = px.line(dff, x='Date', y='High')
return figln
# Line chart - multiple
@app.callback(
Output('line-fig2', 'figure'),
Input('my-dpdn2', 'value')
)
def update_graph(stock_slctd):
dff = df[df['Symbols'].isin(stock_slctd)]
figln2 = px.line(dff, x='Date', y='Open', color='Symbols')
return figln2
# Histogram
@app.callback(
Output('my-hist', 'figure'),
Input('my-checklist', 'value')
)
def update_graph(stock_slctd):
dff = df[df['Symbols'].isin(stock_slctd)]
dff = dff[dff['Date']=='2020-12-03']
fighist = px.histogram(dff, x='Symbols', y='Close')
return fighist
if __name__=='__main__':
app.run_server(debug=True)