Dash - Update issues when I leave the window












0















With Dash v36, I am running a local server. I go to the webpage and I see a live update. However, when I switch to another tab of my browser and I come back, I see an issue where the graph keeps updating the previous data points (right when I switched tabs). The graph acts normal only when it comes back to current data points. One way I tried solving it is refreshing the webpage myself. Is there a better way?



Here is the code:



import dash
import dash_core_components as dcc
import dash_html_components as html
from pandas_datareader.data import DataReader
import time
from collections import deque
import plotly.graph_objs as go
import random

app = dash.Dash('vehicle-data')

max_length = 1500
times = deque(maxlen=max_length)
oil_temps = deque(maxlen=max_length)
intake_temps = deque(maxlen=max_length)
coolant_temps = deque(maxlen=max_length)
rpms = deque(maxlen=max_length)
speeds = deque(maxlen=max_length)
throttle_pos = deque(maxlen=max_length)

data_dict = {"Oil Temperature":oil_temps,
"Intake Temperature": intake_temps,
"Coolant Temperature": coolant_temps,
"RPM":rpms,
"Speed":speeds,
"Throttle Position":throttle_pos}


def update_obd_values(times, oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos):

times.append(time.time())
if len(times) == 1:
#starting relevant values
oil_temps.append(random.randrange(180,230))
intake_temps.append(random.randrange(95,115))
coolant_temps.append(random.randrange(170,220))
rpms.append(random.randrange(1000,9500))
speeds.append(random.randrange(30,140))
throttle_pos.append(random.randrange(10,90))
else:
for data_of_interest in [oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos]:
data_of_interest.append(data_of_interest[-1]+data_of_interest[-1]*random.uniform(-0.0001,0.0001))

return times, oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos

times, oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos = update_obd_values(times, oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos)



app.layout = html.Div([
html.Div([
html.H2('Vehicle Data',
style={'float': 'left',
}),
]),
dcc.Dropdown(id='vehicle-data-name',
options=[{'label': s, 'value': s}
for s in data_dict.keys()],
value=['Coolant Temperature','Oil Temperature','Intake Temperature'],
multi=True
),
html.Div(children=html.Div(id='graphs'), className='row'),
dcc.Interval(
id='graph-update',
interval=1000,
n_intervals = 0),

# html.Div(id='signal', style={'display': 'none'})
], className="container",style={'width':'98%','margin-left':10,'margin-right':10,'max-width':50000})


@app.callback(
dash.dependencies.Output('graphs','children'),
[dash.dependencies.Input('vehicle-data-name', 'value'), dash.dependencies.Input('graph-update', 'n_intervals')]
)
def update_graph(data_names, n):
graphs =
update_obd_values(times, oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos)
if len(data_names)>2:
class_choice = 'col s12 m6 l4'
elif len(data_names) == 2:
class_choice = 'col s12 m6 l6'
else:
class_choice = 'col s12'


for data_name in data_names:

data = go.Scatter(
x=list(times),
y=list(data_dict[data_name]),
name='Scatter',
fill="tozeroy",
fillcolor="#6897bb"
)

graphs.append(html.Div(dcc.Graph(
id=data_name,
animate=True,
figure={'data': [data],'layout' : go.Layout(xaxis=dict(range=[min(times),max(times)]),
yaxis=dict(range=[min(data_dict[data_name]),max(data_dict[data_name])]),
margin={'l':50,'r':1,'t':45,'b':1},
title='{}'.format(data_name))}
), className=class_choice))

return graphs



external_css = ["https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css"]
for css in external_css:
app.css.append_css({"external_url": css})

external_js = ['https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js']
for js in external_css:
app.scripts.append_script({'external_url': js})


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









