-
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
45 changed files
with
1,646 additions
and
132 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 |
---|---|---|
|
@@ -11,3 +11,4 @@ scratch/ | |
# pixi environments | ||
.pixi | ||
|
||
/avenger-python/avenger/_avenger.abi3.so |
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
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,20 @@ | ||
[package] | ||
name = "avenger-python" | ||
version = "0.0.1" | ||
edition = "2021" | ||
license = "BSD-3-Clause" | ||
publish = false | ||
|
||
[lib] | ||
name = "avenger" | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
avenger = { path = "../avenger", features = ["pyo3"] } | ||
avenger-vega = { path = "../avenger-vega", features = ["pyo3"] } | ||
avenger-wgpu = { path = "../avenger-wgpu", features = ["pyo3"] } | ||
pyo3 = { workspace = true, features = ["extension-module", "abi3-py38"] } | ||
pythonize = "0.20.0" | ||
serde = {workspace = true} | ||
pollster = "0.3" | ||
image = {workspace = true} |
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,6 @@ | ||
from ._avenger import * | ||
from . import altair_utils | ||
|
||
# Re-export public members of the Rust _avenger modules | ||
if hasattr(_avenger, "__all__"): | ||
__all__ = _avenger.__all__ |
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,48 @@ | ||
from ._avenger import SceneGraph | ||
|
||
|
||
def avenger_png_renderer(spec: dict, **kwargs) -> dict: | ||
""" | ||
Altair renderer plugin that uses Avenger to render charts to PNG | ||
This function is registered as avenger-png in the altair.vegalite.v5.renderer | ||
entry point group. It may be enabled in Altair using: | ||
>>> import altair as alt | ||
>>> alt.renderers.enable('avenger-png') | ||
See https://altair-viz.github.io/user_guide/custom_renderers.html | ||
for more information | ||
""" | ||
import altair as alt | ||
import vl_convert as vlc | ||
|
||
if alt.data_transformers.active == "vegafusion": | ||
# When the vegafusion transformer is enabled we convert the spec to | ||
# Vega, which will include the pre-transformed inline data | ||
vg_spec = alt.Chart.from_dict(spec).to_dict(format="vega") | ||
vega_sg = vlc.vega_to_scenegraph(vg_spec) | ||
else: | ||
vega_sg = vlc.vegalite_to_scenegraph(spec) | ||
|
||
sg = SceneGraph.from_vega_scenegraph(vega_sg) | ||
return {"image/png": sg.to_png(scale=kwargs.get("scale", None))} | ||
|
||
|
||
def chart_to_png(chart, scale=1) -> bytes: | ||
""" | ||
Convert an altair chart to a png image bytes | ||
:param chart: Altair Chart | ||
:param scale: Scale factor (default 1.0) | ||
:return: png image bytes | ||
""" | ||
import altair as alt | ||
import vl_convert as vlc | ||
if alt.data_transformers.active == "vegafusion": | ||
vg_spec = chart.to_dict(format="vega") | ||
vega_sg = vlc.vega_to_scenegraph(vg_spec) | ||
else: | ||
vl_spec = chart.to_dict(format="vega-lite") | ||
vega_sg = vlc.vegalite_to_scenegraph(vl_spec) | ||
sg = SceneGraph.from_vega_scenegraph(vega_sg) | ||
return sg.to_png(scale=scale) |
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,14 @@ | ||
[project] | ||
name = "avenger" | ||
version = "0.0.1" | ||
|
||
[tool.maturin] | ||
module-name = "avenger._avenger" | ||
|
||
[build-system] | ||
requires = ["maturin>=1.1.0,<2"] | ||
build-backend = "maturin" | ||
|
||
# Register Altair renderer plugin | ||
[project.entry-points."altair.vegalite.v5.renderer"] | ||
avenger-png = "avenger.altair_utils:avenger_png_renderer" |
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,50 @@ | ||
use avenger::scene_graph::SceneGraph as RsSceneGraph; | ||
use avenger_vega::scene_graph::VegaSceneGraph; | ||
use avenger_wgpu::canvas::{Canvas, CanvasDimensions, PngCanvas}; | ||
use image::{EncodableLayout, ImageOutputFormat}; | ||
use pyo3::exceptions::PyValueError; | ||
use pyo3::prelude::*; | ||
use pyo3::types::PyBytes; | ||
use pythonize::depythonize; | ||
use std::io::Cursor; | ||
|
||
#[pyclass] | ||
pub struct SceneGraph { | ||
inner: RsSceneGraph, | ||
} | ||
|
||
#[pymethods] | ||
impl SceneGraph { | ||
#[staticmethod] | ||
fn from_vega_scenegraph(vega_sg: &PyAny) -> PyResult<Self> { | ||
let vega_sg: VegaSceneGraph = depythonize(vega_sg)?; | ||
let inner = vega_sg.to_scene_graph()?; | ||
Ok(Self { inner }) | ||
} | ||
|
||
fn to_png(&mut self, py: Python, scale: Option<f32>) -> PyResult<PyObject> { | ||
let mut png_canvas = pollster::block_on(PngCanvas::new(CanvasDimensions { | ||
size: [self.inner.width, self.inner.height], | ||
scale: scale.unwrap_or(1.0), | ||
}))?; | ||
|
||
png_canvas.set_scene(&self.inner)?; | ||
|
||
let img = pollster::block_on(png_canvas.render())?; | ||
let mut png_data = Vec::new(); | ||
|
||
img.write_to(&mut Cursor::new(&mut png_data), ImageOutputFormat::Png) | ||
.map_err(|err| { | ||
PyValueError::new_err(format!("Failed to convert image to PNG: {err:?}")) | ||
})?; | ||
|
||
Ok(PyObject::from(PyBytes::new(py, png_data.as_bytes()))) | ||
} | ||
} | ||
|
||
#[pymodule] | ||
fn _avenger(_py: Python, m: &PyModule) -> PyResult<()> { | ||
m.add_class::<SceneGraph>()?; | ||
m.add("__version__", env!("CARGO_PKG_VERSION"))?; | ||
Ok(()) | ||
} |
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
Oops, something went wrong.