如何使用 python 将数据记录到 dash 应用程序

how do I log the data on to a dash app using python

import dash_core_components as dcc
import dash_html_components as html
import dash
import sys

app = dash.Dash()

app.layout = html.Div([
    dcc.Interval(id='interval1', interval=1000,
n_intervals=0),
    dcc.Interval(id='interval2', interval=1 * 1000,
n_intervals=0),
    html.H1(id='div-out', children=''),
    html.Iframe(id='console-out',srcDoc='',style={'width':
'100%','height':400})
])

@app.callback(dash.dependencies.Output('div-out',
'children'),
    [dash.dependencies.Input('interval1', 'n_intervals')])
def update_interval(n):
    return 'hello'

@app.callback(dash.dependencies.Output('console-out',
'srcDoc'),
    [dash.dependencies.Input('interval2', 'n_intervals')])
def update_output(n):
    data = ''
    for i in range(n):
        data=data+'printing' + '<BR>'

    return data


app.run_server(debug=True)

我将 python 的 Dash 用作独立应用程序,我想将数据记录到 (http://127.0.0.1:8050/logger)。以上是我通过浏览尝试的代码。有没有办法可以直接将消息打印到应用程序上,这样我就不需要每次都更新列表?不使用 iframe。 任何帮助将不胜感激 提前致谢。

你可以这样做:

import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import dash

app = dash.Dash(__name__)

printing = html.P(["printing", html.Br()])

app.layout = html.Div(
    [
        dcc.Interval(id="interval", interval=1000, n_intervals=0),
        html.Div(id="content"),
    ]
)


@app.callback(
    Output("content", "children"),
    Input("interval", "n_intervals"),
    State("content", "children"),
)
def update_output(interval, content):
    if not content:
        return [printing]

    return content + [printing]


if __name__ == "__main__":
    app.run_server(debug=True)

所以上面代码的想法是创建一个容器元素来容纳文本元素。然后我们可以使用 Interval 组件创建一个每秒调用一次的回调。此回调使用容器的 children 属性 State 获取所有当前子项并添加另一个元素和 return 结果。

请注意,这将永远向 DOM 添加元素。所以在一定数量的元素上你可能想让它停止更新。