This commit is contained in:
Alagris 2021-06-20 19:46:12 +02:00
parent fcfd73a040
commit 6401bcc462

View File

@ -46,8 +46,11 @@ stock_list = ['WHR', 'WMB', 'WLTW', 'WYNN', 'XEL', 'XLNX', 'XYL', 'YUM', 'ZBRA',
tickerData = yf.Tickers(stock_list)
fullTableDf = tickerData.history(period='1d', start='2019-1-1', end='2020-1-25')
tickerDf = fullTableDf['Close']
closePrices = fullTableDf['Close']
selected_stocks_in_graph = [stock_list[0]]
selected_stocks_in_graph_df = closePrices[selected_stocks_in_graph]
selected_stock_in_table = stock_list[0]
selected_stock_in_table_df = fullTableDf.xs(selected_stock_in_table, axis=1, level=1)
# Initialize the app
app = dash.Dash(__name__)
app.config.suppress_callback_exceptions = True
@ -74,14 +77,14 @@ app.layout = html.Div(
className='div-for-dropdown',
children=[
dcc.Dropdown(id='stockselector',
options=get_options(tickerDf.columns.unique()),
multi=True, value=[tickerDf.columns.sort_values()[0]],
options=get_options(stock_list),
multi=True, value=selected_stocks_in_graph,
style={'backgroundColor': '#1E1E1E'},
className='stockselector'
),
dcc.Dropdown(id='table_selector',
options=get_options(tickerDf.columns.unique()),
multi=False, value=[tickerDf.columns.sort_values()[0]],
options=get_options(stock_list),
multi=False, value=selected_stock_in_table,
style={'backgroundColor': '#1E1E1E'},
className='stockselector'
)
@ -93,31 +96,40 @@ app.layout = html.Div(
children=[
dcc.Graph(id='timeseries', config={'displayModeBar': False}, animate=True),
dash_table.DataTable(
id='table',
columns=[{"name": i, "id": i} for i in tickerDf.columns],
data=tickerDf.to_dict('records'),
id='stock_price_table',
style_header={'backgroundColor': 'rgb(30, 30, 30)'},
style_cell={
'backgroundColor': 'rgb(50, 50, 50)',
'color': 'white'
},
)
])
])
]
)
# Callback for timeseries price
@app.callback(Output('timeseries', 'figure'), [Input('stockselector', 'value')])
def update_graph(selected_dropdown_value):
subDf = tickerDf[selected_dropdown_value]
figure = px.line(subDf, template='plotly_dark', title='Stock Prices')
global selected_stocks_in_graph
global selected_stocks_in_graph_df
selected_stocks_in_graph = selected_dropdown_value
selected_stocks_in_graph_df = closePrices[selected_stocks_in_graph]
figure = px.line(selected_stocks_in_graph_df, template='plotly_dark', title='Stock Prices')
return figure
# @app.callback(Output('timeseries', 'figure'),
# [Input('table_selector', 'value')])
# def update_table(selected_dropdown_value):
# subDf = tickerDf[selected_dropdown_value]
# figure = px.line(subDf, template='plotly_dark', title='Stock Prices')
# return figure
@app.callback([Output("stock_price_table", "data"), Output('stock_price_table', 'columns')],
[Input('table_selector', 'value')])
def update_table(selected_dropdown_value):
global selected_stock_in_table
global selected_stock_in_table_df
selected_stock_in_table = selected_dropdown_value
selected_stock_in_table_df = fullTableDf.xs(selected_stock_in_table, axis=1, level=1)
data = selected_stock_in_table_df.to_dict('records')
columns = [{"name": i, "id": i} for i in selected_stock_in_table_df.columns]
return data, columns
if __name__ == '__main__':