Я новичок в создании приложений Dash. Идея состоит в том, что пользователь вводит столько биржевых тикеров, сколько разделено запятыми. Результатом является таблица, отображающая несколько столбцов, связанных с тикерами.
Цикл работает как спроектировано вне Dash framework. Ниже находится цикл:
from yahoo_fin.stock_info import get_analysts_info, get_stats, get_live_price, get_quote_table
import pandas as pd
tickers = ['aapl', 'ayx']
stocks = pd.DataFrame(columns = ['ticker', 'yahoo_live_price', 'closing_price', '52_Week_High', '52_Week_Low','Percent_above_52_low', 'Percent_below_52_high'])
for ticker in tickers:
df = get_stats(ticker)
df['ticker'] = ticker
df = df.pivot(index = 'ticker', columns = 'Attribute', values = 'Value')
df['closing_price'] = get_quote_table(ticker)['Previous Close']
df['yahoo_live_price'] = get_live_price(ticker)
df = df[['yahoo_live_price','closing_price', '52 Week High 3', '52 Week Low 3']]
df[['52 Week High 3', '52 Week Low 3']] = df[['52 Week High 3', '52 Week Low 3']].astype('float')
df['percent_above_52_low'] = round((((df['closing_price'] - df['52 Week Low 3'])/df['closing_price']))*100,2)
df['percent_below_52_high'] = round((((df['52 Week High 3'] - df['closing_price'])/df['52 Week High 3']))*100,2)
df = df.reset_index()
df.columns = ('ticker', 'yahoo_live_price', 'closing_price', '52_Week_High', '52_Week_Low'
, 'Percent_above_52_low', 'Percent_below_52_high')
stocks = stocks.append(df, ignore_index = True)
Это дает мне именно то, что я хочу. Однако это не работает в среде Dash. В настоящее время приложение будет отображать только один тикер, и, как только он отобразится, он не изменится при изменении пользователя. Кто-нибудь замечает, что я делаю не так? Полный штрих-код ниже:
from yahoo_fin.stock_info import get_analysts_info, get_stats, get_live_price, get_quote_table
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_table
import pandas as pd
from dash.dependencies import Input, Output
app = dash.Dash()
app.layout = html.Div(children=[
html.Div(children='''
Symbols to grab:
'''),
dcc.Input(id='input', value='', type='text'),
html.Div(id='table'),
])
@app.callback(
Output(component_id='table', component_property='children'),
[Input(component_id='input', component_property='value')]
)
def update_value(input_data):
tickers = [input_data]
stocks = pd.DataFrame(columns = ['ticker', 'yahoo_live_price', 'closing_price', '52_Week_High', '52_Week_Low','Percent_above_52_low', 'Percent_below_52_high'])
for ticker in tickers:
df = get_stats(ticker)
df['ticker'] = ticker
df = df.pivot(index = 'ticker', columns = 'Attribute', values = 'Value')
df['closing_price'] = get_quote_table(ticker)['Previous Close']
df['yahoo_live_price'] = get_live_price(ticker)
df = df[['yahoo_live_price','closing_price', '52 Week High 3', '52 Week Low 3']]
df[['52 Week High 3', '52 Week Low 3']] = df[['52 Week High 3', '52 Week Low 3']].astype('float')
df['percent_above_52_low'] = round((((df['closing_price'] - df['52 Week Low 3'])/df['closing_price']))*100,2)
df['percent_below_52_high'] = round((((df['52 Week High 3'] - df['closing_price'])/df['52 Week High 3']))*100,2)
df = df.reset_index()
df.columns = ('ticker', 'yahoo_live_price', 'closing_price', '52_Week_High', '52_Week_Low'
, 'Percent_above_52_low', 'Percent_below_52_high')
stocks = stocks.append(df, ignore_index = True)
return dash_table.DataTable(
id='table',
columns=[{"name": i, "id": i} for i in stocks.columns],
data=df.to_dict('records'),
)
if __name__ == '__main__':
app.run_server(debug = False)