Updating geojson info in choroplethmapbox

Hello, Plotly community,

Is it possible to modify the data behind a choroplethmapbox and have the plot updated?

I have a Geopandas Geodataframe with river gauging stations and their corresponding upstream catchments. The ‘geometry’ column of the table supplies catchment polygons through a geojson object, whereas latitude and longitude columns hold the gauge position coordinates.

Some gauges/catchments are more important than others, and my app’s purpose is to let a user revise which catchments are on a high-priority list. The app has a map figure with catchment polygons in a choroplethmapbox trace and gauge locations in a scattermapbox trace. By setting polygon opacity to zero for low-priority catchments, these become invisible in the map despite being present in the geojson data source. The gauge markers are all kept visible.

All is welll so far, the map displays as expected at startup. I can select a polygon or a point, have its properties shown in a table, and modify its value in the ‘priority’ column used for opacity info in the polygon trace.

But I cannot find out how to make the figure re-read the updated geodataframe and show/hide the polygon according to user decision. No error messages or crashes, the map simply will not update to reflect the changes.

Dos anyone know how to force a figure to re-read its data source and refresh (preferrably without making a copletely new figure for each modification)?

What I tried: (‘Forslag’ is the priority column, ‘Delfelt’ is the name of the polygon trace):
opacity = (allstations[‘Forslag’] > 0).to_numpy().astype(float) * 0.4 # 0 → transparent
mapfigure.data[0].geojson = json.loads(allstations[‘geometry’].to_json())
mapfigure.data[0].z = allstations[‘totalAreal’]
mapfigure.data[0].locations = allstations.index
mapfigure = mapfigure.update_traces(selector = ({‘name’:‘Delfelt’}),
mapwidget.update_mapboxes(layers = [{“source”:allstations[‘geometry’].to_json()}])
None of these have the desired effect, neither do they lead to traceback.

This is all in a Shiny for Python app, on Windows, Python 3.11, shiny 0.9.0, shinywidgets 0.3.2, ipywidgets 8.1.2, plotly 5.22.0.

In a previous attempt, I tried to build the two mapbox traces from separate geodataframes; one complete for the points and one restricted for the polygons. Hence, polygons would be present/absent from the polygon geojson object, rather than shown/hidden using opacity. Worked just as fine, but the same inability to force map updates, and a lot more index/slice bookkeeping.

All the best,

Sjur K