How can I use WatchFiles to get a callback for my specific file while running nicegui UI? #417
-
|
I am not sure how to deal with the ui.run() part. I want my callback to be thread safe for nicegui calls. To be more specific, I want to create a nicegui app for editing a file. I will have a Save button to update the file. But I want to be notified about external changes to the file to prompt the user for reload. I don't want to be notified about saves from my nicegui app. I have seen that nicegui already uses the WatchFiles module. Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
With the import watchfiles
from nicegui import app, events, ui
async def watch_my_file():
async for change in watchfiles.awatch('my_file.txt'):
print(change, flush=True)
ui.notify('File changed!')
ui.timer(0, watch_my_file, once=True)
ui.run(uvicorn_reload_excludes='my_file.txt')The |
Beta Was this translation helpful? Give feedback.
With the
ui.runparametersuvicorn_reload_dirs,uvicorn_reload_includes, anduvicorn_reload_excludesyou should be able to limit NiceGUI's file watcher so that a second watcher does not interfere:The
ui.timeris a little trick to run the asyncwatch_my_file()in a UI context so thatui.notifyknows the corresponding client.