Trying to run a function after a period of time on a multipage app with rotating layouts

I have a dash app that rotates through each layout every 5 seconds. I want the data to update once a day at the minimum. I wrote a function UPDATE_DATA() that does that successfully, however no matter what I’ve done I can’t get the data to update without reloading the app.

from dash.dependencies import Input, Output
import csv
import dash
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
from sqlConn import DataUtility
import plotly.express as px

# noinspection DuplicatedCode
sql = DataUtility()
conn = 'DSN=keyvanTest;Database=Digitalization;TrustedConnection=yes;MARS_Connection=yes;UID=UserID;PWD=password'
DashboardValues = '''hold lengthy select statement '''


MTTR_MTBF = ''' holds lengthy select statement'''

update_interval = 20
# Change contents every (INTERVAL) seconds
INTERVAL = 5


def SQL_to_CSV(file_name: str, sql_str: str):
    df = pd.DataFrame(sql.min_query(sql_str))
    file_string = "assets/{}".format(file_name)
    df.to_csv(file_string, index=False)
    with open(file_string, 'r+') as newFile:
        rd = csv.reader(newFile, delimiter=',', quotechar='"')
        new_array = []
        for row in rd:
            new_array.append(row)
        return new_array


def UPDATE_DATA():
    SQL_to_CSV('test.csv', DashboardValues)
    dash_data = pd.read_csv('assets/test.csv')
    global so_data
    so_data = dash_data['DashboardValues']
    global mt_data
    mt_data = SQL_to_CSV('MTTR_MTBF.csv', MTTR_MTBF)


UPDATE_DATA()

Total_Time_Scheduled = so_data[0]
Total_Time_Unscheduled = so_data[1]
Total_Time_ScheduledP = so_data[2]
Total_Time_UnscheduledP = so_data[3]
Leading_Scheduled = so_data[4]
Leading_Unscheduled = so_data[5]
MTTR = mt_data[1]
MTBF = mt_data[2]
MTTR_Definition = "This metric is the average time needed to restore an asset to its full operational capabilities " \
                  "after a loss of function "
MTBF_Definition = "This metric is the average length of operating time between failures for an asset or " \
                  "component"

colors = {'background': '#4f81bd', 'text': 'white'}
borders = {'border': '2px #073763 solid', 'border-radius': '15px'}

