isolated example:
import os
import flet as ft
async def main(page: ft.Page):
list_files = os.listdir("some_files")
print(list_files) #for test
page.add(ft.Text(list_files))
page.update()
ft.app(target=main, view=None, port=44051)
Run it
I see it in the browser (chrome) at startup:
['test1.txt', 'test2.txt', 'test3.txt', 'test4.txt', 'test5.txt']
I go to "some_files" directory and delete, for example, any file.
Click the "update" button in the browser (F5):
The list of files has not changed.
An error appeared in the console:
"self._sock.shutdown(socket.SHUT_RDWR) ConnectionResetError: [WinError 10054]"
How do I make sure that when the page is refreshed in the browser, the application is restarted, in other words, it is updated honestly?
There seems to be no official documentation about this specific problem, but I suspect your problem is related to the lifecycle of flet pages. When you reload the page in the browser the "old" page still lives on behind the scenes and gets displayed. As of now there is no official way to control the lifecycle of a page!
One approach that I found is hooking the update function of the file list into the on_connect
event handler of the page
to update the file listing when a new connection gets established to this page. With this in mind, your main function could look like this:
def main(page: ft.Page):
def update(e=None):
list_files = os.listdir("some_files")
print(list_files)
page.controls.clear()
page.add(ft.Text(str(list_files)))
page.update()
page.on_connect = update
update()
Although I cannot guarantee this approach will work on all platforms due to lack of official documentation, I have tested this approach on Windows using Chrome and it works as intended.
The error you encountered is not in any way related to this issue. To be brief this error just says that the socket flet is trying to close has already been closed.
Here you can read more about this error in the answer of this question: python flat. I constantly catch an exception when I close a tab or refresh a page