hello all,
Is it possible to add an EventListener or something that registers when the browser tab is closed and shuts down the server?
I have created an application with dash. I use cx_Freeze and “python setup.py bdist_msi” to have an installable application.
See this post: Convert Dash to executable file (.exe) - #3 by MGBpy
This all works so far.
When I made some changes and created a new .msi file I wanted to install it without uninstall the older version.
A new installation was not possible because my previous app was still running in the background.
The problem is that the server does not stop when I just close the browser tab.
I have tried several things to stop the server, e.g. with an additional javascript file with windwo.addEventListerner …
Unfortunately nothing really worked.
Does anyone have an idea how to solve this problem?
I have found a solution to my problem. Maybe it will help someone at some point.
I added a JavaScript file to my assets folder:
// Open dialogue window before leaving the page
window.addEventListener('beforeunload', function (e) {
e.preventDefault();
e.returnValue = true;
});
// Shutdown the server if the usere exits
window.addEventListener("unload", function (e) {
fetch('/shutdown', { method: 'POST' });
});
Shutdown function in my app.py
@app.server.route("/shutdown", methods=["POST"])
def shutdown():
os.kill(os.getpid(), signal.SIGINT) # Send a signal to the process to terminate