【Amis Low Code 结合FastAPI进行前端框架开发】

2023-12-10 06:12

本文主要是介绍【Amis Low Code 结合FastAPI进行前端框架开发】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

官方文档

封装思想

  • 直接复制官网json数据即可开发
  • 每个json中的接口由fastapi 转发(透传)使其开发模式与前端思维一致

基础组件

from amis import Page, Service, App
from pydantic import BaseModel, Field
from fastapi import FastAPI, Request, Response, APIRouter
from starlette.responses import HTMLResponseclass JsonRegister(BaseModel):page_json_url: str = "/demo.json"def __init__(self, **kwargs):super(JsonRegister, self).__init__(**kwargs)def register(self, router: APIRouter):router.add_api_route(self.page_json_url, endpoint=self.json)def json(self) -> {}:"""amis json格式数据"""return {}class HtmlRegister(BaseModel):page_url = "/"def __init__(self, **kwargs):super(HtmlRegister, self).__init__(**kwargs)def register(self, router: APIRouter):router.add_api_route(self.page_url, endpoint=self.html)def html(self):"""渲染html页面"""return HTMLResponse(content=Page().render())class TableRegister(JsonRegister):base_url = "/crud/"primary_key = "id"page_json_url = "/curd/model.json"primary_key_url = ""def __init__(self, **kwargs):super(TableRegister, self).__init__(**kwargs)def register(self, router: APIRouter):super(TableRegister, self).register(router=router)self.primary_key_url = self.base_url + "{" + self.primary_key + "}"router.add_api_route(self.primary_key_url, endpoint=self.get_item, methods=["GET"])router.add_api_route(self.primary_key_url, endpoint=self.delete_item, methods=["DELETE"])router.add_api_route(self.primary_key_url, endpoint=self.put_item, methods=["PUT"])router.add_api_route(self.primary_key_url, endpoint=self.options_item, methods=["OPTIONS"])router.add_api_route(self.base_url, endpoint=self.get_items, methods=["GET"])router.add_api_route(self.base_url, endpoint=self.post_item, methods=["POST"])def get_item(self, request: Request):return {}def delete_item(self, request: Request):return {}def put_item(self, request: Request):return {}def options_item(self, request: Request):return {}def get_items(self, request: Request):return []def post_item(self, request: Request):return {}
  • 上诉组件中定义
  • json格式注册器
  • html页面注册器
  • 表格增删改查注册器

常见业务功能组件封装

class RouterJson(JsonRegister):page_json_url = "/router.json"children_pages = [{"label": "父页面","url": "/parent","redirect": "/parent/pageA","children": [{"label": "子页面","url": "pageA","schemaApi": "get:/pages/crud-list.json"},{"label": "子页面","url": "pageB","schemaApi": "get:/pages/crud-list.json"}]}]def json(self) -> {}:return {"status": 0,"msg": "","data": {"pages": {"children": self.children_pages}}}class AppAdminHtml(HtmlRegister):web_url: str = "/""""获取静态页面地址"""app_brandName = "测试Demo"router_json: RouterJson = RouterJson()def html(self):app_ = App()app_.brandName = "测试Demo"app_.api = f"get:{self.router_json.page_json_url}"return HTMLResponse(app_.render())class ModelAdminJson(TableRegister):base_url = "/crud/"primary_key = "id"page_json_url = "/curd/model.json"def get_item(self, request: Request):id_ = request.get(self.primary_key)return {"name": "mock data", "id": id_}def delete_item(self, request: Request):id_ = request.get(self.primary_key)return {"name": "mock data", "id": id_}def put_item(self, request: Request):id_ = request.get(self.primary_key)return {"name": "mock data", "id": id_}def options_item(self, request: Request):id_ = request.get(self.primary_key)return {"name": "mock data", "id": id_}def get_items(self, request: Request):return [{"name": f"mock data {id_}", "id": id_} for id_ in range(10)]def post_item(self, request: Request):return {"name": "mock data", "id": 0}class LoginJson(JsonRegister):page_json_url: str = "/login.json""""获取json配置接口地址"""title: str = """""标题"""success_redirect_url = "/""""成功登陆后的重定向地址"""login_params = {"url": "${serverUrl}/login/","method": "post","dataType": "form-data"}def register(self, router: APIRouter):super(LoginJson, self).register(router=router)router.add_api_route("/login/", endpoint=self.post_item, methods=["POST"])def post_item(self, request: Request):return {"name": "mock data", "id": 0}def json(self):return Page(**{"type": "page","title": "","style": {"backgroundImage": "linear-gradient(180deg, #86a4e9, transparent)"},"cssVars": {"--Form-input-onFocused-borderColor": "#e8e9eb","--Form-input-onHover-borderColor": "#e8e9eb"},"body": {"type": "grid-2d","cols": 12,"grids": [{"x": 5,"y": 5,"h": 1,"w": 4,"width": 200,"type": "form","mode": "horizontal","title": "","api": self.login_params,"panelClassName": "p-r p-l p-b-md","redirect": self.success_redirect_url,"body": [{"type": "tpl","tpl": f"<div style='text-align: center; font-weight: bold;'><p>{self.title}</p></div>"},{"type": "input-text","label": False,"name": "userName","size": "full","placeholder": "登陆名","addOn": {"label": "","type": "text","position": "left","icon": "fa fa-user"}},{"type": "input-password","label": False,"name": "password","size": "full","placeholder": "密码","addOn": {"label": "","type": "text","position": "left","icon": "fa fa-lock"}},{"type": "checkbox","label": False,"name": "record","option": "记住密码"},{"type": "control","label": False,"body": {"type": "button","level": "primary","actionType": "submit","block": True,"label": "登陆","size": "lg"}}]}]}}).to_dict()class LoginHtml(HtmlRegister):page_url: str = "/login""""获取静态页面地址"""login_json: LoginJson = LoginJson()def html(self):return HTMLResponse(Page(body=Service(schemaApi=f"get:{self.login_json.page_json_url}")).render())

