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()
@ui.page('/')
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(app.storage.user, 'dark_mode')
ui.checkbox('dark mode').bind_value(app.storage.user, '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 "demo.py")')