Skip to content

Project Introduction

FastAPI-Amis-Admin

fastapi-amis-admin is a fastapi management background framework with high performance, high efficiency and easy expansion
Inspired by Django-Admin, and has powerful functions not inferior to Django-Admin.

Pytest Package version Downloads Chat on grid 229036692

Source code · Online demo · 文档 · Can't open the document?


fastapi-amis-admin is a high-performance and efficient web-admin framework developed based on fastapi+amis, using Python 3.7+ and based on standard Python type hints. The original intention of fastapi-amis-admin is to improve the fastapi application ecology and quickly generate a visual management background for the fastapi web application.

key features

  • High performance: Based on FastAPI, you can enjoy all the advantages of FastAPI.

  • Faster efficiency: Perfect coding type hints, code reusability is higher.

  • Support asynchronous and synchronous mixed writing: ORM is based on SQLModel+Sqlalchemy, can customize the database type freely, supports synchronous and asynchronous mode, and has strong scalability.

  • Separation of front-end and back-end: The front-end is rendered by Amis, the back-end interface is automatically generated by fastapi-amis-admin, and the interface can be reused.

  • Strong scalability: The background page supports Amis page and ordinary html page, and developers can easily customize the interface freely.

  • Automatically generate API documentation: The interface documentation is automatically generated by FastAPI, which is convenient for developers to debug and share the interface.

core dependencies

  • FastAPI responsible for the web part
  • SQLModel Responsible for ORM model mapping ( Perfect combination of SQLAlchemy+Pydantic, with SQLAlchemy and all features of Pydantic)
  • Amis Responsible for Admin background page display

Project composition

fastapi-amis-admin consists of three core modules, of which amis, fastapi-sqlmodel-crud can be used as independent modules, and amis_admin is built based on the former.

  • amis: pydantic data model building library based on baidu amis for fast generation/parsing of amis json data.
  • fastapi-sqlmodel-crud: Based on FastAPI+SQLModel, it is used to quickly build Create, Read, Update, Delete common API interfaces.
  • admin: Inspired by Django-Admin, combined with amis+fastapi-sqlmodel-crud, used to quickly build Web Admin management background.

Install

pip install fastapi_amis_admin

Simple example

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

# Create FastAPI application
app = FastAPI()

# Create AdminSite instance
site = AdminSite(settings=Settings(database_url_async='sqlite+aiosqlite:///amisadmin.db'))

# Mount the background management system
site.mount_app(app)

if __name__ == '__main__':
    import uvicorn

    uvicorn.run(app, debug=True)

Model management example

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

# Create FastAPI application
app = FastAPI()

# Create AdminSite instance
site = AdminSite(settings=Settings(database_url_async='sqlite+aiosqlite:///amisadmin.db'))


# First create a SQLModel model, please refer to: 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')


# Register ModelAdmin
@site.register_admin
class CategoryAdmin(admin.ModelAdmin):
    page_schema = 'Category Management'
    # Configuration management model
    model = Category


# Mount the background management system
site.mount_app(app)


# Create initialized database table
@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)

Form management example

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

# Create FastAPI application
app = FastAPI()

# Create AdminSite instance
site = AdminSite(settings=Settings(database_url_async='sqlite+aiosqlite:///amisadmin.db'))


# Register FormAdmin
@site.register_admin
class UserLoginFormAdmin(admin.FormAdmin):
    page_schema = 'UserLoginForm'
    # Configure form information, can be omitted
    form = Form(title='This is a test login form', submitText='Login')

    # Create form data model
    class schema(BaseModel):
        username: str = Field(..., title='username', min_length=3, max_length=30)
        password: str = Field(..., title='password')

    # Process form submission data
    async def handle(self, request: Request, data: BaseModel, **kwargs) -> BaseApiOut[Any]:
        if data.username == 'amisadmin' and data.password == 'amisadmin':
            return BaseApiOut(msg='Login successful!', data={'token': 'xxxxxx'})
        return BaseApiOut(status=-1, msg='username or password is wrong!')


# Mount the background management system
site.mount_app(app)

if __name__ == '__main__':
    import uvicorn

    uvicorn.run(app, debug=True)

Using the command line

# Install command line extensions
pip install fastapi_amis_admin[cli]

# view help
faa --help

# Initialize a `FastAPI-Amis-Admin` project
faa new project_name --init

# Initialize a `FastAPI-Amis-Admin` application
faa new app_name

# quickly run the project
It's true

Interface preview

  • 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

agreement

  • fastapi-amis-admin is based on Apache2.0 open source and free to use, and can be used for commercial purposes for free, but please clearly display the copyright information about FastAPI-Amis-Admin in the display interface.

Thanks

Thanks to the following developers for their contributions to FastAPI-Amis-Admin: