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

app.shutdown does not work with ui.run_with #3253

Open
MuuXB opened this issue Jun 20, 2024 · 2 comments
Open

app.shutdown does not work with ui.run_with #3253

MuuXB opened this issue Jun 20, 2024 · 2 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@MuuXB
Copy link

MuuXB commented Jun 20, 2024

Description

import uvicorn
from fastapi import FastAPI
from nicegui import ui, app

server = FastAPI()


def init() -> None:
    @ui.page("/")
    def main():
        ui.button('shutdown', on_click=app.shutdown)

    ui.run_with(server)


init()

if __name__ == "__main__":
    uvicorn.run(server, host="127.0.0.1", port=7778)

My app is started using fastapi. I want to stop the service through a button. How to do it?
app.shutdown will report an exception.

a

@python-and-fiction
Copy link
Contributor

python-and-fiction commented Jun 20, 2024

inspired by encode/uvicorn#1103 (comment)
you could do same as:

import uvicorn
from fastapi import FastAPI
from nicegui import ui, app
import signal

server = FastAPI()

def init() -> None:
    @ui.page("/")
    def main():
        ui.button('shutdown', on_click=lambda: signal.raise_signal(signal.SIGINT))
    ui.run_with(server)

init()

if __name__ == "__main__":
    uvicorn.run(server, host="127.0.0.1", port=7778)
@falkoschindler
Copy link
Contributor

Thanks for reporting this issue, @MuuXB!
When running with an existing FastAPI app, there seems to be no Server.instance. We should either find a reference to the uvicorn server or find another way to shutdown the app.

@falkoschindler falkoschindler added bug Something isn't working help wanted Extra attention is needed labels Jun 24, 2024
@falkoschindler falkoschindler changed the title app.shutdown exception Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
3 participants