Bring Drag & Drop to Dash with Dashboard Engine. 💫 Learn how at our next webinar!

Applying Flask Decorator to Dash App

Hi Everyone! So something that I keep running into is the desire to apply Flask decorators in Dash, like @app.before_request. However, I get the error message when running that on a Dash app instance saying that “Dash object has no attribute ‘before_request’”. I’m wondering whether anyone might be able to help me implement something along the lines of the code in this snippet + explain the distinction between the two (it was my previous understanding that the Dash app object just built off of the Flask app object, so I was surprised when the flask-caching library seems to work with the Dash instance but Flask function decorators might not work?). The functionality that I’m trying to create is to check the referrer’s url and run something if it matches a certain website domain either when the user first enters the application or on every callback that is run. Thanks!

from werkzeug.urls import url_parse
from flask import request, g

@app.before_request
def before_request():
    url = request.referrer
    # Check referrer is my website
    if url and url_parse(url).host == "website.com":
        print("********* FUNCTION RAN **********")

I might have fixed it by applying @app.server.before_request? Will update again if that functions as expected.