-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
99 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from ._avenger import SceneGraph | ||
|
||
import copy | ||
|
||
def avenger_png_renderer(spec: dict, **kwargs) -> dict: | ||
""" | ||
|
@@ -29,6 +29,71 @@ def avenger_png_renderer(spec: dict, **kwargs) -> dict: | |
return {"image/png": sg.to_png(scale=kwargs.get("scale", None))} | ||
|
||
|
||
def avenger_html_renderer(spec: dict, **kwargs) -> dict: | ||
""" | ||
Altair renderer plugin that uses Avenger to render interactive charts | ||
This function is registered as avenger-html in the altair.vegalite.v5.renderer | ||
entry point group. It may be enabled in Altair using: | ||
>>> import altair as alt | ||
>>> alt.renderers.enable('avenger-html') | ||
See https://altair-viz.github.io/user_guide/custom_renderers.html | ||
for more information | ||
""" | ||
from altair.utils.mimebundle import spec_to_mimebundle | ||
from altair import VEGA_VERSION, VEGALITE_VERSION, VEGAEMBED_VERSION | ||
import jinja2 | ||
|
||
template = jinja2.Template( | ||
"""\ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<style> | ||
#{{ output_div }}.vega-embed { | ||
width: 100%; | ||
display: flex; | ||
} | ||
#{{ output_div }}.vega-embed details, | ||
#{{ output_div }}.vega-embed details summary { | ||
position: relative; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div class="vega-visualization" id="{{ output_div }}"></div> | ||
<script type="module"> | ||
import vegaEmbed, { vega } from "https://esm.sh/vega-embed@6?deps=vega@5&[email protected]"; | ||
import { registerVegaRenderer } from "https://esm.sh/[email protected]"; | ||
registerVegaRenderer(vega.renderModule); | ||
const spec = {{ spec }}; | ||
const embedOpt = {{ embed_options }}; | ||
vegaEmbed('#{{ output_div }}', spec, embedOpt).catch(console.error); | ||
</script> | ||
</body> | ||
</html> | ||
""" | ||
) | ||
|
||
embed_options = copy.deepcopy(kwargs.get("embed_options", {})) | ||
embed_options["renderer"] = "avenger" | ||
bundle = spec_to_mimebundle( | ||
spec, | ||
format="html", | ||
mode="vega-lite", | ||
template=template, | ||
embed_options=embed_options, | ||
vega_version=VEGA_VERSION, | ||
vegaembed_version=VEGAEMBED_VERSION, | ||
vegalite_version=VEGALITE_VERSION, | ||
) | ||
return bundle | ||
|
||
|
||
def chart_to_png(chart, scale=1) -> bytes: | ||
""" | ||
Convert an altair chart to a png image bytes | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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