Я пытаюсь создать обратный вызов для отправки URL-адреса, использованного пользователем, когда он достиг страницы 404. Для других страниц я обычно слушаю сам URL-адрес и инициирую что-то, когда он соответствует шаблону имени пути, но в конкретном случае со страницей 404 это не может работать, поскольку страница не указывает на что-то конкретное.
Вы можете попробовать следующую страницу:
"""Code to generate the layout of a 404 error page."""
import logging
import dash
from dash import Input, Output, State, callback, html
from .. import telemetry
def layout() -> html.Div:
"""Return the layout of a 404 error page."""
# prepare the text in advance to avoid long lines
oops = "Oops! The page you are looking for does not exist."
btn = "Go Back to Main"
# create the different elements of the page
title = html.H1("404")
subtitle = html.H2("Page Not Found")
message = html.P(oops)
button = html.A(btn, href="/")
# add a telemetry tracker
tracker = html.Div(id="not_found_404_tracker", style={"display": "none"})
# create the layout of the 404 page
return html.Div([title, subtitle, message, button])
@callback(
Output("not_found_404_tracker", "children"),
# I guess it's not the right Input and should be a state instead
# and then the real question begin: how to catch when the 404 is reached
Input("url", "pathname"),
State("magic_session", "data"),
prevent_initial_call=False,
)
def track_not_found_404_visit(url, session_data):
"""Track 404 page visit with session ID."""
if session_data and session_data.get("session_id"):
telemetry.add_page_visit(
session_data["session_id"],
"not_found_404_page",
"url": url
)
return ""
dash.register_page(__name__, layout=layout)
Подробнее здесь: https://stackoverflow.com/questions/798 ... -triggered