Browse Source

Automatic Commit By liuyuqi

liuyuqi-dellpc 9 months ago
parent
commit
bc6280513d
8 changed files with 88 additions and 71 deletions
  1. 11 0
      .env
  2. 3 71
      README.md
  3. 1 0
      apps/__init__.py
  4. 11 0
      apps/app.py
  5. 22 0
      apps/config.py
  6. 17 0
      apps/frontend.py
  7. 15 0
      main.py
  8. 8 0
      requirements.txt

+ 11 - 0
.env

@@ -0,0 +1,11 @@
+APP_NAME=nicegui-test
+PORT=3000
+HOST=localhost
+
+# database config
+DB_HOST=localhost
+DB_PORT=27017
+DB_NAME=test
+DB_USER=test
+DB_PASS=<PASSWORD>
+

+ 3 - 71
README.md

@@ -1,6 +1,6 @@
 # nicegui
 # nicegui
 
 
-这个项目有点类似 [gradio](https://git.yoqi.me/python/gradio),python 快速开发web前后端项目。
+这个项目有点类似 [gradio](https://git.yoqi.me/python/gradio),python 快速开发web前后端项目。nicegui+fastapi
 
 
 用户无需编写css,html,直接通过python代码快速布局,并使用 **fastapi** ,快速打通前后端。此外对接**sqlite**数据库。
 用户无需编写css,html,直接通过python代码快速布局,并使用 **fastapi** ,快速打通前后端。此外对接**sqlite**数据库。
 
 
@@ -11,54 +11,6 @@
 
 
 直接打开 https://github.com/zauberzeug/nicegui 下 examples目录查看代码,使用比较简单。
 直接打开 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
-```
-
-
 ## 源码分析
 ## 源码分析
 
 
 首先 **nicegui 基于fastapi开发web框架**。
 首先 **nicegui 基于fastapi开发web框架**。
@@ -72,9 +24,6 @@ ui.label('Hello, FastAPI!')
 代码,如何在页面上生成一个div呢?
 代码,如何在页面上生成一个div呢?
 
 
 
 
-
-
-
 **1、首先nicegui启动app实例:**
 **1、首先nicegui启动app实例:**
 
 
 ```
 ```
@@ -112,24 +61,7 @@ def on_event(sid: str, msg: Dict) -> None:
     handle_event(client, msg)
     handle_event(client, msg)
 ```
 ```
 
 
+## Reference
 
 
-
-2、
-
-```
-    @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')
-
-```
-
-
-
-
-
-
+- [zdimension/plexdlweb](https://github.com/zdimension/plexdlweb)4
 
 

+ 1 - 0
apps/__init__.py

@@ -0,0 +1 @@
+from .app import app

+ 11 - 0
apps/app.py

@@ -0,0 +1,11 @@
+
+import apps.frontend
+from fastapi import FastAPI
+
+app = FastAPI()
+
+@app.get('/')
+def read_root():
+    return {'Hello': 'World'}
+# fastapi insert nicegui
+frontend.init(app)

+ 22 - 0
apps/config.py

@@ -0,0 +1,22 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+'''
+@Contact :   liuyuqi.gov@msn.cn
+@Time    :   2024/07/29 13:48:28
+@License :   Copyright © 2017-2022 liuyuqi. All Rights Reserved.
+@Desc    :   config
+'''
+import os
+class Settings(object):
+    """ settings for app """
+    # read config from .env
+    if not os.path.exists('.env'):
+        raise Exception('please create.env file')
+    with open('.env') as f:
+        for line in f:
+            if line.startswith('FLY_'):
+                k, v = line.strip().split('=')
+                os.environ[k] = v
+
+
+settings = Settings()

+ 17 - 0
apps/frontend.py

@@ -0,0 +1,17 @@
+from fastapi import FastAPI
+from nicegui import app, ui, context
+
+def init(fastapi_app: FastAPI) -> None:
+    @ui.page('/show')
+    async 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')
+        app.add_static_files("/scripts", "scripts")
+
+    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
+    )

+ 15 - 0
main.py

@@ -0,0 +1,15 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+'''
+@Contact :   liuyuqi.gov@msn.cn
+@Time    :   2024/07/29 01:40:10
+@License :   Copyright © 2017-2022 liuyuqi. All Rights Reserved.
+@Desc    :   enter point
+'''
+from apps import app
+
+if __name__=='__main__':
+    from nicegui import start, ui
+    start()
+    # ui.run(title='An experimental task generator', favicon="favicon.ico", reload='FLY_ALLOC_ID' not in os.environ)
+    # ui.run(host=webui_ip, port=webui_port, title=webui_title, language="zh-CN", dark=False, reload=False)

+ 8 - 0
requirements.txt

@@ -0,0 +1,8 @@
+nicegui
+fastapi
+gunicorn
+websockets
+sqlmodel
+sqlalchemy
+pydantic
+