示例#1
0
def test_available_formats(monkeypatch: MonkeyPatch, mode: str) -> None:
    monkeypatch.setattr(NodeSaver, "enabled", lambda: False)
    monkeypatch.setattr(SeleniumSaver, "enabled", lambda: False)
    expected = {mode, "json", "html"}
    assert available_formats(mode) == expected

    monkeypatch.setattr(SeleniumSaver, "enabled", lambda: True)
    expected |= {"vega", "png", "svg"}
    assert available_formats(mode) == expected

    monkeypatch.setattr(NodeSaver, "enabled", lambda: True)
    expected |= {"pdf"}
    assert available_formats(mode) == expected
示例#2
0
def require_altair_saver_png():
    try:
        import altair_saver  # noqa: F401
    except ImportError:
        pytest.skip("altair_saver not importable; cannot run saver tests")
    if "png" not in altair_saver.available_formats('vega-lite'):
        pytest.skip("altair_saver not configured to save to png")
示例#3
0
def test_save(format, basic_chart):
    if format in ["pdf", "png"]:
        out = io.BytesIO()
        mode = "rb"
    else:
        out = io.StringIO()
        mode = "r"

    if format in ["svg", "png", "pdf"]:
        if not altair_saver:
            with pytest.raises(ValueError) as err:
                basic_chart.save(out, format=format)
            assert "github.com/altair-viz/altair_saver" in str(err.value)
            return
        elif format not in altair_saver.available_formats():
            with pytest.raises(ValueError) as err:
                basic_chart.save(out, format=format)
            assert f"No enabled saver found that supports format='{format}'" in str(
                err.value
            )
            return

    basic_chart.save(out, format=format)
    out.seek(0)
    content = out.read()

    if format == "json":
        assert "$schema" in json.loads(content)
    if format == "html":
        assert content.startswith("<!DOCTYPE html>")

    fid, filename = tempfile.mkstemp(suffix="." + format)
    os.close(fid)

    # test both string filenames and pathlib.Paths
    for fp in [filename, pathlib.Path(filename)]:
        try:
            basic_chart.save(fp)
            with open(fp, mode) as f:
                assert f.read()[:1000] == content[:1000]
        finally:
            os.remove(fp)
示例#4
0
def _plot_history_altair(history, filename=None):

    # Saving as png requires that chromedriver + selenium are available
    ext, mode = ('png', 'wb') if 'png' in available_formats() else ('html',
                                                                    'w')

    if filename is None:
        filename = date(FILE_DATE) + "." + ext

    data = history.history
    data["epoch"] = history.epoch
    data = pd.DataFrame(data).melt("epoch")
    chart = (alt.Chart(data).mark_line().encode(
        x=alt.X('epoch', title='Epoch'),
        y=alt.Y('value', title='Value'),
        color="variable").properties(title=title(history)).configure_legend(
            title=None))

    with open(filename, mode) as f:
        save(chart, f)
示例#5
0
def test_available_formats_error() -> None:
    message = "Invalid mode: 'bad-mode'. Must be one of ('vega', 'vega-lite')"
    with pytest.raises(ValueError) as err:
        available_formats("bad-mode")
    assert message in str(err.value)