Flask cache isn't in main file


I have a multi-page project in Dash.

main.py looks like:

app = Dash(__name__, use_pages=True)

app.layout = dbc.Container([
], fluid=True

pages/home.py looks like:

import dash
from solver import some_function

dash.register_page(__name__, path_template='/company/<id>')

def layout(id=None):
    data = some_function(id) # huge amount of data

And finally solver.py:

def some_function(id):
    # some code 
    return data

I want to add caching. I need to add to solver.py:

from flask_caching import Cache
cache = Cache(app.server, config={ ... })

def query_data():
    # some code for caching

I don’t understand how to use the app variable in another module and I can’t find a solution.

As I understand it, there was a similar problem here, but I need to add caching not in main.py.

I tried adding app_flask = Flask(_ name _) to solver.py, but this solution doesn’t seem to work and gives errors.

You can access the app variable through dash. get_app(). API Reference | Dash for Python Documentation | Plotly

Hello, @Werner


Dash app is created in main. py
To access in solver.py

from dash import get_app


1 Like

Yes, I made it, but still same error :slightly_frowning_face:

ok sorry, just checked my code. I defined the cache in the file where I have access to the flask server (app. server) and imported the defined cache to the module where I needed to decorate the function. This worked for me.

I found that I call some functions from solver.py in main.py, I moved them and solved my first problem.

Now I have “AttributeError: Dash object has no attribute ‘jinja_env’” and this is not help :thinking:

Thank you, you were right! I removed all imports from solver.py in main.py. Also it was necessary to take not app, but app.server :man_facepalming: