Multi-page app: prevent page refresh

Hey - I have a multi-page app, where the main page is a dash-leaflet map and another tab where I display the data. Trouble is, when the user goes to “data” tab and back, the map is refreshed. It can be expensive, plus information about the previous location is lost.

I tried to extend the component with Persistence of center and zoom, but the map still gets refreshed.

Is there any way to indicate that the page should not be refreshed upon navigating to it? I don’t want all the callbacks to fire.

Here’s my setup in brief:

app = dash.Dash(__name__, use_pages=True, prevent_initial_callbacks=True, external_stylesheets=[dbc.themes.BOOTSTRAP], external_scripts=[chroma], background_callback_manager=get_background_callback_manager())

content = html.Div(id="page-content", style=CONTENT_STYLE)
app.layout = html.Div([dcc.Location(id="url", refresh=False), sidebar, dash.page_container])

I hoped the refresh=False will do the trick, but no luck. The background_callback_manager is DiskcacheManager, I am also using gunicorn, but i guess that does not matter.

1 Like

Could you post a complete MWE demonstrating the issue? That may help people debugging :slight_smile:

Hi @Emil ,

Am I right to think that dl.Map() does not support the persistence keyword? (Persisting User Preferences & Control Values | Dash for Python Documentation | Plotly)

If not is there any straightforward way (other than keeping the state of the map in a dcc.Store) of preserving the state of the map when the use navigates away from a page containing a map (e.g. in a multi-page app) and then returns to that page?

I can put together a working example if that would help.
(And I can work around the issue in other ways if there’s no straightforward way of preserving the state of the map :slight_smile: )

David

No, it does not. One possible solution would be to keep the map mounted in the DOM, toggling its visibility depending on the page - either manually, or using page components,

https://www.dash-extensions.com/sections/pages

Thanks, I didn’t know about that approach, and I’ll try it out.

It’s also possible dmc.Drawer() might do what I need.