plotly / dash

Data Apps & Dashboards for Python. No JavaScript Required.

Home Page:https://plotly.com/dash

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] `clickData` from `go.ScatterMapbox` does not work when an invalid tile layer is specified.

colinbrust opened this issue · comments

Describe your context

dash                      2.13.0
dash-bootstrap-components 1.5.0
dash-core-components      2.0.0
dash-daq                  0.5.0
dash-html-components      2.0.0
dash-loading-spinners     1.0.0
dash-mantine-components   0.12.1
dash-table                5.0.0

Describe the bug

When using go.ScatterMapbox, clickData is not triggered if an invalid tile layer is specified. In the example below, the dialog box doesn't pop up when clicking the point on the map. Commenting out the invalid tile layer solves the problem:

import dash
import pandas as pd
import plotly.graph_objects as go
from dash import dcc, html
from dash.dependencies import Input, Output

app = dash.Dash(__name__)

data = {"Latitude": [37.7749], "Longitude": [-122.4194], "Location": ["San Francisco"]}
df = pd.DataFrame(data)

# Create a map using go.Scattermapbox
fig = go.Figure(
    go.Scattermapbox(
        lat=df["Latitude"],
        lon=df["Longitude"],
        mode="markers",
    )
)

# Add tile layers to the map
fig.update_layout(
    mapbox_style="white-bg",
    mapbox_layers=[
        # A valid tile layer.
        {
            "below": "traces",
            "sourcetype": "raster",
            "sourceattribution": "USGS Map Tiles",
            "source": [
                "https://basemap.nationalmap.gov/arcgis/rest/services/USGSShadedReliefOnly/MapServer/tile/{z}/{y}/{x}"
            ],
        },
        # Here is an invalid tile layer.
        # If you comment out the dict below, the popup works as expected.
        {
            "below": "traces",
            "sourcetype": "raster",
            "source": [
                "https://asdf/{z}/{x}/{y}.png"
            ],
        },
    ],
    mapbox={"center": {"lon": -122.4194, "lat": 37.7749}, "zoom": 5},
)
app.layout = html.Div(
    [
        dcc.Graph(id="map", figure=fig),
        dcc.ConfirmDialog(
            id="modal",
            message="You clicked the point!",
        ),
    ]
)

# Display modal
@app.callback(Output("modal", "displayed"), [Input("map", "clickData")])
def display_modal(clickData):
    if clickData is not None:
        return True
    return False


if __name__ == "__main__":
    app.run_server(debug=True)

Expected behavior

clickData should work even if an invalid tile layer is used. At the very least, it would be nice if an exception was thrown when an invalid tile layer is used (although that may be more of a Plotly issue). I discovered this issue because we were using a deprecated Stamen tile layer. The lack of any errors made it difficult to track down the root cause of the problem.