-
Notifications
You must be signed in to change notification settings - Fork 33
Add support for passing options to vega CLI utils via vega_cli_options
kwarg
#52
Add support for passing options to vega CLI utils via vega_cli_options
kwarg
#52
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good – a few comments.
One larger question: do the CLI commands all accept the same flags? Is a single list applied to all commands the best approach here?
They have the same interface, so I think it's worth keeping this simple? I don't really think you'd want to vary any of the params per renderer except for maybe These are the options for
|
@jakevdp I fixed the typo (oops!) and added a commit to only pass I don't really love this solution though. It seems like it'd be cleaner to pass along to |
Thanks – I think we should avoid special-casing for subclasses within the base class. Probably a better approach is to let the subclasses accept arbitrary kwargs and ignore any unrecognized/unsupported ones. |
OK, I've refactored to have all of the subclasses take a Let me know if this looks good! |
Looks good! Last issue is to add some tests. I think we want two basic tests of this new behavior:
This will probably involve some mocking; if you're not familiar with mocking in pytest, search for |
Added some tests. Tried to mimic the style and mocking set up you have already, but let me know if they need tweaks (and also do feel free to just tweak directly if easier). A couple of the tests felt a little awkward/verbose but needed extra runtime checks because of MyPy. |
Thanks - in the new test within |
These do shell out to I think this addresses all of your comments @jakevdp. Let me know if you need anything else to merge. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bunch of minor comments; once the tests are passing I think this is good to go!
Thanks so much for your work on this.
altair_saver/_core.py
Outdated
@@ -162,7 +165,7 @@ def save( | |||
embed_options = alt.renderers.options.get("embed_options", None) | |||
|
|||
Saver = _select_saver(method, mode=mode, fmt=fmt, fp=fp) | |||
saver = Saver(spec, mode=mode, embed_options=embed_options, **kwargs) | |||
saver = Saver(spec, mode=mode, embed_options=embed_options, **kwargs,) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove the stray comma
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
altair_saver/_core.py
Outdated
@@ -242,7 +248,7 @@ def render( | |||
|
|||
for fmt in fmts: | |||
Saver = _select_saver(method, mode=mode, fmt=fmt) | |||
saver = Saver(spec, mode=mode, embed_options=embed_options, **kwargs) | |||
saver = Saver(spec, mode=mode, embed_options=embed_options, **kwargs,) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove comma
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
altair_saver/savers/_node.py
Outdated
@@ -69,6 +72,28 @@ class NodeSaver(Saver): | |||
"vega": ["pdf", "png", "svg"], | |||
"vega-lite": ["pdf", "png", "svg", "vega"], | |||
} | |||
_vega_cli_options: List |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
List[str]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
altair_saver/savers/_node.py
Outdated
embed_options: Optional[JSONDict] = None, | ||
vega_version: str = alt.VEGA_VERSION, | ||
vegalite_version: str = alt.VEGALITE_VERSION, | ||
vegaembed_version: str = alt.VEGAEMBED_VERSION, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove embed_options
and the three versions, I believe, as they are not used by NodeSaver
. kwargs will handle them if present.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
altair_saver/savers/_node.py
Outdated
embed_options=embed_options, | ||
vega_version=vega_version, | ||
vegalite_version=vegalite_version, | ||
vegaembed_version=vegaembed_version, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pass kwargs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@@ -167,6 +167,7 @@ def __init__( | |||
webdriver: Optional[Union[str, WebDriver]] = None, | |||
offline: bool = True, | |||
scale_factor: Optional[float] = 1, | |||
**kwargs, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pass kwargs to super(), below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@pytest.mark.parametrize("mode,fmt", get_modes_and_formats()) | ||
@pytest.mark.parametrize("vega_cli_options", [None, ["--loglevel", "error"]]) | ||
def test_node_mimebundle( | ||
name: str, data: Any, mode: str, fmt: str, vega_cli_options: Optional[List] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional[List[str]]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
altair_saver/tests/test_core.py
Outdated
@pytest.mark.parametrize("fmt", ["html", "svg"]) | ||
@pytest.mark.parametrize("vega_cli_options", [None, ["--loglevel", "debug"]]) | ||
def test_save_w_vega_cli_options( | ||
capsys: Any, chart: alt.TopLevelMixin, fmt: str, vega_cli_options: Optional[List] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
capsys: SysCapture (use from _pytest.capture import SysCapture
)
vega_cli_options: Optional[List[str]]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
altair_saver/tests/test_core.py
Outdated
|
||
@pytest.mark.parametrize("vega_cli_options", [None, ["--loglevel", "debug"]]) | ||
def test_render_w_vega_cli_options( | ||
capsys: Any, chart: alt.TopLevelMixin, vega_cli_options: Optional[List] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
types, same as above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
bundle = render(chart, fmts=["html", "svg"], vega_cli_options=vega_cli_options) | ||
assert len(bundle) == 2 | ||
for mimetype, content in bundle.items(): | ||
fmt = mimetype_to_fmt(mimetype) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert content is not None (for mypy)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Awesome, thanks for the contribution! |
My primary use case is to be able to render interactive charts with multiple renderers (
alt.renderers.enable("altair_saver", fmts=["html", "svg"]
) without getting warnings about thewindow
coming back and getting rendered into a notebook or othernbconvert
export.Closes #51. @jakevdp Would love your review and happy to tweak as needed – this was my quick and dirty approach to pass the arguments through, but if there's a more elegant way of doing so, please let me know!