app = dash.Dash(__name__)
# noinspection DuplicatedCode
# app layouts to flip through
PAGES = [
    dcc.Tab(label='Tab one', children=[
        dbc.Row(html.H1('Hot Mill Maintenance KPI Dashboard (Total Score)'), style={
            'height': 100,
            'textAlign': 'center',
            'color': colors['text'],
            'backgroundColor': colors['background'],
            'fontSize': 24,
            'border': '2px #073763 solid',
            'border-radius': '4px'
        }, ),
        dbc.Row(children=[
            html.Span("Leading Indicators",
                      style={'textAlign': 'left',
                             'color': colors['text'],
                             'background': colors['background'],
                             'fontSize': 60,
                             'display': 'inline-block',
                             'margin-left': 150,
                             'margin-top': 10,
                             'border': '4px #073763 solid',
                             'border-radius': '4px'
                             }),
            html.Span("Lagging Indicators",
                      style={'textAlign': 'right',
                             'color': colors['text'],
                             'background': colors['background'],
                             'fontSize': 60,
                             'display': 'inline-block',
                             'margin-left': 670,
                             'margin-top': 10,
                             'border': '4px #073763 solid',
                             'border-radius': '4px'
                             }),

        ]),
        dbc.Row(children=[

            html.Span("% Scheduled", style={'textAlign': 'center',
                                            'color': colors['text'],
                                            'background': colors['background'],
                                            'fontSize': 50,
                                            'display': 'inline-block',
                                            'margin-left': 10,
                                            'margin-top': 10,
                                            'border': '4px #073763 solid',
                                            'border-radius': '4px',
                                            'width': 350
                                            }, ),
            html.Span("% Unscheduled", style={'textAlign': 'center',
                                              'color': colors['text'],
                                              'background': colors['background'],
                                              'fontSize': 50,
                                              'display': 'inline-block',
                                              'margin-left': 10,
                                              'margin-top': 10,
                                              'border': '4px #073763 solid',
                                              'border-radius': '4px',
                                              'width': 350
                                              }, ),
            html.Span("% Scheduled", style={'textAlign': 'center',
                                            'color': colors['text'],
                                            'background': colors['background'],
                                            'fontSize': 50,
                                            'display': 'inline-block',
                                            'margin-left': 400,
                                            'border': '4px #073763 solid',
                                            'border-radius': '4px',
                                            'width': 350
                                            }, ),
            html.Span("% Unscheduled", style={'textAlign': 'center',
                                              'color': colors['text'],
                                              'background': colors['background'],
                                              'fontSize': 50,
                                              'display': 'inline-block',
                                              'margin-left': 10,
                                              'border': '4px #073763 solid',
                                              'border-radius': '4px',
                                              'width': 350
                                              }, ),
        ]),
        dbc.Row(children=[
            html.Span(Leading_Scheduled, style={'textAlign': 'center',
                                                'color': colors['text'],
                                                'background': colors['background'],
                                                'fontSize': 50,
                                                'display': 'inline-block',
                                                'margin-left': 10,
                                                'margin-top': 10,
                                                'border': '4px #073763 solid',
                                                'border-radius': '4px',
                                                'width': 350
                                                }),
            html.Span(Leading_Unscheduled, style={'textAlign': 'center',
                                                  'color': colors['text'],
                                                  'background': colors['background'],
                                                  'fontSize': 50,
                                                  'display': 'inline-block',
                                                  'margin-left': 10,
                                                  'border': '4px #073763 solid',
                                                  'border-radius': '4px',
                                                  'width': 350
                                                  }, ),
            html.Span(MTTR, style={'textAlign': 'right',
                                   'color': colors['text'],
                                   'background': colors['background'],
                                   'fontSize': 50,
                                   'display': 'inline-block',
                                   'margin-left': 550,
                                   'border': '4px #073763 solid',
                                   'border-radius': '4px'
                                   }, ),
            html.Span(MTBF, style={'textAlign': 'right',
                                   'color': colors['text'],
                                   'background': colors['background'],
                                   'fontSize': 50,
                                   'display': 'inline-block',
                                   'margin-left': 250,
                                   'margin-top': 10,
                                   'border': '4px #073763 solid',
                                   'border-radius': '4px'
                                   }, )
        ]),
        html.Div(html.Img(src=app.get_asset_url("NASLogo.jpg"),
                          style={'height': '25%',
                                 'width': '25%',
                                 }),
                 style={
                     'textAlign': 'center'
                 }
                 ),
    ]),
    dcc.Tab(label='Tab two', children=[
        html.Div(children=[
            dbc.Row(html.H1('May 2021: Scheduled vs Unscheduled Maintenance'),
                    style={
                        'height': 100,
                        'textAlign': 'center',
                        'color': colors['text'],
                        'backgroundColor': colors['background'],
                        'fontSize': 24,
                        'border': '2px #073763 solid',
                        'border-radius': '4px'
                    }, ),
            dbc.Row(children=[
                dbc.Col(html.Span("Total Time Hours"),
                        style={'textAlign': 'left',
                               'color': colors['text'],
                               'background': colors['background'],
                               'fontSize': 60,
                               'display': 'inline-block',
                               'margin-left': 200,
                               'margin-top': 10,
                               'border': '4px #073763 solid',
                               'border-radius': '4px'
                               }),
                dbc.Col(html.Span("Total Time %"),
                        style={'textAlign': 'right',
                               'color': colors['text'],
                               'background': colors['background'],
                               'fontSize': 60,
                               'display': 'inline-block',
                               'margin-left': 700,
                               'margin-top': 10,
                               'border': '4px #073763 solid',
                               'border-radius': '4px'
                               }),
            ], ),
            dbc.Row(children=[

                html.Span("Scheduled", style={'textAlign': 'center',
                                              'color': colors['text'],
                                              'background': colors['background'],
                                              'fontSize': 50,
                                              'display': 'inline-block',
                                              'margin-left': 10,
                                              'margin-top': 10,
                                              'border': '4px #073763 solid',
                                              'border-radius': '4px',
                                              'width': 350
                                              }),
                html.Span("Unscheduled", style={'textAlign': 'center',
                                                'color': colors['text'],
                                                'background': colors['background'],
                                                'fontSize': 50,
                                                'display': 'inline-block',
                                                'margin-left': 10,
                                                'margin-top': 10,
                                                'border': '4px #073763 solid',
                                                'border-radius': '4px',
                                                'width': 350
                                                }, ),
                html.Span("% Scheduled", style={'textAlign': 'center',
                                                'color': colors['text'],
                                                'background': colors['background'],
                                                'fontSize': 50,
                                                'display': 'inline-block',
                                                'margin-left': 400,
                                                'border': '4px #073763 solid',
                                                'border-radius': '4px',
                                                'width': 350
                                                }, ),
                html.Span("% Unscheduled", style={'textAlign': 'center',
                                                  'color': colors['text'],
                                                  'background': colors['background'],
                                                  'fontSize': 50,
                                                  'display': 'inline-block',
                                                  'margin-left': 10,
                                                  'border': '4px #073763 solid',
                                                  'border-radius': '4px',
                                                  'width': 350
                                                  }, ),
            ]),
            dbc.Row(id='row4', children=[
                html.Span(Total_Time_Scheduled, style={'textAlign': 'center',
                                                       'color': colors['text'],
                                                       'background': colors['background'],
                                                       'fontSize': 50,
                                                       'display': 'inline-block',
                                                       'margin-left': 10,
                                                       'margin-top': 10,
                                                       'border': '4px #073763 solid',
                                                       'border-radius': '4px',
                                                       'width': 350
                                                       }),
                html.Span(Total_Time_Unscheduled, style={'textAlign': 'center',
                                                         'color': colors['text'],
                                                         'background': colors['background'],
                                                         'fontSize': 50,
                                                         'display': 'inline-block',
                                                         'margin-left': 10,
                                                         'border': '4px #073763 solid',
                                                         'border-radius': '4px',
                                                         'width': 350
                                                         }, ),
                html.Span(Total_Time_ScheduledP, style={'textAlign': 'right',
                                                        'color': colors['text'],
                                                        'background': colors['background'],
                                                        'fontSize': 50,
                                                        'display': 'inline-block',
                                                        'margin-left': 550,
                                                        'border': '4px #073763 solid',
                                                        'border-radius': '4px'
                                                        }, ),
                html.Span(Total_Time_UnscheduledP, style={'textAlign': 'right',
                                                          'color': colors['text'],
                                                          'background': colors['background'],
                                                          'fontSize': 50,
                                                          'display': 'inline-block',
                                                          'margin-left': 225,
                                                          'margin-top': 10,
                                                          'border': '4px #073763 solid',
                                                          'border-radius': '4px'
                                                          }, )
            ]),
            html.Div(html.Img(src=app.get_asset_url("NASLogo.jpg"),
                              style={'height': '25%',
                                     'width': '25%',
                                     }),
                     style={
                         'textAlign': 'center'
                     }
                     ),
        ])
    ]),
    dcc.Tab(label='Tab three', children=[
        html.Div(children=

                 [html.H1('Defining MTTR and MTBF',
                          style={
                              'height': 100,
                              'textAlign': 'center',
                              'color': colors['text'],
                              'backgroundColor': colors['background'],
                              'fontSize': 65,
                              'border': borders['border'],
                              'border-radius': borders['border-radius']}),
                  html.Div("MTTR-Mean Time to Repair:",
                           style={
                               'textAlign': 'center',
                               'color': colors['text'],
                               'backgroundColor': colors['background'],
                               'fontSize': 60,
                               'border': borders['border'],
                               'border-radius': borders['border-radius'],
                               'width': 800,
                               'margin-bottom': 3}),
                  dcc.Textarea(value=MTTR_Definition,
                               style={
                                   'fontSize': 50,
                                   'color': colors['text'],
                                   'backgroundColor': colors['background'],
                                   'border': borders['border'],
                                   'border-radius': borders['border-radius'],
                                   'width': '100%', 'height': 150
                               }),
                  html.Div("MTBF- Mean Time Between Failure:",
                           style={
                               'margin-top': 50,
                               'textAlign': 'center',
                               'color': colors['text'],
                               'backgroundColor': colors['background'],
                               'fontSize': 60,
                               'border': borders['border'],
                               'border-radius': borders['border-radius'],
                               'width': 1000,
                               'margin-bottom': 3
                           }),
                  dcc.Textarea(
                      value=MTBF_Definition,
                      style={
                          'fontSize': 50,
                          'color': colors['text'],
                          'backgroundColor': colors['background'],
                          'border': borders['border'],
                          'border-radius': borders['border-radius'],
                          'width': '100%', 'height': 150
                      }),
                  ],

                 )
    ]),
    dcc.Tab(label='Tab Four', children=[
        html.Div(children=[
            dbc.Row(dbc.Col(html.H1('Mean Time to Repair(MTTR): Mechanical Only May 2021')),
                    style={
                        'height': 100,
                        'textAlign': 'center',
                        'color': colors['text'],
                        'backgroundColor': colors['background'],
                        'fontSize': 24,
                        'border': '2px #073763 solid',
                        'border-radius': '4px'
                    }, ),
            dbc.Row(dbc.Col(dcc.Graph(
                id='Graph',
                figure=px.bar(figure_df, y='MeanTime', x='OpCodeGroupCode',
                              title='testing',
                              )
            )
            ),
            )
        ])
    ]),
    dcc.Tab(label='Tab Five', children=[
        dcc.Graph(
            figure={
                'data': [
                    {'x': [1, 5, 3], 'y': [2, 4, 3],
                     'type': 'line', 'name': 'SF'},
                    {'x': [1, 2, 3], 'y': [5, 4, 3],
                     'type': 'line', 'name': u'Montréal'},
                ]
            }
        )
    ]),
]
# app.config.suppress_callback_exceptions = True
app.layout = html.Div(children=[
    html.Div(id='app-container'),
    dcc.Interval(
        id='interval1',
        interval=INTERVAL * 1000,  # in milliseconds
        n_intervals=0
    ),
], )


@app.callback(Output('app-container', 'children'),
              [Input('interval1', 'n_intervals')])
def CHANGE_PAGE(n_intervals):
    return PAGES[n_intervals % len(PAGES)]


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

The use of global variables is discouraged, see this section of the documentation,

https://dash.plotly.com/sharing-data-between-callbacks

The reasoning they give for not using global variables does not apply to what I am doing.

Could you elaborate? It seems to me that you would like to update the global variables one per day without restarting the app?

In their documentation they discourage the use of global variables if there are multiple users accessing the app. This is for one screen to display the same layouts. I would just want it to update daily.

Ah, I think what the really mean is “global variables won’t work” (or to be more specific, modifying global varaibles won’t), and the reason that they are not support is to enabled support for multiple users. Nonetheless, it is not expected to work with one user either. Instead, you should read the data in the callback. If you only want to update one a day, you could use a cache.

Apologies, I am not familiar with either of those. Can you elaborate or provide documentation?