Can I deploy a NiceGUI app to Posit Connect?

Hi there,

I'm trying to test whether a python app built with NiceGUI will work on Posit Connect (I can't find any mention of the two together) before I get into learning it proper. NiceGUI is built on FastAPI so I hope it should work, but I'm currently stuck.

I'm just trying to upload the basic demo:

from nicegui import ui

ui.label('Hello NiceGUI!')

I'm deploying it with:
rsconnect deploy fastapi -n rconnect --entrypoint app .

The deployment works but I get the following error on the Posit Connect logs when the app tries to start

2023/10/11 22:55:43.243207611 ERROR:    Traceback (most recent call last):
2023/10/11 22:55:43.243245111   File "/opt/rstudio-connect/mnt/app/python/env/lib/python3.9/site-packages/starlette/", line 677, in lifespan
2023/10/11 22:55:43.243275296     async with self.lifespan_context(app) as maybe_state:
2023/10/11 22:55:43.243276348   File "/opt/rstudio-connect/mnt/app/python/env/lib/python3.9/site-packages/starlette/", line 566, in __aenter__
2023/10/11 22:55:43.243296243     await self._router.startup()
2023/10/11 22:55:43.243306350   File "/opt/rstudio-connect/mnt/app/python/env/lib/python3.9/site-packages/starlette/", line 656, in startup
2023/10/11 22:55:43.243311926     handler()
2023/10/11 22:55:43.243312770   File "/opt/rstudio-connect/mnt/app/python/env/lib/python3.9/site-packages/nicegui/", line 75, in handle_startup
2023/10/11 22:55:43.243317822     raise RuntimeError('\n\n'
2023/10/11 22:55:43.243318250 RuntimeError: 
2023/10/11 22:55:43.243322835 
2023/10/11 22:55:43.243323595 You must call to start the server.
2023/10/11 22:55:43.243330081 If is behind a main guard
2023/10/11 22:55:43.243330511    if __name__ == "__main__":
2023/10/11 22:55:43.243334291 remove the guard or replace it with
2023/10/11 22:55:43.243334726    if __name__ in {"__main__", "__mp_main__"}:
2023/10/11 22:55:43.243338471 to allow for multiprocessing.
2023/10/11 22:55:43.243338936 

I've also tried

from nicegui import ui

ui.label('Hello NiceGUI!')

app =

and deploying it with:
rsconnect deploy fastapi -n rconnect --entrypoint app:app .

Any help would be greatly appreciated

In the NiceGUI documentation under server hosting, there is a reference to using a custom fastapi app to deploy:

I would try that.

Thanks @edavidaja, that worked!

As discussed in How to deploy to Posit Connect · zauberzeug/nicegui · Discussion #1787 · GitHub the example can be further simplified to

from fastapi import FastAPI
from nicegui import app, ui

fastapi_app = FastAPI()'/')
def index():
    ui.label('Hello, FastAPI!')
    # NOTE dark mode will be persistent for each user across tabs and server restarts
    ui.dark_mode().bind_value(, 'dark_mode')
    ui.checkbox('dark mode').bind_value(, 'dark_mode')

ui.run_with(fastapi_app, storage_secret='secret')

if __name__ == '__main__':
    print('Please start with "uvicorn demo:fastapi_app --reload" (if your file is named "")')

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.