使用’dcc.Interval’中的’extendData’将仅在我浏览其他Chrome标签时更新我的图表

huangapple go评论51阅读模式
英文:

Using 'extendData' in a 'dcc.Interval' event will only update my graph when I'm browsing another Chrome tab

问题

I'm here to help you with the translation. Below is the translated text from the code and context you provided:

我在Dash中创建了一个非常简单的(一个页面)应用程序,使用`dcc.Interval`组件和`extendData`方法将随机数据附加到Plotly图表中(我希望有最大的x值)。
该程序一直运行得很好,直到我尝试将其移植到多页面应用程序:

我使用了以下示例:

https://github.com/facultyai/dash-bootstrap-components/blob/main/examples/python/templates/multi-page-apps/responsive-collapsible-sidebar/sidebar.py
并替换了:
```python
elif pathname == "/page-1":
        return html.P("This is the content of page 1. Yay!")

使用以下代码替换:

import page_1

...

elif pathname == "/page-1":
        return page_1.layout

我的page_1.py包含以下代码:


from dash import dcc, html
import dash_bootstrap_components as dbc
import plotly.graph_objs as go


layout = dbc.Card(dbc.CardBody([
        html.H4('Live Feed'),
        dcc.Graph(id='live-update-graph',
                    figure=go.Figure({'data': 
                        [
                            {'x': [], 'y': []},
                            {'x': [], 'y': []},
                            {'x': [], 'y': []},
                            {'x': [], 'y': []}
                        ] 
                    }),
                    ),
        dcc.Interval(
            id='interval-component',
            interval=0.1*1000, # in milliseconds
            n_intervals=0
        )
    ]
))

我将回调函数放在我的app.py文件中:

@app.callback(Output('live-update-graph', 'extendData'),
              Input('interval-component', 'n_intervals')
              )
def update_graph_live(n):
    # 收集一些数据
    y1 = np.random.normal(loc = 10, scale=10)
    y2 = y1 + random.randint(-5, 5)
    y3 = y2 + random.randint(-10, 60)
    y4 = y3 + random.randint(-40, 2)
    return [{'x': [[datetime.datetime.now()]] * 4,'y': [[y1], [y2], [y3], [y4]]}, [0,1, 2, 3], 300]

...

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

不幸的是,只有当我在Chrome浏览器中浏览其他标签时,我的图表才会更新,而当我显示它时不会更新。

我的app.py文件中还有另一页带有其他组件和相关回调函数:

@app.callback(
    Output("result-code", "children"),
    Input("slider", "value"),
)
def create_python_script(slider):
    
    
    markdown = markdown_start
        
    markdown += '''
    msg = {{
        "slider_value": {slider}  
    }}'''.format(slider=slider)

    markdown += markdown_end
    
    return markdown

我的Markdown组件实时更新,没有问题。

以下是我的回调状态的复制:

Dash中的回调状态

我的开发者控制台显示前端部分的每个传入消息:

{
   "multi": true,
   "response": {
     "live-update-graph": {
       "extendData": [
         {
           "x": [
 [
               "2023-02-13T16:58:37.533426"
             ],
 [
               "2023-02-13T16:58:37.533426"
             ],
 [
               "2023-02-13T16:58:37.533426"
             ],
 [
               "2023-02-13T16:58:37.533426"
             ]
           ],
           "y": [
 [
               -4.26648933108117
             ],
 [
               -3.2664893310811696
             ],
 [
               -8.26648933108117
             ],
 [
               -9.26648933108117
             ]
           ]
         },
 [
 0,
 1,
 2,
 3
         ],
 300
       ]
     }
   }
 }

我是否做错了什么?

提前感谢!


<details>
<summary>英文:</summary>

I created a very simple (one page) application in Dash that appends random data to a plotly chart using a `dcc.Interval` component and the `extendData` method (I&#39;d like to have x values max). 
The program worked like a charm, until I tried to port it to a multi-page application:


I used the following example:

https://github.com/facultyai/dash-bootstrap-components/blob/main/examples/python/templates/multi-page-apps/responsive-collapsible-sidebar/sidebar.py
and replaced:
```python
elif pathname == &quot;/page-1&quot;:
        return html.P(&quot;This is the content of page 1. Yay!&quot;)

