I trying to set up a 404.html page template but the issue that I am running into is that dash returns a 200
when a request is made to a page that does not exist. Why is that?
Request URL: | http://127.0.0.1:8050/test |
Request Method: | GET |
Status Code: | 200 OK |
Example:
app.py
from dash import Dash, html, page_container
from flask import render_template
app = Dash(__name__, use_pages=True)
server = app.server
app.layout = html.Div([
page_container
])
@app.server.errorhandler(404)
def not_found(error):
return render_template('404.html'), 404
if __name__ == "__main__":
app.run(debug=False)
pages/home.py
import dash
from dash import html
dash.register_page(__name__, path='/')
layout = html.Div('Hello World')
404.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>404 Not Found</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #1a1a2e;
color: white;
margin: 0;
padding: 0;
}
h1 {
font-size: 4em;
margin-top: 50px;
}
p {
font-size: 1.5em;
}
.emoji {
font-size: 5em;
}
a {
color: #ffcc00;
text-decoration: none;
font-weight: bold;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="emoji">🚀👨🚀</div>
<h1>404: Lost in Space</h1>
<p>Uh-oh! The page you’re looking for has drifted into the void.</p>
<p>Maybe it’s on another planet? Or just <i>really</i> good at hiding?</p>
<p><a href="/">Return to Mission Control</a></p>
</body>
</html>