【问题标题】:Why does my dash app not update correctly?为什么我的仪表板应用程序无法正确更新?
【发布时间】: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)

【问题讨论】:

    标签: python-3.x plotly-dash


    【解决方案1】:

    看起来问题出在您如何格式化tickers 变量。在您的工作示例中,您有tickers = ['aapl', 'ayx']。然而,在你的回调中,结果是这样的:

    tickers = ['aapl, ayx']

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-09
      • 2019-09-08
      • 1970-01-01
      • 2018-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多