Plot data not updating on page refresh; only when server is restarted

As described above, the data doesn’t update when the page is refreshed - only when the server is restarted (by closing and restarting locally, or by heroku restart (then the data updates just fine, but won’t update again until the server is restarted)

Please help!

Here’s my code:

import dash
import dash_core_components as dcc
import dash_html_components as html

import os
import bs4
import time
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup



app = dash.Dash()
server = app.server # Added by me



my_url = 'www.url_to_data.com'
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, "html.parser")



scraped_dates = page_soup.findAll("div")[0].findAll("p")

dates = []

for i in range(0,(len(scraped_dates))):
    dates.append(scraped_dates[i].text)



scraped_percent_prob = page_soup.findAll("div")[1].findAll("p") 

percent_prob = []

for i in range(0,(len(scraped_percent_prob))):
    percent_prob.append(float(scraped_percent_prob[i].text))




app.layout = html.Div(children=[
    html.H1(children='Heading text'),

    html.Div(children='''
        Some more text.
    '''),

    dcc.Graph(
        id='example-graph',
        style={'height': '700px'},
        figure={
            'data': [
                {'x': dates, 
                'y': percent_prob, 
                "line": {
    "color": "rgb(255, 0, 0)", 
    "width": 3.5
  }, 
  "name": "T20", 
  "opacity": 0.8, 
  "type": "scatter", 
  "uid": "e78862", 

}
            ],
            'layout': {
  "title": "Time Series with Rangeslider", 
  "xaxis": {
    "autorange": True, 
    #"range": ["2015-02-17", "2017-02-16"], 
    "rangeselector": {"buttons": [
        {
          "count": 1, 
          "label": "1m", 
          "step": "month", 
          "stepmode": "backward"
        }, 
        {
          "count": 6, 
          "label": "6m", 
          "step": "month", 
          "stepmode": "backward"
        }, 
        {"step": "all"}
      ]}, 
    "rangeslider": {
      # "range": ["2016-10-01", "2017-02-16"],
      # "autorange": True, 
      # "range": ["2015-02-17", "2017-02-16"], 
      # "yaxis": {"rangemode": "match"},
      
    }, 
    "title": "AAPL High, x; AAPL Low, x", 
    "type": "date"
  }, 
  "yaxis": {
    "autorange": False, 
    "range": [0, 100], 
    "title": "Win Probability", 
    "type": "linear"
  }
}
        }
    )
])

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

Set app.layout to a function and call your data inside that function. See https://dash.plot.ly/live-updates

1 Like

It works - thank you!