-
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.
global functions for figure & row, don't need to import in templates
- Loading branch information
1 parent
637d31e
commit 1bef202
Showing
5 changed files
with
91 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from textwrap import dedent | ||
from typing import Any | ||
|
||
import markdown | ||
from fastapi import Request | ||
from jinja2 import Undefined, pass_context | ||
from jinja2.runtime import Context | ||
from markupsafe import Markup | ||
from plotly.graph_objects import Figure | ||
|
||
|
||
def to_html(fig: Figure) -> Markup: | ||
"""Renders a Plotly Figure to an HTML string.""" | ||
if not isinstance(fig, Figure): | ||
raise ValueError(f"Input must be a Plotly Figure, got {type(fig)}") | ||
return Markup( | ||
fig.to_html( | ||
full_html=False, | ||
include_plotlyjs=False, | ||
default_height="100%", | ||
default_width="100%", | ||
config={ | ||
"displaylogo": False, | ||
"responsive": True, | ||
"displayModeBar": False, | ||
}, | ||
) | ||
) | ||
|
||
|
||
def md_to_html(md: str) -> Markup: | ||
"""Renders a Markdown string to HTML.""" | ||
return Markup(markdown.markdown(md)) | ||
|
||
|
||
@pass_context | ||
def figure( | ||
context: Context, | ||
figure: str, | ||
*, | ||
css_class: str = "min-h-112 min-w-80", | ||
**kwargs: dict[str, Any], | ||
) -> Markup: | ||
"""""" | ||
report = context.resolve("report") | ||
if isinstance(report, Undefined): | ||
raise ValueError("report is not available in the context") | ||
if not isinstance(report, str): | ||
raise ValueError(f"report must be a string, got {type(report)}") | ||
|
||
request = context.resolve("request") | ||
if isinstance(request, Undefined): | ||
raise ValueError("request is not available in the context") | ||
if not isinstance(request, Request): | ||
raise ValueError(f"request must be a Request, got {type(request)}") | ||
|
||
url = request.url_for(figure, report_name=report).include_query_params(**kwargs) | ||
|
||
return Markup( | ||
dedent(f""" | ||
<div | ||
hx-ext="response-targets" | ||
class="not-prose {css_class} flex flex-1 items-stretch" | ||
> | ||
<figure | ||
class="plotly-container min-h-0 min-w-0 flex-1" | ||
hx-get="{url}" | ||
hx-trigger="load" | ||
hx-swap="innerHTML" | ||
hx-target-error="find div" | ||
> | ||
<div class="flex h-full w-full items-center justify-center bg-stone-100"> | ||
<p>loading...</p> | ||
</div> | ||
</figure> | ||
</div> | ||
""") | ||
) | ||
|
||
|
||
def row(*figures: Markup) -> Markup: | ||
return Markup( | ||
dedent(f""" | ||
<div class="flex flex-wrap"> | ||
{"\n".join(figures)} | ||
</div> | ||
""") | ||
) |
This file was deleted.
Oops, something went wrong.
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,5 +1,3 @@ | ||
{% from "components/figure.html" import figure, row with context %} | ||
|
||
August 2024 | ||
|
||
## Summary | ||
|
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