FastAPI Integration
Build REST APIs for your solver with FastAPI.
SolverForge integrates easily with Python web frameworks and data systems.
from fastapi import FastAPI
from solverforge_legacy.solver import SolverManager
app = FastAPI()
solver_manager = SolverManager.create(solver_factory)
@app.post("/solve")
async def solve(problem: Timetable) -> str:
job_id = str(uuid.uuid4())
solver_manager.solve_and_listen(
job_id,
lambda _: problem,
on_best_solution_changed
)
return job_id
@app.get("/solution/{job_id}")
async def get_solution(job_id: str) -> Timetable:
return solver_manager.get_best_solution(job_id)
@app.delete("/solve/{job_id}")
async def stop_solving(job_id: str):
solver_manager.terminate_early(job_id)
SolverForge domain objects are standard Python dataclasses, making them easy to serialize:
import json
from dataclasses import asdict
# Serialize to JSON
json_str = json.dumps(asdict(solution))
# With Pydantic for validation
from pydantic.dataclasses import dataclass as pydantic_dataclass
@pydantic_dataclass
class TimetableDTO:
timeslots: list[TimeslotDTO]
rooms: list[RoomDTO]
lessons: list[LessonDTO]
Use any Python ORM (SQLAlchemy, Django ORM, etc.) for persistence:
The solver works with in-memory Python objects, so any data source that can produce those objects will work.
Build REST APIs for your solver with FastAPI.
JSON serialization with dataclasses and Pydantic.
Configure logging for debugging and monitoring.
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.