Skip to content

Commit

Permalink
Add option to control whether to display Vega-Lite compiler warnings (#…
Browse files Browse the repository at this point in the history
…96)

* Add show_warnings option to control whether to display vega-lite compiler warnings

* can't use -s for both scale and show-warnings
  • Loading branch information
jonmmease authored Sep 4, 2023
1 parent 97ce200 commit 0bada6b
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 17 deletions.
22 changes: 16 additions & 6 deletions vl-convert-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@ lazy_static! {
/// (default to latest)
/// config (dict | None): Chart configuration object to apply during conversion
/// theme (str | None): Named theme (e.g. "dark") to apply during conversion
/// show_warnings (bool | None): Whether to print Vega-Lite compilation warnings (default false)
/// Returns:
/// dict: Vega JSON specification dict
#[pyfunction]
#[pyo3(text_signature = "(vl_spec, vl_version, config, theme)")]
#[pyo3(text_signature = "(vl_spec, vl_version, config, theme, show_warnings)")]
fn vegalite_to_vega(
vl_spec: PyObject,
vl_version: Option<&str>,
config: Option<PyObject>,
theme: Option<String>,
show_warnings: Option<bool>,
) -> PyResult<PyObject> {
let vl_spec = parse_json_spec(vl_spec)?;
let config = config.and_then(|c| parse_json_spec(c).ok());
Expand All @@ -59,6 +61,7 @@ fn vegalite_to_vega(
vl_version,
config,
theme,
show_warnings: show_warnings.unwrap_or(false),
},
)) {
Ok(vega_spec) => vega_spec,
Expand Down Expand Up @@ -111,15 +114,17 @@ fn vega_to_svg(vg_spec: PyObject) -> PyResult<String> {
/// (default to latest)
/// config (dict | None): Chart configuration object to apply during conversion
/// theme (str | None): Named theme (e.g. "dark") to apply during conversion
/// show_warnings (bool | None): Whether to print Vega-Lite compilation warnings (default false)
/// Returns:
/// str: SVG image string
#[pyfunction]
#[pyo3(text_signature = "(vl_spec, vl_version, config, theme)")]
#[pyo3(text_signature = "(vl_spec, vl_version, config, theme, show_warnings)")]
fn vegalite_to_svg(
vl_spec: PyObject,
vl_version: Option<&str>,
config: Option<PyObject>,
theme: Option<String>,
show_warnings: Option<bool>,
) -> PyResult<String> {
let vl_spec = parse_json_spec(vl_spec)?;
let config = config.and_then(|c| parse_json_spec(c).ok());
Expand All @@ -140,6 +145,7 @@ fn vegalite_to_svg(
vl_version,
config,
theme,
show_warnings: show_warnings.unwrap_or(false),
},
)) {
Ok(vega_spec) => vega_spec,
Expand Down Expand Up @@ -197,18 +203,19 @@ fn vega_to_png(vg_spec: PyObject, scale: Option<f32>, ppi: Option<f32>) -> PyRes
/// ppi (float): Pixels per inch (default 72)
/// config (dict | None): Chart configuration object to apply during conversion
/// theme (str | None): Named theme (e.g. "dark") to apply during conversion
///
/// show_warnings (bool | None): Whether to print Vega-Lite compilation warnings (default false)
/// Returns:
/// bytes: PNG image data
#[pyfunction]
#[pyo3(text_signature = "(vl_spec, vl_version, scale, ppi, config, theme)")]
#[pyo3(text_signature = "(vl_spec, vl_version, scale, ppi, config, theme, show_warnings)")]
fn vegalite_to_png(
vl_spec: PyObject,
vl_version: Option<&str>,
scale: Option<f32>,
ppi: Option<f32>,
config: Option<PyObject>,
theme: Option<String>,
show_warnings: Option<bool>,
) -> PyResult<PyObject> {
let vl_version = if let Some(vl_version) = vl_version {
VlVersion::from_str(vl_version)?
Expand All @@ -228,6 +235,7 @@ fn vegalite_to_png(
vl_version,
config,
theme,
show_warnings: show_warnings.unwrap_or(false),
},
scale,
ppi,
Expand Down Expand Up @@ -290,18 +298,19 @@ fn vega_to_jpeg(vg_spec: PyObject, scale: Option<f32>, quality: Option<u8>) -> P
/// quality (int): JPEG Quality between 0 (worst) and 100 (best). Default 90
/// config (dict | None): Chart configuration object to apply during conversion
/// theme (str | None): Named theme (e.g. "dark") to apply during conversion
///
/// show_warnings (bool | None): Whether to print Vega-Lite compilation warnings (default false)
/// Returns:
/// bytes: JPEG image data
#[pyfunction]
#[pyo3(text_signature = "(vl_spec, vl_version, scale, quality, config, theme)")]
#[pyo3(text_signature = "(vl_spec, vl_version, scale, quality, config, theme, show_warnings)")]
fn vegalite_to_jpeg(
vl_spec: PyObject,
vl_version: Option<&str>,
scale: Option<f32>,
quality: Option<u8>,
config: Option<PyObject>,
theme: Option<String>,
show_warnings: Option<bool>,
) -> PyResult<PyObject> {
let vl_version = if let Some(vl_version) = vl_version {
VlVersion::from_str(vl_version)?
Expand All @@ -321,6 +330,7 @@ fn vegalite_to_jpeg(
vl_version,
config,
theme,
show_warnings: show_warnings.unwrap_or(false),
},
scale,
quality,
Expand Down
57 changes: 53 additions & 4 deletions vl-convert-rs/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub struct VlOpts {
pub config: Option<serde_json::Value>,
pub theme: Option<String>,
pub vl_version: VlVersion,
pub show_warnings: bool,
}

fn set_json_arg(arg: serde_json::Value) -> Result<i32, AnyError> {
Expand Down Expand Up @@ -126,6 +127,42 @@ import('{vega_themes_url}').then((imported) => {{
));

self.worker.execute_script("<anon>", import_code)?;

let logger_code = ModuleCode::from(
r#"""
class WarningCollector {
constructor() {
this.warningsLogs = [];
}
level(lvl) {
if (lvl == null) return 0;
return this;
}
error(msg) {
console.error(msg);
return this;
}
warn(msg) {
this.warningsLogs.push(msg);
return this;
}
// skip info an debug
info() {
return this;
}
debug() {
return this;
}
}
"""#
.to_string(),
);
self.worker.execute_script("<anon>", logger_code)?;
self.worker.run_event_loop(false).await?;

// Override text width measurement in vega-scenegraph
Expand Down Expand Up @@ -200,7 +237,7 @@ import('{vl_url}').then((imported) => {{
// Create and initialize function string
let function_code = ModuleCode::from(format!(
r#"
function compileVegaLite_{ver_name}(vlSpec, config, theme) {{
function compileVegaLite_{ver_name}(vlSpec, config, theme, warnings) {{
let options = {{}};
// Handle config and theme
Expand All @@ -212,10 +249,14 @@ function compileVegaLite_{ver_name}(vlSpec, config, theme) {{
options["config"] = config;
}}
if (!warnings) {{
options["logger"] = new WarningCollector();
}}
return {ver_name}.compile(vlSpec, options).spec
}}
function vegaLiteToSvg_{ver_name}(vlSpec, config, theme) {{
function vegaLiteToSvg_{ver_name}(vlSpec, config, theme, warnings) {{
let options = {{}};
// Handle config and theme
Expand All @@ -227,6 +268,10 @@ function vegaLiteToSvg_{ver_name}(vlSpec, config, theme) {{
options["config"] = config;
}}
if (!warnings) {{
options["logger"] = new WarningCollector();
}}
let vgSpec = {ver_name}.compile(vlSpec, options).spec;
return vegaToSvg(vgSpec)
}}
Expand Down Expand Up @@ -381,13 +426,15 @@ function vegaLiteToSvg_{ver_name}(vlSpec, config, theme) {{
compileVegaLite_{ver_name:?}(
JSON.parse(Deno[Deno.internal].core.ops.op_get_json_arg({spec_arg_id})),
JSON.parse(Deno[Deno.internal].core.ops.op_get_json_arg({config_arg_id})),
{theme_arg}
{theme_arg},
{show_warnings},
)
"#,
ver_name = vl_opts.vl_version,
spec_arg_id = spec_arg_id,
config_arg_id = config_arg_id,
theme_arg = theme_arg,
show_warnings = vl_opts.show_warnings,
);

let value = self.execute_script_to_json(&code).await?;
Expand Down Expand Up @@ -416,7 +463,8 @@ var svg;
vegaLiteToSvg_{ver_name:?}(
JSON.parse(Deno[Deno.internal].core.ops.op_get_json_arg({spec_arg_id})),
JSON.parse(Deno[Deno.internal].core.ops.op_get_json_arg({config_arg_id})),
{theme_arg}
{theme_arg},
{show_warnings}
).then((result) => {{
svg = result;
}});
Expand All @@ -425,6 +473,7 @@ vegaLiteToSvg_{ver_name:?}(
spec_arg_id = spec_arg_id,
config_arg_id = config_arg_id,
theme_arg = theme_arg,
show_warnings = vl_opts.show_warnings,
));
self.worker.execute_script("<anon>", code)?;
self.worker.run_event_loop(false).await?;
Expand Down
2 changes: 2 additions & 0 deletions vl-convert-rs/tests/test_specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ mod test_png_theme_config {
vl_version,
theme: Some(theme.to_string()),
config: Some(json!({"background": BACKGROUND_COLOR})),
show_warnings: false
},
Some(scale),
None
Expand All @@ -415,6 +416,7 @@ mod test_png_theme_config {
vl_version,
theme: None,
config: Some(json!({"background": BACKGROUND_COLOR})),
show_warnings: false
},
Some(scale),
None
Expand Down
Loading

0 comments on commit 0bada6b

Please sign in to comment.