plotly.py icon indicating copy to clipboard operation
plotly.py copied to clipboard

Cannot provide custom HTTP headers (e.g. bearer token) to Mapbox layer source

Open bchretien opened this issue 1 year ago • 2 comments

Hi!

I wanted to display a Mapbox layer that requires authentication through bearer tokens, i.e. when doing something like:

fig.update_layout(
    mapbox_style="white-bg",
    mapbox_layers=[
        {
            "below": 'traces',
            "sourcetype": "raster",
            "sourceattribution": "My private ortho",
            "source": [
                "https://my_private_source.com/MapServer/tile/{z}/{y}/{x}"
            ]
        }
      ]
)

I need to pass custom HTTP headers to each tile request. In JavaScript, this can be easily achieved with the Mapbox API thanks to transformRequest (see here):

const map = new mapboxgl.Map({
  container: 'map',
  center: [2.35, 48.86],
  zoom: 13,
  transformRequest: (url, resourceType) => {
    if (url.startsWith('https://my_private_source.com')) {
      return {
        url: url,
        headers: { 'Authorization': 'Bearer ' + my_private_token }
      }
    }
  }
});

However, this does not seem supported by plotly.py, or am I missing something? With some pointers I could work on a PR, but I'm not sure where to start in the code base.

bchretien avatar Apr 04 '24 18:04 bchretien

I think this is something that would need to happen in plotly.js. Most of the plots are autogenerated from there. Here is the initial creation of the map object: https://github.com/plotly/plotly.js/blob/d20b8e22370a28116c1276159a0e666e73cba5ae/src/plots/mapbox/mapbox.js#L98-L114

From my minimal research, it looks like either the entire map object would have to be recreated or create a new request manager on the map. https://github.com/plotly/mapbox-gl-js/blob/52fa02f543e8403946a861b4736261561b2a2b6b/src/ui/map.js#L404

TillerBurr avatar Apr 07 '24 03:04 TillerBurr

@TillerBurr thanks a lot for the information! I'll keep you posted once I start working on this (not sure when).

bchretien avatar Apr 08 '24 10:04 bchretien