with:

import page_1

...

elif pathname == &quot;/page-1&quot;:
        return page_1.layout

My page_1.py contains the following code:


from dash import dcc, html
import dash_bootstrap_components as dbc
import plotly.graph_objs as go


layout = dbc.Card(dbc.CardBody([
        html.H4(&#39;Live Feed&#39;),
        dcc.Graph(id=&#39;live-update-graph&#39;,
                    figure=go.Figure({&#39;data&#39;: 
                        [
                            {&#39;x&#39;: [], &#39;y&#39;: []},
                            {&#39;x&#39;: [], &#39;y&#39;: []},
                            {&#39;x&#39;: [], &#39;y&#39;: []},
                            {&#39;x&#39;: [], &#39;y&#39;: []}
                        ] 
                    }),
                    ),
        dcc.Interval(
            id=&#39;interval-component&#39;,
            interval=0.1*1000, # in milliseconds
            n_intervals=0
        )
    ]
))

I put my Callback in my app.py file:

@app.callback(Output(&#39;live-update-graph&#39;, &#39;extendData&#39;),
              Input(&#39;interval-component&#39;, &#39;n_intervals&#39;)
              )
def update_graph_live(n):
    # Collect some data
    y1 = np.random.normal(loc = 10, scale=10)
    y2 = y1 + random.randint(-5, 5)
    y3 = y2 + random.randint(-10, 60)
    y4 = y3 + random.randint(-40, 2)
    return [{&#39;x&#39;: [[datetime.datetime.now()]] * 4,&#39;y&#39;: [[y1], [y2], [y3], [y4]]}, [0,1, 2, 3], 300]

...

if __name__ == &#39;__main__&#39;:
    app.run_server(debug=True)

Unfortunatly, my chart will only update when I'm browsing another tab in Chrome, not when I'm displaying it.

I have another page with some other components and an associated callback declared in my app.py file as :

@app.callback(
    Output(&quot;result-code&quot;, &quot;children&quot;),
    Input(&quot;slider&quot;, &quot;value&quot;),
)
def create_python_script(slider):
    
    
    markdown = markdown_start
        
    markdown += &#39;&#39;&#39;
    msg = {{
        &quot;slider_value&quot;: {slider}  
    }}&#39;&#39;&#39;.format(slider=slider)

    markdown += markdown_end
    
    return markdown

And my Markdown component is updated in real-time, no problem with that.

Here is a copy of my callback status:

Callback status in Dash

My developper console shows every incoming message in the front-end part:

{
   &quot;multi&quot;: true,
   &quot;response&quot;: {
     &quot;live-update-graph&quot;: {
       &quot;extendData&quot;: [
         {
           &quot;x&quot;: [
 [
               &quot;2023-02-13T16:58:37.533426&quot;
             ],
 [
               &quot;2023-02-13T16:58:37.533426&quot;
             ],
 [
               &quot;2023-02-13T16:58:37.533426&quot;
             ],
 [
               &quot;2023-02-13T16:58:37.533426&quot;
             ]
           ],
           &quot;y&quot;: [
 [
               -4.26648933108117
             ],
 [
               -3.2664893310811696
             ],
 [
               -8.26648933108117
             ],
 [
               -9.26648933108117
             ]
           ]
         },
 [
 0,
 1,
 2,
 3
         ],
 300
       ]
     }
   }
 }

Am I doing something wrong?

Thanks in advance !

答案1

得分: 0

我使用的是 http://localhost:8888 而不是 http://127.0.0.1:8888 来连接我的 Web 应用程序,不知何故没有注意到它,这导致了图表无法更新。

英文:

I was using http://localhost:8888 instead of http://127.0.0.1:8888 to connect to my web app and somehow didn't see it, and that was preventing the chart from being updated.

huangapple
  • 本文由 发表于 2023年2月14日 00:18:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438522.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定