Skip to content

Commit

Permalink
allow Paths in save()
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalTS committed Nov 30, 2020
1 parent 54e03d4 commit 45265ff
Showing 1 changed file with 8 additions and 4 deletions.
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

0 comments on commit 45265ff

Please sign in to comment.