启动运行

import json
import refrom fastapi import FastAPI, Request, APIRouter
from default_template import *app = FastAPI()def main_router():router = APIRouter()login_json = LoginJson()login_json.title = "欢迎登录"login_json.success_redirect_url = "/#/home"login_json.register(router=router)"""登录json"""router_json = RouterJson()router_json.children_pages = [{"label": "首页","url": "/home","schema": {"type": "markdown","value": "# title\n markdown **text**"}},{"label": "测试页面","url": "/parent","redirect": "/parent/pageA","children": [{"label": "测试页面1","url": "pageA","schemaApi": "get:/pages/crud-list.json"},{"label": "测试页面2","url": "pageB","schemaApi": "get:/pages/crud-list.json"}]}]router_json.register(router=router)"""路由json"""login_html = LoginHtml()login_html.login_json = login_jsonlogin_html.register(router=router)"""登录页面"""app_admin_html = AppAdminHtml()app_admin_html.app_brandName = "测试应用"app_admin_html.router_json = router_jsonapp_admin_html.register(router=router)"""后台页面"""return routerapp.include_router(main_router())if __name__ == "__main__":import uvicornuvicorn.run(app, host="0.0.0.0", port=9000)
  • 可以知道当下页面中有登录界面,后台页面
  • 业务中的所有json 页面接口都可以先行注册,再注册静态页面

在这里插入图片描述

在这里插入图片描述

这篇关于【Amis Low Code 结合FastAPI进行前端框架开发】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/476235

相关文章

C#使用SQLite进行大数据量高效处理的代码示例

《C#使用SQLite进行大数据量高效处理的代码示例》在软件开发中,高效处理大数据量是一个常见且具有挑战性的任务,SQLite因其零配置、嵌入式、跨平台的特性,成为许多开发者的首选数据库,本文将深入探... 目录前言准备工作数据实体核心技术批量插入:从乌龟到猎豹的蜕变分页查询:加载百万数据异步处理:拒绝界面

Python使用自带的base64库进行base64编码和解码

《Python使用自带的base64库进行base64编码和解码》在Python中,处理数据的编码和解码是数据传输和存储中非常普遍的需求,其中,Base64是一种常用的编码方案,本文我将详细介绍如何使... 目录引言使用python的base64库进行编码和解码编码函数解码函数Base64编码的应用场景注意

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

Java进行文件格式校验的方案详解

《Java进行文件格式校验的方案详解》这篇文章主要为大家详细介绍了Java中进行文件格式校验的相关方案,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、背景异常现象原因排查用户的无心之过二、解决方案Magandroidic Number判断主流检测库对比Tika的使用区分zip

Java使用Curator进行ZooKeeper操作的详细教程

《Java使用Curator进行ZooKeeper操作的详细教程》ApacheCurator是一个基于ZooKeeper的Java客户端库,它极大地简化了使用ZooKeeper的开发工作,在分布式系统... 目录1、简述2、核心功能2.1 CuratorFramework2.2 Recipes3、示例实践3

Python Dash框架在数据可视化仪表板中的应用与实践记录

《PythonDash框架在数据可视化仪表板中的应用与实践记录》Python的PlotlyDash库提供了一种简便且强大的方式来构建和展示互动式数据仪表板,本篇文章将深入探讨如何使用Dash设计一... 目录python Dash框架在数据可视化仪表板中的应用与实践1. 什么是Plotly Dash?1.1

基于Flask框架添加多个AI模型的API并进行交互

《基于Flask框架添加多个AI模型的API并进行交互》:本文主要介绍如何基于Flask框架开发AI模型API管理系统,允许用户添加、删除不同AI模型的API密钥,感兴趣的可以了解下... 目录1. 概述2. 后端代码说明2.1 依赖库导入2.2 应用初始化2.3 API 存储字典2.4 路由函数2.5 应

Python GUI框架中的PyQt详解

《PythonGUI框架中的PyQt详解》PyQt是Python语言中最强大且广泛应用的GUI框架之一,基于Qt库的Python绑定实现,本文将深入解析PyQt的核心模块,并通过代码示例展示其应用场... 目录一、PyQt核心模块概览二、核心模块详解与示例1. QtCore - 核心基础模块2. QtWid

利用Python开发Markdown表格结构转换为Excel工具

《利用Python开发Markdown表格结构转换为Excel工具》在数据管理和文档编写过程中,我们经常使用Markdown来记录表格数据,但它没有Excel使用方便,所以本文将使用Python编写一... 目录1.完整代码2. 项目概述3. 代码解析3.1 依赖库3.2 GUI 设计3.3 解析 Mark