Spaces:
Running
Running
from fastapi import FastAPI, HTTPException | |
from fastapi.responses import FileResponse | |
from pathlib import Path | |
app = FastAPI() | |
# Base directory where notes are stored | |
NOTES_DIR = Path("notes") | |
def read_root(): | |
return {"message": "Welcome to the Notes API!"} | |
def get_subjects(): | |
"""Get all subjects.""" | |
if not NOTES_DIR.exists(): | |
raise HTTPException(status_code=404, detail="Notes directory not found") | |
subjects = [subject.name for subject in NOTES_DIR.iterdir() if subject.is_dir()] | |
return {"subjects": subjects} | |
def get_units(subject: str): | |
"""Get all units for a specific subject.""" | |
subject_path = NOTES_DIR / subject | |
if not subject_path.exists() or not subject_path.is_dir(): | |
raise HTTPException(status_code=404, detail="Subject not found") | |
units = [unit.stem for unit in subject_path.glob("*.md")] | |
return {"units": units} | |
def get_unit_notes(subject: str, unit: str): | |
"""Get the content of a specific unit.""" | |
unit_path = NOTES_DIR / subject / f"{unit}.md" | |
if not unit_path.exists(): | |
raise HTTPException(status_code=404, detail="Unit notes not found") | |
return FileResponse(unit_path) | |
# Optional: Serve notes as plain text or rendered HTML | |
def get_unit_notes_content(subject: str, unit: str): | |
"""Get the content of a specific unit as plain text.""" | |
unit_path = NOTES_DIR / subject / f"{unit}.md" | |
if not unit_path.exists(): | |
raise HTTPException(status_code=404, detail="Unit notes not found") | |
with open(unit_path, "r", encoding="utf-8") as file: | |
content = file.read() | |
return {"content": content} | |