NiceGUI的管理系统模板
- 数据库:SQLite
- 数据库ORM:SQLAlchemy
- 后端:FastAPI
开发中... 不稳定...
main.py -> app -> app.create_app() -> init_db -> init_frontend -> init_backend
setting => .env
default username: [email protected] password: passw0rd
Pycharm使用FastAPI类型的方式启动,可以使用参数--reload
,每次修改代码会自动重启(Pycharm有问题,不能自动重启)
参数:
application file : \NiceGUI-Manage-Template\app\app.py
application name : app
Uvicorn options: --reload
或者使用命令行启动: uvicorn app.app:app --reload
pip install -r requriements.txt
依赖
- NiceGUI
- SQLAlchemy
- FastAPI
- FastAPIUsers
pip install 'fastapi-users[sqlalchemy]'
- aiosqlite Sqlite for AsyncIO
pip install aiosqlite
- alembic
参考了以下项目
- https:/thekingofhero2/nicegui_login_template
- https:/zauberzeug/nicegui/tree/main/examples/fastapi
- https:/ffillouxdev/NiceGui_project-portfolio
- https:/chatpire/chatgpt-web-share
- https://gist.github.com/ProbablyBrian/5a47e6ee2242eb0edb572d07b54dcd73
- fastapi-users/fastapi-users#1361
TODO
- 添加sidebar
- 自动注册页面、路由: 新增页面可以自动注册~~顶部菜单/~~侧边菜单
- 自定义侧边栏
- 顶部栏右侧镶嵌用户信息
- 访问记录\日志
- 更改密码\更改邮箱和用户名
- 增加系统配置表和权限表
- 引入权限控制pycasbin
- 注册用户\用户登出
- 用户管理页面
- 增加首页\WIKI页
- 增加各组件示例页
- FastAPI Token认证接口
- 简单表格增删改查示例
- 简单图表示例
- 完善使用示例
- DockerFile
- 适配PostgreSQL数据库
继承app.frontend.module_base.BaseClass
, 实现__init__(self)
,在__init__中添加页面
使用注解app.frontend.router.Router.page
,页面函数需要使用async.
from app.frontend.module_base import BaseClass
@BaseClass.router.page(path='/a', title='A')
async def page():
pass
使用了该注解的函数,会被app/frontend/router.py
的app.frontend.router.Router
接管.
该注解将会调用nicegui.page.page,相当于@ui.page(path='/')
. 并且会额外的将path添加到Router的routes中,用于在单页应用中跳转.
对应title会被添加到顶部菜单,path是对应的路由.
在点击顶部菜单时,会根据path找到对应的函数,并实现页面内加载.
在通过路由直接进入时,会根据path找到对应的函数,并加载到对应函数页面.
NiceGUI的鉴权方式使用NiceGUI自身的存储app.storage.user
登录成功后,在app.storage.user中存储用户信息
需要鉴权的页面使用:
from app.db.users import current_authenticated_user_nicegui
@ui.page('/')
def show(request: Request, user: User = Depends(current_authenticated_user_nicegui)):
pass
FastAPI的接口使用FastAPIUsers
from app.db.users import current_active_user
@app.get("/authenticated-route")
async def authenticated_route(user: User = Depends(current_active_user)):
return {"message": f"Hello {user.email}!"}
其鉴权方式为:
auth_backend = AuthenticationBackend(
name="jwt",
transport=bearer_transport,
get_strategy=get_jwt_strategy,
)
TODO token auth