首页 > 解决方案 > 在 Plotly Dash 中显示后端日志消息

问题描述

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

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

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

谢谢你的帮助!

标签: pythonloggingplotly-dash

解决方案


我不熟悉您如何接收脚本中的消息,但您想要使用的是涉及以下内容的内容:

  • 一个普通html.Div(id='log-div',style=dict(height='300px',overflow='auto'))的,只是一个可滚动的空 div
  • adcc.Interval触发每 n 秒抓取一次新日志
  • 一个看起来像的回调函数
@app.callback(
  Output('log-div','children'),
  [Input('log-interval','n_intervals')],
  [State('log-div','children')]
)
def log_content(n_intervals,old_logs):
    # 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   

注意 - 旧的响应不正确。

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


推荐阅读