为什么我的仪表板应用程序无法正确更新?

时间:2019-07-04 10:07:39

标签: python-3.x plotly-dash

我刚开始构建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)

1 个答案:

答案 0 :(得分:0)

问题似乎出在格式化tickers变量的方式上。在您的工作示例中,您有tickers = ['aapl', 'ayx']。但是,在您的回调中,结果如下:

tickers = ['aapl, ayx']

这是一个包含逗号分隔值的字符串。因此,发生的情况是您的循环仅检测到一个项目,即一个字符串。您需要分解输入字符串,以使每种股票具有正确的格式,即每只股票一个字符串,然后列表中将有一个len()与输入的股票数量相匹配。