What I mean is something like this: The geolocation component is added in the callback when its used. Here’s one way to do it:
from dash import Dash, dcc, html, Input, Output, callback
app = Dash(__name__, suppress_callback_exceptions=True)
app.layout = html.Div(
[
html.Button("Update Position", id="geo-start-btn"),
html.Div(id="geo"),
]
)
@callback(
Output("geo", "children"),
Input("geo-start-btn", "n_clicks"),
prevent_initial_call=True
)
def start_geolocation_section(n):
return html.Div([
html.Button("Update Position", id="update_btn"),
dcc.Geolocation(id="geolocation"),
html.Div(id="text_position"),
])
@callback(Output("geolocation", "update_now"), Input("update_btn", "n_clicks"))
def update_now(click):
return True if click and click > 0 else False
@callback(
Output("text_position", "children"),
Output("geo-start-btn", "style"),
Input("geolocation", "local_date"),
Input("geolocation", "position"),
)
def display_output(date, pos):
if pos:
return html.P(
f"As of {date} your location was: lat {pos['lat']},lon {pos['lon']}, accuracy {pos['accuracy']} meters",
), {"display": "none"}
return "No position data available", {"display": "none"}
if __name__ == "__main__":
app.run(debug=True)