# python-amis
```python
import random
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse
from fastapi import Request
from fastapi.exceptions import HTTPException
from amis.amis import amis
import json
app = FastAPI()
@app.get("/")
def home():
page = amis().Page().body([
amis().Form()
.api("/add")
.title("登录")
.rules([
{"rule": "data.usernmae", "message": "用户名 为空", "name": ["username"]},
{"rule": "data.password", "message": "密码 为空", "name": "password"}
])
.body([
amis().InputText().label("用户名").name('username'),
amis().InputPassword().label("密码").name('password'),
])
.actions([
amis().Button().label("重置").actionType("reset"),
amis().Button().label("保存").actionType("submit").level("primary")
])
])
amis_html = page.to_html()
return HTMLResponse(content=amis_html, status_code=200)
@app.get("/table")
def home():
page = (amis().Page()
.type("service")
.api("/get_table")
.body([
amis().CRUD().title("表格1").source("$rows").columns([
{"name": "id", "label": "ID"},
{"name": "name", "label": "姓名"}
])
]))
amis_html = page.to_html()
return HTMLResponse(content=amis_html, status_code=200)
@app.get("/get_table")
def get_table():
# 随机生成name列表
names = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
"v", "w", "x", "y", "z"]
# 随机生成id列表
ids = [i for i in range(1, 101)]
# 随机生成name列表
names = random.choices(names, k=100)
rows = []
for i in range(100):
rows.append({
"name": names[i],
"id": ids[i]
})
return {
"status": 0,
"msg": "ok",
"data": {
"count": len(rows),
"rows": rows
}
}
@app.api_route("/{path:path}", methods=["POST", "PUT", "DELETE"])
async def handle_all_requests(request: Request):
json_data = await request.json()
print(f"请求地址: {request.url}")
print(f"请求数据: {json_data}")
message = json.dumps({
"json_data": json_data,
"url": str(request.url),
"method": request.method
})
return {
"status": 0,
"message": message,
"mydata": message,
}
# 将public目录挂载为静态文件目录
app.mount("/jssdk", StaticFiles(directory="public/jssdk", html=True), name="static")
if __name__ == "__main__":
import uvicorn
uvicorn.run("main:app", host="0.0.0.0", port=5001, reload=True)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/duolabmeng6/py-amis",
"name": "python-amis",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": "python-amis",
"author": "duolabmeng6",
"author_email": "1715109585@qq.com",
"download_url": "https://files.pythonhosted.org/packages/f9/bb/79adbbc92e7235abdbfdd9ff70ce27fdbb1a333e1dc6e32605672b32667e/python_amis-0.0.3.tar.gz",
"platform": null,
"description": "# python-amis\n\n```python\nimport random\n\nfrom fastapi import FastAPI\nfrom fastapi.staticfiles import StaticFiles\nfrom fastapi.responses import HTMLResponse\nfrom fastapi import Request\nfrom fastapi.exceptions import HTTPException\n\nfrom amis.amis import amis\nimport json\n\napp = FastAPI()\n\n\n@app.get(\"/\")\ndef home():\n page = amis().Page().body([\n amis().Form()\n .api(\"/add\")\n .title(\"\u767b\u5f55\")\n .rules([\n {\"rule\": \"data.usernmae\", \"message\": \"\u7528\u6237\u540d \u4e3a\u7a7a\", \"name\": [\"username\"]},\n {\"rule\": \"data.password\", \"message\": \"\u5bc6\u7801 \u4e3a\u7a7a\", \"name\": \"password\"}\n ])\n .body([\n amis().InputText().label(\"\u7528\u6237\u540d\").name('username'),\n amis().InputPassword().label(\"\u5bc6\u7801\").name('password'),\n ])\n .actions([\n amis().Button().label(\"\u91cd\u7f6e\").actionType(\"reset\"),\n amis().Button().label(\"\u4fdd\u5b58\").actionType(\"submit\").level(\"primary\")\n ])\n ])\n amis_html = page.to_html()\n return HTMLResponse(content=amis_html, status_code=200)\n\n\n@app.get(\"/table\")\ndef home():\n page = (amis().Page()\n .type(\"service\")\n .api(\"/get_table\")\n .body([\n amis().CRUD().title(\"\u8868\u683c1\").source(\"$rows\").columns([\n {\"name\": \"id\", \"label\": \"ID\"},\n {\"name\": \"name\", \"label\": \"\u59d3\u540d\"}\n ])\n ]))\n amis_html = page.to_html()\n return HTMLResponse(content=amis_html, status_code=200)\n\n\n@app.get(\"/get_table\")\ndef get_table():\n # \u968f\u673a\u751f\u6210name\u5217\u8868\n names = [\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\", \"j\", \"k\", \"l\", \"m\", \"n\", \"o\", \"p\", \"q\", \"r\", \"s\", \"t\", \"u\",\n \"v\", \"w\", \"x\", \"y\", \"z\"]\n # \u968f\u673a\u751f\u6210id\u5217\u8868\n ids = [i for i in range(1, 101)]\n # \u968f\u673a\u751f\u6210name\u5217\u8868\n names = random.choices(names, k=100)\n rows = []\n for i in range(100):\n rows.append({\n \"name\": names[i],\n \"id\": ids[i]\n })\n return {\n \"status\": 0,\n \"msg\": \"ok\",\n \"data\": {\n \"count\": len(rows),\n \"rows\": rows\n }\n }\n\n\n@app.api_route(\"/{path:path}\", methods=[\"POST\", \"PUT\", \"DELETE\"])\nasync def handle_all_requests(request: Request):\n json_data = await request.json()\n print(f\"\u8bf7\u6c42\u5730\u5740: {request.url}\")\n print(f\"\u8bf7\u6c42\u6570\u636e: {json_data}\")\n message = json.dumps({\n \"json_data\": json_data,\n \"url\": str(request.url),\n \"method\": request.method\n })\n return {\n \"status\": 0,\n \"message\": message,\n \"mydata\": message,\n }\n\n\n# \u5c06public\u76ee\u5f55\u6302\u8f7d\u4e3a\u9759\u6001\u6587\u4ef6\u76ee\u5f55\napp.mount(\"/jssdk\", StaticFiles(directory=\"public/jssdk\", html=True), name=\"static\")\n\nif __name__ == \"__main__\":\n import uvicorn\n\n uvicorn.run(\"main:app\", host=\"0.0.0.0\", port=5001, reload=True)\n\n\n```\n",
"bugtrack_url": null,
"license": null,
"summary": "This is the Python version of the AMIS component encapsulated by Baidu.",
"version": "0.0.3",
"project_urls": {
"Homepage": "https://github.com/duolabmeng6/py-amis"
},
"split_keywords": [
"python-amis"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "9cec0cd7209c33123b15a99d0d58e96eebe4889c641804379e144a436d4e8063",
"md5": "e43c80f877779b6dd6a91a4cf09948df",
"sha256": "e5bb56f4b266f7fa711b1a911e7c76757f66689b6353c855728874f822b6b8c0"
},
"downloads": -1,
"filename": "python_amis-0.0.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e43c80f877779b6dd6a91a4cf09948df",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 481576,
"upload_time": "2024-10-06T14:24:27",
"upload_time_iso_8601": "2024-10-06T14:24:27.529796Z",
"url": "https://files.pythonhosted.org/packages/9c/ec/0cd7209c33123b15a99d0d58e96eebe4889c641804379e144a436d4e8063/python_amis-0.0.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "f9bb79adbbc92e7235abdbfdd9ff70ce27fdbb1a333e1dc6e32605672b32667e",
"md5": "e8c3838b3ba1f4d602c3083eb7ac09b3",
"sha256": "a8d97004803ef1678b8d1c65b94ccf44d8673edacab5bae65dba7725cc5feb82"
},
"downloads": -1,
"filename": "python_amis-0.0.3.tar.gz",
"has_sig": false,
"md5_digest": "e8c3838b3ba1f4d602c3083eb7ac09b3",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 192376,
"upload_time": "2024-10-06T14:24:29",
"upload_time_iso_8601": "2024-10-06T14:24:29.419484Z",
"url": "https://files.pythonhosted.org/packages/f9/bb/79adbbc92e7235abdbfdd9ff70ce27fdbb1a333e1dc6e32605672b32667e/python_amis-0.0.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-06 14:24:29",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "duolabmeng6",
"github_project": "py-amis",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "python-amis"
}