|
@@ -11,3 +11,61 @@
|
|
|
|
|
|
直接打开 https://github.com/zauberzeug/nicegui 下 examples目录查看代码,使用比较简单。
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+main.py
|
|
|
+```
|
|
|
+import frontend
|
|
|
+from fastapi import FastAPI
|
|
|
+
|
|
|
+app = FastAPI()
|
|
|
+
|
|
|
+
|
|
|
+@app.get('/')
|
|
|
+def read_root():
|
|
|
+ return {'Hello': 'World'}
|
|
|
+
|
|
|
+
|
|
|
+frontend.init(app)
|
|
|
+
|
|
|
+```
|
|
|
+
|
|
|
+frontend.py
|
|
|
+```
|
|
|
+from fastapi import FastAPI
|
|
|
+
|
|
|
+from nicegui import app, ui
|
|
|
+
|
|
|
+
|
|
|
+def init(fastapi_app: FastAPI) -> None:
|
|
|
+ @ui.page('/show')
|
|
|
+ def show():
|
|
|
+ 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='pick your private secret here', # NOTE setting a secret is optional but allows for persistent storage per user
|
|
|
+ )
|
|
|
+
|
|
|
+```
|
|
|
+
|
|
|
+启动:
|
|
|
+```
|
|
|
+uvicorn main:app --reload --log-level debug --port 8000
|
|
|
+```
|
|
|
+
|
|
|
+
|
|
|
+## 源码分析
|
|
|
+
|
|
|
+当执行启动命令,调用 uvicorn 命令,启动一个 FastAPI 实例app,
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|