Skip to main content

FastAPI-Amis-Admin is a high-performance, efficient and easily extensible FastAPI admin framework. Inspired by Django-admin, and has as many powerful functions as Django-admin.

Project description

简体中文 | English

项目介绍

FastAPI-Amis-Admin

fastapi-amis-admin是一个拥有高性能,高效率,易拓展的fastapi管理后台框架.
启发自Django-Admin,并且拥有不逊色于Django-Admin的强大功能.

Pytest Package version Downloads Chat on Gitter 229036692

源码 · 在线演示 · 文档 · 文档打不开?


fastapi-amis-admin是一个基于fastapi+amis开发的高性能并且高效率 web-admin 框架,使用 Python 3.7+ 并基于标准的 Python 类型提示. fastapi-amis-admin开发的初衷是为了完善fastapi应用生态, 为fastapi web应用程序快速生成一个可视化管理后台.

关键特性

  • 性能极高:基于FastAPI, 可享受FastAPI的全部优势.

  • 效率更快:完善的编码类型提示, 代码可重用性更高.

  • 支持异步和同步混合编写: ORM基于SQLModel+Sqlalchemy, 可自由定制数据库类型, 支持同步及异步模式, 可拓展性强.

  • 前后端分离: 前端由Amis渲染, 后端接口由fastapi-amis-admin自动生成, 接口可重复利用.

  • 可拓展性强: 后台页面支持Amis页面及普通html页面,开发者可以很方便的自由定制界面.

  • 自动生成API文档: 由FastAPI自动生成接口文档,方便开发者调试,以及接口分享.

核心依赖

项目组成

fastapi-amis-admin由三部分核心模块组成,其中amis, fastapi-sqlmodel-crud 可作为独立模块单独使用,amis_admin基于前者共同构建.

  • amis: 基于baidu amispydantic数据模型构建库,用于快速生成/解析amis json 数据.
  • fastapi-sqlmodel-crud: 基于FastAPI+SQLModel, 用于快速构建Create,Read,Update,Delete通用API接口.
  • admin: 启发自Django-Admin, 结合amis+fastapi-sqlmodel-crud, 用于快速构建Web Admin管理后台.

安装

pip install fastapi_amis_admin

简单示例

from fastapi import FastAPI
from fastapi_amis_admin.admin.settings import Settings
from fastapi_amis_admin.admin.site import AdminSite

# 创建FastAPI应用
app = FastAPI()

# 创建AdminSite实例
site = AdminSite(settings = Settings(database_url_async = 'sqlite+aiosqlite:///amisadmin.db'))

# 挂载后台管理系统
site.mount_app(app)

if __name__ == '__main__':
    import uvicorn

    uvicorn.run(app, debug = True)

模型管理示例

from fastapi import FastAPI
from sqlmodel import SQLModel
from fastapi_amis_admin.admin.settings import Settings
from fastapi_amis_admin.admin.site import AdminSite
from fastapi_amis_admin.admin import admin
from fastapi_amis_admin.models.fields import Field

# 创建FastAPI应用
app = FastAPI()

# 创建AdminSite实例
site = AdminSite(settings = Settings(database_url_async = 'sqlite+aiosqlite:///amisadmin.db'))

# 先创建一个SQLModel模型,详细请参考: https://sqlmodel.tiangolo.com/
class Category(SQLModel, table = True):
    id: int = Field(default = None, primary_key = True, nullable = False)
    name: str = Field(title = 'CategoryName')
    description: str = Field(default = '', title = 'Description')

# 注册ModelAdmin
@site.register_admin
class CategoryAdmin(admin.ModelAdmin):
    page_schema = '分类管理'
    # 配置管理模型
    model = Category

# 挂载后台管理系统
site.mount_app(app)

# 创建初始化数据库表
@app.on_event("startup")
async def startup():
    await site.db.async_run_sync(SQLModel.metadata.create_all, is_session = False)

if __name__ == '__main__':
    import uvicorn

    uvicorn.run(app, debug = True)

表单管理示例

from typing import Any
from fastapi import FastAPI
from pydantic import BaseModel
from starlette.requests import Request
from fastapi_amis_admin.amis.components import Form
from fastapi_amis_admin.admin import admin
from fastapi_amis_admin.admin.settings import Settings
from fastapi_amis_admin.admin.site import AdminSite
from fastapi_amis_admin.crud.schema import BaseApiOut
from fastapi_amis_admin.models.fields import Field

# 创建FastAPI应用
app = FastAPI()

# 创建AdminSite实例
site = AdminSite(settings = Settings(database_url_async = 'sqlite+aiosqlite:///amisadmin.db'))

# 注册FormAdmin
@site.register_admin
class UserLoginFormAdmin(admin.FormAdmin):
    page_schema = 'UserLoginForm'
    # 配置表单信息, 可省略
    form = Form(title = '这是一个测试登录表单', submitText = '登录')

    # 创建表单数据模型
    class schema(BaseModel):
        username: str = Field(..., title = '用户名', min_length = 3, max_length = 30)
        password: str = Field(..., title = '密码')

    # 处理表单提交数据
    async def handle(self, request: Request, data: BaseModel, **kwargs) -> BaseApiOut[Any]:
        if data.username == 'amisadmin' and data.password == 'amisadmin':
            return BaseApiOut(msg = '登录成功!', data = {'token': 'xxxxxx'})
        return BaseApiOut(status = -1, msg = '用户名或密码错误!')

# 挂载后台管理系统
site.mount_app(app)

if __name__ == '__main__':
    import uvicorn

    uvicorn.run(app, debug = True)

使用命令行

# 安装命令行拓展
pip install fastapi_amis_admin[cli]

# 查看帮助
faa --help

# 初始化一个`FastAPI-Amis-Admin`项目
faa new project_name --init

# 初始化一个`FastAPI-Amis-Admin`应用
faa new app_name

# 快速运行项目
faa run

界面预览

  • Open http://127.0.0.1:8000/admin/ in your browser:

ModelAdmin

  • Open http://127.0.0.1:8000/admin/docs in your browser:

Docs

相关项目

许可协议

  • fastapi-amis-admin基于Apache2.0开源免费使用,可以免费用于商业用途,但请在展示界面中明确显示关于FastAPI-Amis-Admin的版权信息.

鸣谢

感谢以下开发者对 FastAPI-Amis-Admin 作出的贡献:

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_amis_admin-0.2.3.tar.gz (288.4 kB view hashes)

Uploaded Source

Built Distribution

fastapi_amis_admin-0.2.3-py3-none-any.whl (82.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page