Prevent user from leaving dash app

A way to deal with this is by using JavaScript. JavaScript can be included in a Dash app via the assets directory:
https://dash.plotly.com/external-resources

Then one can use straight forward JavaScript code to let the user confirm the exit of the page:

// This default onbeforeunload event
window.onbeforeunload = function(){
    return "Do you want to leave?"
}
2 Likes