I am currently developing an app using Dash Python that has users upload files that will then be placed in an app directory for temporary storage to be used for visualization in a callback. After the callback, the files are then deleted.
The issue is that there may be instances in which the files may still linger, whether that may be through an interruption before the callback, the user exits the page preemptively, etc. In order to alleviate this issue, I was thinking that I would keep a record of all paths created and then have the app just shutil.rmtree all these paths upon the user exiting the app.
I have tried searching through the internet, but canât seem to find information about this type of capability existing. Is it possible to have a callback be run upon a user exiting the webpage/refreshing the webpage?
This is exactly what I am looking for thank you!
Iâm just confused on how I would go about implementing this, as it seems this is an HTML/JS component rather than a Dash Python component. Would I need to make a separate JS file for this? Or is there a way to implement it into app.py?
Sorry for the late response!
The solution to use a separate JS file in the âassetsâ directory raises another question for me.
All the history of directories/files to be deleted through this onbeforeunload event are being logged as a list by a html.Div component in Dash, as having separate users using the app at once would mean personalized logs would be useful.
Is there a way for this JS file to get the children from this html.Div component? Assuming that the JS file containing the onbeforeunload event is coded as follows:
<!DOCTYPE html>
<html>
<body onbeforeunload="return myFunction()">
<script>
function myFunction() {
/// Take list from children of html.Div and delete files by iterating through this list
}
</script>
</body>
</html>
So basically I have an html.Div component that logs all the files/directories that are created by the user during their session. For example, if we have files with paths: file1.txt, file2.txt, file3.txt, then the html.Div component would contain the string representation of this list of files: â[file1.txt, file2.txt, file3.txt]â.
In this case, I was hoping that the onbeforeunload event would be able to pull this list from the html.Div and iterate through it in order to purge the files on unload.
However, since this is more than likely stored on the server side, your onbeforeunload would have to fire an event (clicking a button is easy) and trigger the callback on the server with the children of that div as a state, and then you could remove all of those files that way.
Iâm sorry it is now my turn to say Iâm not entirely sure what you mean by this.
So you are saying that it is not possible to pass the children of the div as a state without using a Dash callback? Would this not work if the user unloads the session by exiting or refreshing the webpage without clicking the specific button to trigger the callback?
You are asking to be notified when the browser closes, this requires an event listener on the client.
You are wanting to use this to dump the userâs data from your serverâs file system (correct?) In order to do so, youâd have to have something that triggers it to do so. An invisible button that gets triggered from the onbeforeunload like this:
So as long as I just put that one line of code into a JS file in the âassetsâ directory, it will call the callback associated with this hidden button?
I am also trying to achieve same thing ( capturing the Brower close event in dash app).
i donât know js and not getting how to use (i already placed the java script file in asset folder)
and added one callback with Input(âhiddenButtonâ,âclickâ) but my callback is not getting called .
it will be really helpful you you can give some sample code ( dash app code with callback to capture close event).
Hi Jinnyzor,
Yes i made that changes intentionally.
because when i am using âclickâ in callback , then i am getting below error message
"
Property âclickâ was used with component ID:
âhiddenButtonâ
in one of the Input items of a callback.
This ID is assigned to a dash_html_components.Button component
in the layout, which does not support this property.
This ID was used in the callback(s) for Output(s):
out.children
"
please suggest how to handle âclickâ event in dash callback.
it will be really helpful if you shared dash callback code to handle âclickâ event.
I was able to successfully implement it but I had to make a few changes to the line of code according to other StackOverflow posts I saw regarding this same issue.
Unfortunately, I have long since deleted my code for that since I found a workaround option not needing on page exit callbacks, sorry.
I can try to get a working example back up again, but I kind of forgot which changes I made.