share|improve this question



























    0















    With Dash v36, I am running a local server. I go to the webpage and I see a live update. However, when I switch to another tab of my browser and I come back, I see an issue where the graph keeps updating the previous data points (right when I switched tabs). The graph acts normal only when it comes back to current data points. One way I tried solving it is refreshing the webpage myself. Is there a better way?



    Here is the code:



    import dash
    import dash_core_components as dcc
    import dash_html_components as html
    from pandas_datareader.data import DataReader
    import time
    from collections import deque
    import plotly.graph_objs as go
    import random

    app = dash.Dash('vehicle-data')

    max_length = 1500
    times = deque(maxlen=max_length)
    oil_temps = deque(maxlen=max_length)
    intake_temps = deque(maxlen=max_length)
    coolant_temps = deque(maxlen=max_length)
    rpms = deque(maxlen=max_length)
    speeds = deque(maxlen=max_length)
    throttle_pos = deque(maxlen=max_length)

    data_dict = {"Oil Temperature":oil_temps,
    "Intake Temperature": intake_temps,
    "Coolant Temperature": coolant_temps,
    "RPM":rpms,
    "Speed":speeds,
    "Throttle Position":throttle_pos}


    def update_obd_values(times, oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos):

    times.append(time.time())
    if len(times) == 1:
    #starting relevant values
    oil_temps.append(random.randrange(180,230))
    intake_temps.append(random.randrange(95,115))
    coolant_temps.append(random.randrange(170,220))
    rpms.append(random.randrange(1000,9500))
    speeds.append(random.randrange(30,140))
    throttle_pos.append(random.randrange(10,90))
    else:
    for data_of_interest in [oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos]:
    data_of_interest.append(data_of_interest[-1]+data_of_interest[-1]*random.uniform(-0.0001,0.0001))

    return times, oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos

    times, oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos = update_obd_values(times, oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos)



    app.layout = html.Div([
    html.Div([
    html.H2('Vehicle Data',
    style={'float': 'left',
    }),
    ]),
    dcc.Dropdown(id='vehicle-data-name',
    options=[{'label': s, 'value': s}
    for s in data_dict.keys()],
    value=['Coolant Temperature','Oil Temperature','Intake Temperature'],
    multi=True
    ),
    html.Div(children=html.Div(id='graphs'), className='row'),
    dcc.Interval(
    id='graph-update',
    interval=1000,
    n_intervals = 0),

    # html.Div(id='signal', style={'display': 'none'})
    ], className="container",style={'width':'98%','margin-left':10,'margin-right':10,'max-width':50000})


    @app.callback(
    dash.dependencies.Output('graphs','children'),
    [dash.dependencies.Input('vehicle-data-name', 'value'), dash.dependencies.Input('graph-update', 'n_intervals')]
    )
    def update_graph(data_names, n):
    graphs =
    update_obd_values(times, oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos)
    if len(data_names)>2:
    class_choice = 'col s12 m6 l4'
    elif len(data_names) == 2:
    class_choice = 'col s12 m6 l6'
    else:
    class_choice = 'col s12'


    for data_name in data_names:

    data = go.Scatter(
    x=list(times),
    y=list(data_dict[data_name]),
    name='Scatter',
    fill="tozeroy",
    fillcolor="#6897bb"
    )

    graphs.append(html.Div(dcc.Graph(
    id=data_name,
    animate=True,
    figure={'data': [data],'layout' : go.Layout(xaxis=dict(range=[min(times),max(times)]),
    yaxis=dict(range=[min(data_dict[data_name]),max(data_dict[data_name])]),
    margin={'l':50,'r':1,'t':45,'b':1},
    title='{}'.format(data_name))}
    ), className=class_choice))

    return graphs



    external_css = ["https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css"]
    for css in external_css:
    app.css.append_css({"external_url": css})

    external_js = ['https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js']
    for js in external_css:
    app.scripts.append_script({'external_url': js})


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









    share|improve this question

























      0












      0








      0








      With Dash v36, I am running a local server. I go to the webpage and I see a live update. However, when I switch to another tab of my browser and I come back, I see an issue where the graph keeps updating the previous data points (right when I switched tabs). The graph acts normal only when it comes back to current data points. One way I tried solving it is refreshing the webpage myself. Is there a better way?



      Here is the code:



      import dash
      import dash_core_components as dcc
      import dash_html_components as html
      from pandas_datareader.data import DataReader
      import time
      from collections import deque
      import plotly.graph_objs as go
      import random

      app = dash.Dash('vehicle-data')

      max_length = 1500
      times = deque(maxlen=max_length)
      oil_temps = deque(maxlen=max_length)
      intake_temps = deque(maxlen=max_length)
      coolant_temps = deque(maxlen=max_length)
      rpms = deque(maxlen=max_length)
      speeds = deque(maxlen=max_length)
      throttle_pos = deque(maxlen=max_length)

      data_dict = {"Oil Temperature":oil_temps,
      "Intake Temperature": intake_temps,
      "Coolant Temperature": coolant_temps,
      "RPM":rpms,
      "Speed":speeds,
      "Throttle Position":throttle_pos}


      def update_obd_values(times, oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos):

      times.append(time.time())
      if len(times) == 1:
      #starting relevant values
      oil_temps.append(random.randrange(180,230))
      intake_temps.append(random.randrange(95,115))
      coolant_temps.append(random.randrange(170,220))
      rpms.append(random.randrange(1000,9500))
      speeds.append(random.randrange(30,140))
      throttle_pos.append(random.randrange(10,90))
      else:
      for data_of_interest in [oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos]:
      data_of_interest.append(data_of_interest[-1]+data_of_interest[-1]*random.uniform(-0.0001,0.0001))

      return times, oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos

      times, oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos = update_obd_values(times, oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos)



      app.layout = html.Div([
      html.Div([
      html.H2('Vehicle Data',
      style={'float': 'left',
      }),
      ]),
      dcc.Dropdown(id='vehicle-data-name',
      options=[{'label': s, 'value': s}
      for s in data_dict.keys()],
      value=['Coolant Temperature','Oil Temperature','Intake Temperature'],
      multi=True
      ),
      html.Div(children=html.Div(id='graphs'), className='row'),
      dcc.Interval(
      id='graph-update',
      interval=1000,
      n_intervals = 0),

      # html.Div(id='signal', style={'display': 'none'})
      ], className="container",style={'width':'98%','margin-left':10,'margin-right':10,'max-width':50000})


      @app.callback(
      dash.dependencies.Output('graphs','children'),
      [dash.dependencies.Input('vehicle-data-name', 'value'), dash.dependencies.Input('graph-update', 'n_intervals')]
      )
      def update_graph(data_names, n):
      graphs =
      update_obd_values(times, oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos)
      if len(data_names)>2:
      class_choice = 'col s12 m6 l4'
      elif len(data_names) == 2:
      class_choice = 'col s12 m6 l6'
      else:
      class_choice = 'col s12'


      for data_name in data_names:

      data = go.Scatter(
      x=list(times),
      y=list(data_dict[data_name]),
      name='Scatter',
      fill="tozeroy",
      fillcolor="#6897bb"
      )

      graphs.append(html.Div(dcc.Graph(
      id=data_name,
      animate=True,
      figure={'data': [data],'layout' : go.Layout(xaxis=dict(range=[min(times),max(times)]),
      yaxis=dict(range=[min(data_dict[data_name]),max(data_dict[data_name])]),
      margin={'l':50,'r':1,'t':45,'b':1},
      title='{}'.format(data_name))}
      ), className=class_choice))

      return graphs



      external_css = ["https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css"]
      for css in external_css:
      app.css.append_css({"external_url": css})

      external_js = ['https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js']
      for js in external_css:
      app.scripts.append_script({'external_url': js})


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









      share|improve this question














      With Dash v36, I am running a local server. I go to the webpage and I see a live update. However, when I switch to another tab of my browser and I come back, I see an issue where the graph keeps updating the previous data points (right when I switched tabs). The graph acts normal only when it comes back to current data points. One way I tried solving it is refreshing the webpage myself. Is there a better way?



      Here is the code:



      import dash
      import dash_core_components as dcc
      import dash_html_components as html
      from pandas_datareader.data import DataReader
      import time
      from collections import deque
      import plotly.graph_objs as go
      import random

      app = dash.Dash('vehicle-data')

      max_length = 1500
      times = deque(maxlen=max_length)
      oil_temps = deque(maxlen=max_length)
      intake_temps = deque(maxlen=max_length)
      coolant_temps = deque(maxlen=max_length)
      rpms = deque(maxlen=max_length)
      speeds = deque(maxlen=max_length)
      throttle_pos = deque(maxlen=max_length)

      data_dict = {"Oil Temperature":oil_temps,
      "Intake Temperature": intake_temps,
      "Coolant Temperature": coolant_temps,
      "RPM":rpms,
      "Speed":speeds,
      "Throttle Position":throttle_pos}


      def update_obd_values(times, oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos):

      times.append(time.time())
      if len(times) == 1:
      #starting relevant values
      oil_temps.append(random.randrange(180,230))
      intake_temps.append(random.randrange(95,115))
      coolant_temps.append(random.randrange(170,220))
      rpms.append(random.randrange(1000,9500))
      speeds.append(random.randrange(30,140))
      throttle_pos.append(random.randrange(10,90))
      else:
      for data_of_interest in [oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos]:
      data_of_interest.append(data_of_interest[-1]+data_of_interest[-1]*random.uniform(-0.0001,0.0001))

      return times, oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos

      times, oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos = update_obd_values(times, oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos)



      app.layout = html.Div([
      html.Div([
      html.H2('Vehicle Data',
      style={'float': 'left',
      }),
      ]),
      dcc.Dropdown(id='vehicle-data-name',
      options=[{'label': s, 'value': s}
      for s in data_dict.keys()],
      value=['Coolant Temperature','Oil Temperature','Intake Temperature'],
      multi=True
      ),
      html.Div(children=html.Div(id='graphs'), className='row'),
      dcc.Interval(
      id='graph-update',
      interval=1000,
      n_intervals = 0),

      # html.Div(id='signal', style={'display': 'none'})
      ], className="container",style={'width':'98%','margin-left':10,'margin-right':10,'max-width':50000})


      @app.callback(
      dash.dependencies.Output('graphs','children'),
      [dash.dependencies.Input('vehicle-data-name', 'value'), dash.dependencies.Input('graph-update', 'n_intervals')]
      )
      def update_graph(data_names, n):
      graphs =
      update_obd_values(times, oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos)
      if len(data_names)>2:
      class_choice = 'col s12 m6 l4'
      elif len(data_names) == 2:
      class_choice = 'col s12 m6 l6'
      else:
      class_choice = 'col s12'


      for data_name in data_names:

      data = go.Scatter(
      x=list(times),
      y=list(data_dict[data_name]),
      name='Scatter',
      fill="tozeroy",
      fillcolor="#6897bb"
      )

      graphs.append(html.Div(dcc.Graph(
      id=data_name,
      animate=True,
      figure={'data': [data],'layout' : go.Layout(xaxis=dict(range=[min(times),max(times)]),
      yaxis=dict(range=[min(data_dict[data_name]),max(data_dict[data_name])]),
      margin={'l':50,'r':1,'t':45,'b':1},
      title='{}'.format(data_name))}
      ), className=class_choice))

      return graphs



      external_css = ["https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css"]
      for css in external_css:
      app.css.append_css({"external_url": css})

      external_js = ['https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js']
      for js in external_css:
      app.scripts.append_script({'external_url': js})


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






      python dash






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 4 at 7:41









      pshahpshah

      11




      11






















          0






          active

          oldest

          votes











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "3"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1401755%2fdash-update-issues-when-i-leave-the-window%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Super User!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1401755%2fdash-update-issues-when-i-leave-the-window%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

          is 'sed' thread safe

          How to make a Squid Proxy server?