From 6401bcc46205847c69622bfd4b09e5b43a9432b9 Mon Sep 17 00:00:00 2001 From: Alagris Date: Sun, 20 Jun 2021 19:46:12 +0200 Subject: [PATCH] table --- finance.py | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/finance.py b/finance.py index ecb79f8..9364735 100755 --- a/finance.py +++ b/finance.py @@ -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__':