Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions altair/utils/save.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import json
import pathlib

from .mimebundle import spec_to_mimebundle


def write_file_or_filename(fp, content, mode="w"):
"""Write content to fp, whether fp is a string or a file-like object"""
if isinstance(fp, str):
"""Write content to fp, whether fp is a string, a pathlib Path or a
file-like object"""
if isinstance(fp, str) or isinstance(fp, pathlib.PurePath):
with open(fp, mode) as f:
f.write(content)
else:
Expand Down Expand Up @@ -34,8 +36,8 @@ def save(
----------
chart : alt.Chart
the chart instance to save
fp : string filename or file-like object
file in which to write the chart.
fp : string filename, pathlib.Path or file-like object
file to which to write the chart.
format : string (optional)
the format to write: one of ['json', 'html', 'png', 'svg'].
If not specified, the format will be determined from the filename.
Expand Down Expand Up @@ -71,6 +73,8 @@ def save(
if format is None:
if isinstance(fp, str):
format = fp.split(".")[-1]
elif isinstance(fp, pathlib.PurePath):
format = fp.suffix.lstrip(".")
else:
raise ValueError(
"must specify file format: " "['png', 'svg', 'pdf', 'html', 'json']"
Expand Down
15 changes: 9 additions & 6 deletions altair/vegalite/v4/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import operator
import os
import pathlib
import tempfile

import jsonschema
Expand Down Expand Up @@ -284,12 +285,14 @@ def test_save(format, basic_chart):
fid, filename = tempfile.mkstemp(suffix="." + format)
os.close(fid)

try:
basic_chart.save(filename)
with open(filename, mode) as f:
assert f.read()[:1000] == content[:1000]
finally:
os.remove(filename)
# 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)


def test_facet_basic():
Expand Down