How to use caching to determine whether the new queried data have changed

The example uses uuid to create a dataframe.

I want to query data using df = pd.read_sql(sql_statement, conn) to cache the dataframe. Afterwards, would I then need a global variable that I need to save the data to, to determine whether my query contains new data?

I’m looking for an architectural guidance in regards to:

  1. Query data on initial load
  2. Use n_interval every 10 seconds to query data
    → if data has changed → then show these changes
    → if data has not changed → dont do anything
import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
import datetime
from flask_caching import Cache
import os
import pandas as pd
import time
import uuid

external_stylesheets = [
    # Dash CSS
    # Loading screen CSS

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
cache = Cache(app.server, config={
    'CACHE_TYPE': 'filesystem',
    'CACHE_DIR': 'cache-directory',

    # should be equal to maximum number of users on the app at a single time
    # higher numbers will store more data in the filesystem / redis cache

def get_dataframe(session_id):
    def query_and_serialize_data(session_id):
        # expensive or user/session-unique data processing step goes here

        # df = pd.read_sql(sql_statement, conn)

        # simulate a user/session-unique data processing step by generating
        # data that is dependent on time
        now =

        df = pd.DataFrame({
            'time': [
                str(now - datetime.timedelta(seconds=15)),
                str(now - datetime.timedelta(seconds=10)),
                str(now - datetime.timedelta(seconds=5)),
            'values': ['a', 'b', 'a', 'c']
        return df.to_json()

    return pd.read_json(query_and_serialize_data(session_id))

def serve_layout():
    session_id = str(uuid.uuid4())

    return html.Div([
        dcc.Store(data=session_id, id='session-id'),
        html.Button('Get data', id='get-data-button'),

app.layout = serve_layout

@app.callback(Output('output-1', 'children'),
              Input('get-data-button', 'n_clicks'),
              Input('session-id', 'data'))
def display_value_1(value, session_id):
    df = get_dataframe(session_id)
    return html.Div([
        'Output 1 - Button has been clicked {} times'.format(value),

@app.callback(Output('output-2', 'children'),
              Input('get-data-button', 'n_clicks'),
              Input('session-id', 'data'))
def display_value_2(value, session_id):
    df = get_dataframe(session_id)
    return html.Div([
        'Output 2 - Button has been clicked {} times'.format(value),

if __name__ == '__main__':

Even i’m facing similar challenge, did you find any solution for this