【发布时间】:2019-07-04 10:07:39
【问题描述】:
我是构建 Dash 应用程序的新手。这个想法是,用户可以根据需要输入任意数量的以逗号分隔的股票代码。结果是一个表格,显示了与代码相关的几列。
循环在 Dash 框架之外按设计运行。下面是循环:
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)
【问题讨论】: