在Plotly Dash中显示后端日志消息

问题描述 投票:0回答:1

我目前正在使用Plotly Dash为基于JVM的(Kotlin)服务开发仪表板。本质上,JVM服务将消息(通过ZMQ)推送到我的Python Dash脚本,后者又更新了一系列实时图表。

除了图表之外,我还想在Dash仪表板中显示服务的日志消息(它们当前显示在控制台中并写入文件)。我可以轻松修改JVM app / Python脚本以通过ZMQ发送/接收消息,但是我找不到可以实时显示这些消息的Dash组件。

由于消息的吞吐量非常高(每秒几十个),我希望能够按级别(信息,警告等)过滤消息,也许还有其他标准(正则表达式是理想的)。我阅读了Dash文档,但无法找到适合我需求的组件。有没有办法在Dash中实现这一目标?

谢谢你的帮助!

python logging plotly-dash
1个回答
1
投票

我不熟悉你如何在脚本中收到消息,但你想要使用的是涉及到的内容:

  • 一个正常的html.Div(id='log-div',style=dict(height='300px',overflow='auto')),它只是一个可滚动的空div
  • 一个类似于的回调函数
@app.callback(
  Output('log-div','children'),
  [Input('log-div','children')
)
def log_content(old_logs):
    # wait for whatever time interval you want between updating the messages
    time.sleep(2)

    # take in the messages through some function
    messages = receive_messages_function()

    # filter the messages here
    messages = filter_messages_function()

    # old_logs is a list of the old messages, with each line being a 
    # separate div (to separate the messages, this can be done in many different ways).
    # So what you do is add the new filtered messages to the old filtered messages
    # Note - this assumes the messages are in a list of some sort; it doesn't matter
    # as the concept is the same - just add the new messages after the old ones
    messages = old_logs + messages

    return messages   

这可行,但你必须确保sleep()ing不会妨碍应用程序的其余部分工作(即将threaded=True传递给app.run_server()或将服务器对象配置为线程化。

希望这可以帮助!评论任何问题/反馈,我很乐意回应。

© www.soinside.com 2019 - 2024. All rights reserved.