Open vieyahn2017 opened 1 year ago
https://github.com/vnurhaqiqi/fastapi-sqlite uvicorn main:app --reload
http://127.0.0.1:8000/users/ [{"email":"viqi@mail.com","id":1,"is_active":true,"home_works":[{"title":"Math","description":"Algebra Liniear","id":1,"owner_id":1},{"title":"Math","description":"Calculus","id":2,"owner_id":1},{"title":"Computer Science","description":"Artificial Intelegent","id":3,"owner_id":1}]}]
https://github.com/zhkuo24/full-stack-fastapi-demo
完全按照官方的例子来做一个FastAPI前后端分离的Demo,不使用Docker,数据库采用SQLite3
记录整个架构以及常见的问题设置等,方便以后快速生成模板
后端 依赖 FastAPI
版本管理 poetry
数据库 SQLite3和sqlalchemy
== 后端的controller好像太少。 前端npm i没成功
if __name__ == '__main__':
import uvicorn
uvicorn.run(app='main:app', host="127.0.0.1", port=8000, reload=True)
https://github.com/AgusT613/app_crud_rest_api 这个也是前后端的项目 前端纯js写的用户列表和edit/delete[todo]功能,架子搭起了,挺不错 后端db部分封装吧,但是没用sqlalchemy
fastapi用的技术栈偏好,可能还真不一样, 比如上面的例子
anyio==3.7.0
certifi==2023.5.7
charset-normalizer==3.1.0
click==8.1.3
colorama==0.4.6
fastapi==0.98.0
h11==0.14.0
httptools==0.5.0
idna==3.4
numpy==1.25.0
pandas==2.0.2
pydantic==1.10.9
python-dateutil==2.8.2
python-dotenv==1.0.0
pytz==2023.3
PyYAML==6.0
requests==2.31.0
six==1.16.0
sniffio==1.3.0
starlette==0.27.0
typing_extensions==4.6.3
tzdata==2023.3
urllib3==2.0.3
uvicorn==0.22.0
watchfiles==0.19.0
websockets==11.0.3
一些bug修复记录
pydantic.errors.PydanticUserError: Field 'DOCS_URL' defined on a base class was overridden by a non-annotated attribute. All field definitions, including overrides, require a type annotation.
uvicorn.run(app='main:app', host="127.0.0.1", port=8090, reload=True, debug=True) TypeError: run() got an unexpected keyword argument 'debug'
C:\Python37-32\lib\site-packages\pydantic_internal_config.py:269: UserWarning: Valid config keys have changed in V2:
我本地的一些记录
pip install pydantic[email] pip install eamil pip install python-jose==3.2.0 pip install python-multipart
pydantic.errors.PydanticUserError: Field 'DOCS_URL' defined on a base class was overridden by a non-annotated attribute. All field definitions, including overrides, require a type annotation.
uvicorn.run(app='main:app', host="127.0.0.1", port=8090, reload=True, debug=True) TypeError: run() got an unexpected keyword argument 'debug'
C:\Python37-32\lib\site-packages\pydantic_internal_config.py:269: UserWarning: Valid config keys have changed in V2:
fastapi sqlmodel
SQLModel 实际上是在 Pydantic 和 SQLAlchemy 之间增加了一层兼容适配,经过精心设计以兼容两者
https://github.com/jod35/FastAPI-SQLModel-crash-course https://github.com/vieyahn2017/FastAPI-SQLModel-crash-course
asgiref==3.4.1 click==8.0.1 colorama==0.4.4 fastapi==0.68.1 greenlet==1.1.1 h11==0.12.0 pydantic==1.8.2 SQLAlchemy==1.4.23 sqlalchemy2-stubs==0.0.2a14 sqlmodel==0.0.4 starlette==0.14.2 typing-extensions==3.10.0.0 uvicorn==0.15.0
为了运行FastAPI应用程序,你需要一个名为uvicorn 的ASGI服务器,因此也要使用pip安装程序安装。它还会安装uvicorn的依赖项–asgiref、click、h11和tying-extensions。
test:
header Content-Type application/json
raw: { "title": "title123", "description":"456" }
response { "id": 3, "title": "title123", "description": "456" }
这个demo确实精简!!!
models.py增加
class Environment(SQLModel,table=True):
id:Optional[int]=Field(default=None,primary_key=True)
type:int
csphostip:str
sshuser:Optional[str]=Field(default='cspuu')
sshpwd:Optional[str]=Field(default='pwd123456')
cspfloatuser: str
cspfloatport:Optional[str]=Field(default='3100')
main.py 增加
from models import Book, Environment
@app.get('/envs', response_model=List[Environment], status_code=status.HTTP_200_OK)
async def get_all_envs():
statement = select(Environment)
results = session.exec(statement).all()
return results
@app.post('/envs', response_model=Environment, status_code=status.HTTP_201_CREATED)
async def create_an_env(env: Environment):
new_item = Environment(type=env.type, csphostip=env.csphostip, cspfloatuser=env.cspfloatuser)
session.add(new_item)
session.commit()
return new_item
测试
post http://127.0.0.1:8000/envs { "type": "1", "csphostip":"1.1.1.2", "cspfloatuser":"admin" }
get http://127.0.0.1:8000/envs [{"id":1,"sshuser":"cspuu","sshpwd":"pwd123456","cspfloatport":"3100","type":1,"csphostip":"1.1.1.2","cspfloatuser":"admin"}]
另外还有 https://github.com/frankie567/fastapi-dramatiq-data-ingestion
还在用老掉牙的celery? 换dramatiq 试试- 掘金 2020年5月10日 — 下面来简单介绍下dramatiq的使用。 有一个注意事项是:因为所有的消息都必须通过网络发送,所以发送给actor的任何参数都必须是能被json编码的。
https://github.com/4linuxfun/Fastapi-Admin
前后端分离项目实现的一个后端管理框架
前端:vue3 + element plus 后端:fastapi + sqlmodel
这边找到一个重的全量方案 https://github.com/amisadmin/fastapi-amis-admin https://github.com/amisadmin/fastapi-amis-admin-demo 有图 https://github.com/amisadmin/fastapi-amis-admin-demo/tree/master/backend/upload/img
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.
FastAPI-Amis-Admin是一个高性能、高效、易于扩展的FastAPI管理框架。受django-admin的启发,具有与django-admin一样多的强大功能。
pip install fastapi pip install uvicorn
教程 第一步
运行 将其复制到main.py,打开cmd,输入uvicorn main:app --reload,即可运行。
https://zhuanlan.zhihu.com/p/344366003