Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui.run(native=True) does not exit python execution when exiting native app. #3931

Open
henrytheodoroff opened this issue Oct 30, 2024 · 8 comments

Comments

@henrytheodoroff
Copy link

henrytheodoroff commented Oct 30, 2024

Description

from nicegui import ui

ui.label('page with custom title')

ui.run(title='My App', native=True)
print("Done!")

When running, I get this output while the native app is open (not on a browser):
image

Then, after pressing the close button on the app, the console still looks the same as above, and python is still running.

I expect the print("Done!") to not execute until after the app is closed, and then I expect python to halt execution, but it doesn't. Can someone please point me to how python ends on exit of native app? Thanks in advance.

I am running python 3.11.6 and nicegui 2.5.0.

@henrytheodoroff
Copy link
Author

I am also not wanting to ctrl + c in the terminal. I want to be able to run this and not have manual interference to stop python.

Also, why does "Done!" get printed twice?

@rodja
Copy link
Member

rodja commented Oct 30, 2024

@henrytheodoroff
Copy link
Author

I see that now, but my main question still stands. Do I have to shutdown the server manually?

@rodja
Copy link
Member

rodja commented Nov 1, 2024

I agree. The program should exit automatically if you close the main window. And, at least on OS X, it does exactly that. And with the following modifications you'll only see a single "Done" printed after the window is closed:

from nicegui import ui

ui.label('page with custom title')

if __name__ == '__main__':
    ui.run(reload=False, native=True)
    print("Done!")

@Lexstok
Copy link

Lexstok commented Dec 10, 2024

You can shutdown the server with a button, if that helps ? Or just call it as a function when you decide that the program is finished.

from nicegui import ui, app
from datetime import datetime

ui.button('shutdown', on_click=app.shutdown)
app.on_shutdown(lambda: print(f'shutdown:  {datetime.now()}'))

ui.run(native=True)

@07rohanraj
Copy link

07rohanraj commented Jan 6, 2025

when we close the app it's running in the background in the task manager. Can I use it for this purpose.

@Lexstok
Copy link

Lexstok commented Jan 6, 2025

Yes, it should. You should just try it !

@07rohanraj
Copy link

Okay, but will trigger this app.shutdown event, in your case it's button in the UI but i want it to trigger when window close X button clicked:
I was trying with signal like SIGTERM, but it's not working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants