Thank you for the response but that option only inserts a layout when there is a “404” into dash.page_container
and keeps the default layout. I really just want to return a static html file. I will probably end up going with something like
@app.server.before_request
def override_dash_404():
""" Ensure unknown paths return a proper 404 page. """
# Store the stripped request path once
requested_path = flask.request.path.strip("/")
# Known Dash and Flask routes
dash_routes = {page["path"].strip("/") for page in dash.page_registry.values()}
flask_routes = {rule.rule.strip("/") for rule in app.server.url_map.iter_rules()}
# ✅ Allow valid Dash routes
if requested_path in dash_routes:
return # Let Dash handle it
# ✅ Allow valid Flask routes
if requested_path in flask_routes:
return # Let Flask handle it
# ✅ Allow Dash-internal API calls (updates, assets, favicon, etc.)
if flask.request.path.startswith(("/_dash", "/assets/", "/favicon.ico")):
return # Let Dash handle it
# ❌ Everything else gets a real 404 error
flask.abort(404) # This ensures Flask's real 404 handling takes over
@app.server.errorhandler(404)
def page_not_found(error):
""" Serve a proper 404 page instead of Dash's fake 404 """
return flask.render_template("404.html"), 404