-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
router can directly serialise plotly Figures
- Loading branch information
1 parent
e6a05c3
commit c3ad52b
Showing
7 changed files
with
93 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,74 @@ | ||
from collections.abc import Callable | ||
from typing import Any | ||
|
||
import plotly.graph_objects as go | ||
from fastapi import APIRouter | ||
from fastapi.responses import HTMLResponse | ||
from fastapi.types import DecoratedCallable | ||
from pydantic import GetCoreSchemaHandler | ||
from pydantic_core import CoreSchema, core_schema | ||
|
||
from boredcharts.jinja import to_html | ||
|
||
|
||
def validate_figure(fig: Any) -> go.Figure: | ||
assert isinstance(fig, go.Figure) | ||
return fig | ||
|
||
|
||
class HTMLFigure(go.Figure): # type: ignore[misc] | ||
"""A Plotly Figure that Pydantic can understand and serialize. | ||
This allows us to return a Plotly Figure from a FastAPI route. | ||
""" | ||
|
||
@classmethod | ||
def __get_pydantic_core_schema__( | ||
cls, _source_type: Any, _handler: GetCoreSchemaHandler | ||
) -> CoreSchema: | ||
return core_schema.json_or_python_schema( | ||
json_schema=core_schema.any_schema(), | ||
python_schema=core_schema.union_schema( | ||
[ | ||
core_schema.is_instance_schema(go.Figure), | ||
core_schema.any_schema(), | ||
] | ||
), | ||
serialization=core_schema.plain_serializer_function_ser_schema( | ||
lambda instance: to_html(instance) | ||
), | ||
) | ||
|
||
|
||
class BCRouter(APIRouter): | ||
"""A FastAPI APIRouter that is specifically designed for creating chart routes. | ||
Usage: | ||
```py | ||
from boredcharts import BCRouter | ||
import plotly.graph_objects as go | ||
router = BCRouter() | ||
@router.chart("my_chart") | ||
async def my_chart() -> go.Figure: | ||
return go.Figure() | ||
``` | ||
""" | ||
|
||
def chart( | ||
self, | ||
name: str, | ||
) -> Callable[[DecoratedCallable], DecoratedCallable]: | ||
""" | ||
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}" | ||
return self.api_route( | ||
return self.get( | ||
path=path, | ||
name=name, | ||
response_model=HTMLFigure, | ||
response_class=HTMLResponse, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters