【问题标题】:how do I log the data on to a dash app using python如何使用 python 将数据记录到仪表板应用程序
【发布时间】:2021-07-06 14:23:43
【问题描述】:
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)

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

【问题讨论】:

标签: javascript python plotly-dash


【解决方案1】:

你可以这样做:

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 获取所有当前子元素并添加另一个元素并返回结果。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-29
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    • 2012-01-16
    • 2015-02-26
    • 2021-06-16
    • 2017-08-01
    相关资源
    最近更新 更多