Dash Python 实时更新的间隔错误

Dash Python interval error with live update

我正在学习 python 和 dash,我正在构建一个应用程序来监控我的交易。

我改编了来自“实时更新组件”示例(轨道卫星)的代码。它在 jupyter notebook 和 repl.it 上运行良好,但在我的计算机上尝试时出现错误:

"回溯(最近调用最后): 文件“orbital.py”,第 62 行,位于 输入('interval-component', 'n_intervals'))"

“输入参数 interval-component.n_intervals 必须是列表或元组 dash.dependencies.Inputs。”

不明白为什么

这是我的代码:

import ccxt
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly
from dash.dependencies import Input, Output
import pandas as pd



app = dash.Dash(__name__)

ftx = ccxt.ftx({'verbose': True})
ftx = ccxt.ftx({
    'apiKey': '',
    'secret': '',

    
})


app.layout = html.Div(
    html.Div([
        html.H4('FTX Live Feed'),
        html.Div(id='live-update-text'),
        dcc.Interval(
            id='interval-component',
            interval=1*1000, # in milliseconds
            n_intervals=0
        )
    ])
)

class Client:
    """A sample client class"""

    def __init__(self):
        self.pnl = []

    @classmethod
    def get_balances(client):
        try:
            balance = ftx.fetch_balance()
        except ccxt.BaseError as e:
            print(f"Could not get account with error: {e}")
            raise e
        else:
            result = balance["info"]["result"]
            total = []
            for x in result:
                if float(x["free"]) > 0.0:
                    total.append(x["usdValue"])
                    a = list(map(float, total))

            df = pd.Series(a)
            # global totCap
            totCap = df.sum()
            return totCap

@app.callback(Output('live-update-text', 'children'),
              Input('interval-component', 'n_intervals'))


def update_metrics(n):

    arc = Client().get_balances()
    style = {'padding': '5px', 'fontSize': '16px'}
    return [
        html.Span('Balance: {0:.2f}'.format(arc), style=style)
    ]



if __name__ == '__main__':
    app.run_server(host="0.0.0.0", port="8050")

这是 replit 应用程序的屏幕截图: replit dash

正如错误所说,您需要将 Input 包装在一个列表中,如下所示:

@app.callback(Output('live-update-text', 'children'),
              [Input('interval-component', 'n_intervals')])