from fastapi import APIRouter
router = APIRouter()
from fastapi import APIRouter
router = APIRouter()
@router.get("/", tags=["${1:tag}"])
async def ${2:route_name}():
return {"message": "${3:Hello World}"}
from fastapi import APIRouter
router = APIRouter()
@router.post("/", tags=["${1:tag}"])
async def ${2:route_name}():
return {"message": "${3:Hello World}"}
from fastapi import APIRouter
router = APIRouter()
@router.put("/", tags=["${1:tag}"])
async def ${2:route_name}():
return {"message": "${3:Hello World}"}
from fastapi import APIRouter
router = APIRouter()
@router.delete("/", tags=["${1:tag}"])
async def ${2:route_name}():
return {"message": "${3:Hello World}"}
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class ${1:ModelName}(Base):
__tablename__ = "${2:table_name}"
id = Column(Integer, primary_key=True, index=True)
${3:column_name} = Column(String)
from sqlalchemy.orm import Session
from . import models
def ${1:func_name}(db: Session, ${2:args}):
return db.query(models.${3:ModelName}).filter(models.${3:ModelName}.${4:column_name} == ${5:arg})
from sqlalchemy.orm import Session
from . import models
def ${1:func_name}(db: Session, ${2:args}):
${3:model_name} = models.${4:ModelName}(${5:args})
db.add(${3:model_name})
db.commit()
db.refresh(${3:model_name})
return ${3:model_name}
from sqlalchemy.orm import Session
from . import models
def ${1:func_name}(db: Session, ${2:args}):
${3:model_name} = db.query(models.${4:ModelName}).filter(models.${4:ModelName}.${5:column_name} == ${6:arg}).first()
${3:model_name}.${7:column_name} = ${8:arg}
db.commit()
db.refresh(${3:model_name})
return ${3:model_name}
from pydantic import BaseModel
class ${1:ModelName}(BaseModel):
${2:column_name}: ${3:str}
Certainly! Here's an updated README based on the provided code snippets:
This repository provides a collection of Visual Studio Code snippets for FastAPI development. These snippets can help you write FastAPI applications more efficiently.
from fastapi import APIRouter
router = APIRouter()
from fastapi import APIRouter
router = APIRouter()
@router.get("/", tags=["${1:tag}"])
async def ${2:route_name}():
return {"message": "${3:Hello World}"}
from fastapi import APIRouter
router = APIRouter()
@router.post("/", tags=["${1:tag}"])
async def ${2:route_name}():
return {"message": "${3:Hello World}"}
from fastapi import APIRouter
router = APIRouter()
@router.put("/", tags=["${1:tag}"])
async def ${2:route_name}():
return {"message": "${3:Hello World}"}
from fastapi import APIRouter
router = APIRouter()
@router.delete("/", tags=["${1:tag}"])
async def ${2:route_name}():
return {"message": "${3:Hello World}"}
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class ${1:ModelName}(Base):
__tablename__ = "${2:table_name}"
id = Column(Integer, primary_key=True, index=True)
${3:column_name} = Column(String)
from sqlalchemy.orm import Session
from . import models
def ${1:func_name}(db: Session, ${2:args}):
return db.query(models.${3:ModelName}).filter(models.${3:ModelName}.${4:column_name} == ${5:arg})
from sqlalchemy.orm import Session
from . import models
def ${1:func_name}(db: Session, ${2:args}):
${3:model_name} = models.${4:ModelName}(${5:args})
db.add(${3:model_name})
db.commit()
db.refresh(${3:model_name})
return ${3:model_name}
from sqlalchemy.orm import Session
from . import models
def ${1:func_name}(db: Session, ${2:args}):
${3:model_name} = db.query(models.${4:ModelName}).filter(models.${4:ModelName}.${5:column_name} == ${6:arg}).first()
${3:model_name}.${7:column_name} = ${8:arg}
db.commit()
db.refresh(${3:model_name})
return ${3:model_name}
from pydantic import BaseModel
class ${1:ModelName}(BaseModel):
${2:column_name}: ${3:str}
raise HTTPException(status_code=400, detail="${1:detail}")
raise HTTPException(status_code=404, detail="${1:detail}")
raise HTTPException(status_code=500, detail="${1:detail}")
raise HTTPException(status_code=401, detail="${1:detail}")
raise HTTPException(status_code=403, detail="${1:detail}")
raise HTTPException(status_code=422, detail="${1:detail}")
raise HTTPException(status_code=409, detail="${1:detail}")
raise HTTPException(status_code=405, detail="${1:detail}")
raise HTTPException(status_code=406, detail="${1:detail}")
raise HTTPException(status_code=500, detail="${1:detail}")
raise HTTPException(status_code=503, detail="${1:detail}")
raise HTTPException(status_code=501, detail="${1:detail}")
raise HTTPException(status_code=502, detail="${1:detail}")
raise HTTPException(status_code=504, detail="${1:detail}")
Feel free to use these snippets to streamline your FastAPI development process. If you have any suggestions or improvements, feel free to contribute!
Prefix | Description |
---|---|
frt | FastAPI Router |
frg | FastAPI Get Route |
frp | FastAPI Post Route |
frpu | FastAPI Put Route |
frd | FastAPI Delete Route |
sqlm | SQLAlchemy Model |
sqlg | SQL Get From Model |
sqlc | SQL Create From Model |
sqlu | SQL Update From Model |
pym | Pydantic Model |
fa400 | Raise FastAPI 400 Exception |
fa401 | Raise FastAPI 401 Exception |
fa403 | Raise FastAPI 403 Exception |
fa404 | Raise FastAPI 404 Exception |
fa422 | Raise FastAPI 422 Exception |
fa409 | Raise FastAPI 409 Exception |
fa405 | Raise FastAPI 405 Exception |
fa406 | Raise FastAPI 406 Exception |
fa500 | Raise FastAPI 500 Exception |
fa503 | Raise FastAPI 503 Exception |
fa501 | Raise FastAPI 501 Exception |
fa502 | Raise FastAPI 502 Exception |
fa504 | Raise FastAPI 504 Exception |