Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
from fastapi import FastAPI, HTTPException from pydantic import BaseModel from sqlalchemy import create_engine, Column, Integer, String, select from sqlalchemy.orm import sessionmaker, declarative_base app = FastAPI() # ---------- DATABASE SETUP ---------- DATABASE_URL = "sqlite:///./projects.db" engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False}) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) Base = declarative_base() class Project(Base): __tablename__ = "projects" id = Column(Integer, primary_key=True, index=True) name = Column(String) description = Column(String) Base.metadata.create_all(bind=engine) # ---------- RESPONSE MODEL ---------- class ProjectResponse(BaseModel): id: int name: str description: str # ---------- API ENDPOINT ---------- @app.get("/projects/{project_id}", response_model=ProjectResponse) def get_project(project_id: int): db = SessionLocal() project = db.execute( select(Project).where(Project.id == project_id) ).scalar_one_or_none() if not project: raise HTTPException(status_code=404, detail="Project not found") return ProjectResponse( id=project.id, name=project.name, description=project.description )
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)