Correctly updating data and pages

tl;dr: am I misunderstanding using functions for my layouts? Am I updating my data wrong?

I am working on a dashboard which I initially built as static, and am now making dynamic/live. I have built my pages as functions, so it would render with the most recently scraped data every time I go on a different page/reload.

@callback(
        Output("content", "children"),
        Input("url", "pathname")
        )
def serve_page_content(pathname):
    if pathname == '/':
        return pages.Today()
    elif pathname == '/dagverkoop':
        return pages.Daysales()
    elif pathname == '/standen':
        return pages.Records()
    else:
        return html.P('This page does not exist!')

At intervals I run a function that runs a scraper, thus updating the DataFrames I am using in my pages. However, after scraping, the pages are what they were before, without the updated information. With some testing I did find that my DataFrames would update and all functions would run properly and the DataFrames should be globally accessible.

# 'sinkhole' is a div that is meant to be hidden
@callback(
        Output('sinkhole', 'children'),
        Input('interval-large', 'n_intervals'),
        )
def reload_data(count):
    print('Ping')
    if 'testTime' not in globals():
        global testTime
        testTime = int(time())
    if (time() - testTime) >= 30:
        pages.update_all()
    return html.P()
def update_all():
    print('Updating...')
    df_live = scrape.liverecords()
    df_salesstats = scrape.salesrecords()
    df_today = scrape.dayfilter(df_salesstats, currentDay)
    print('Done!')

Am I misunderstanding how Dash renders layouts returned from from a function called within a callback? Or am I missing something else?

Hi @thanndbt welcome to the forums.

Where do you store the updated data and how do make this data accessible (for each page)?

Hi @AIMPED and thank you

Luck would have it I just fixed it and it was somewhat of an oversight on my end. For readability I had split my app across multiple files (as seen with the pages.* and scrape.* functions). For convenience’s sake I had imported the file with my DataFrame’s as ‘from df import *’ as opposed to ‘import df’ and when reloading the data, it would create a local DataFrame next to the global one.

In other words: rookie mistake.

1 Like