Parcourir la source

extend fastapi example to use storage in run_with

Rodja Trappe il y a 1 an
Parent
commit
62857a059d
1 fichiers modifiés avec 10 ajouts et 3 suppressions
  1. 10 3
      examples/fastapi/frontend.py

+ 10 - 3
examples/fastapi/frontend.py

@@ -1,11 +1,18 @@
 from fastapi import FastAPI
 from fastapi import FastAPI
 
 
-from nicegui import ui
+from nicegui import app, ui
 
 
 
 
-def init(app: FastAPI) -> None:
+def init(fastapi_app: FastAPI) -> None:
     @ui.page('/show')
     @ui.page('/show')
     def show():
     def show():
         ui.label('Hello, FastAPI!')
         ui.label('Hello, FastAPI!')
 
 
-    ui.run_with(app)
+        # 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='pick your private secret here'  # NOTE setting a secret is optional but allows for persistent storage per user
+    )