Skip to content

Commit

Permalink
get openapi docs working for figures
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverlambson committed Aug 24, 2024
1 parent fb8c034 commit ddebb3c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion bored-charts/boredcharts/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.4.0"
__version__ = "0.4.1"

from boredcharts.router import BCRouter
from boredcharts.webapp import boredcharts
Expand Down
4 changes: 2 additions & 2 deletions bored-charts/boredcharts/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __get_pydantic_core_schema__(


class BCRouter(APIRouter):
"""A FastAPI APIRouter that is specifically designed for creating chart routes.
"""A FastAPI router that turns charts into endpoints.
Usage:
Expand All @@ -65,7 +65,7 @@ def chart(
Creates a GET route for a chart, just a shorter form of the FastAPI get decorator,
your function still has to return a HTMLResponse
"""
path = f"/figure/{name}"
path = name if name.startswith("/") else f"/{name}"
return self.get(
path=path,
name=name,
Expand Down
11 changes: 7 additions & 4 deletions bored-charts/boredcharts/webapp.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
from pathlib import Path

from fastapi import APIRouter, FastAPI, Request
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from jinja2 import Environment, FileSystemLoader, StrictUndefined
from plotly.offline import get_plotlyjs

from boredcharts.jinja import figure, md_to_html, row
from boredcharts.router import BCRouter

module_root = Path(__file__).parent.absolute()


def boredcharts(
pages: Path,
figure_router: APIRouter,
figure_router: BCRouter,
*,
name: str = "bored-charts",
) -> FastAPI:
Expand All @@ -23,7 +24,7 @@ def boredcharts(
templates_root = module_root / "templates"
Path(static_root / "plotlyjs.min.js").write_text(get_plotlyjs())

app = FastAPI()
app = FastAPI(title=name)

app.mount(
"/static",
Expand Down Expand Up @@ -78,6 +79,8 @@ async def report(report_name: str, request: Request) -> HTMLResponse:
},
)

app.mount("/", figure_router)
if figure_router.tags is None or len(figure_router.tags) == 0:
figure_router.tags = ["figures"]
app.include_router(figure_router, prefix="/figures")

return app
4 changes: 2 additions & 2 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from pathlib import Path

from boredcharts.router import BCRouter
from boredcharts.webapp import boredcharts
from fastapi import APIRouter
from fastapi.testclient import TestClient


def test_healthz() -> None:
client = TestClient(
boredcharts(
pages=Path(__file__).parent / "pages",
figure_router=APIRouter(),
figure_router=BCRouter(),
)
)
response = client.get("/healthz")
Expand Down

0 comments on commit ddebb3c

Please sign in to comment.