-
Notifications
You must be signed in to change notification settings - Fork 47
/
ggsave_.py
95 lines (76 loc) · 2.95 KB
/
ggsave_.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Copyright (c) 2020. JetBrains s.r.o.
# Use of this source code is governed by the MIT license that can be found in the LICENSE file.
import os
from os.path import join, abspath
from typing import Union
from ..plot.core import _to_svg, _to_html, _to_png, _to_pdf
from ..plot.core import PlotSpec
from ..plot.plot import GGBunch
from ..plot.subplots import SupPlotsSpec
__all__ = ['ggsave']
_DEF_EXPORT_DIR = "lets-plot-images"
def ggsave(plot: Union[PlotSpec, SupPlotsSpec, GGBunch], filename: str, *, path: str = None, iframe: bool = True,
scale: float = 2.0) -> str:
"""
Export plot or `bunch` to a file.
Supported formats: PNG, SVG, PDF, HTML.
The exported file is created in directory ${user.dir}/lets-plot-images
if not specified otherwise (see the `path` parameter).
Parameters
----------
plot : `PlotSpec` or `GGBunch`
Plot specification to export.
filename : str
The name of file. It must end with a file extension corresponding
to one of the supported formats: SVG, HTML (or HTM), PNG (requires CairoSVG library), PDF.
path : str
Path to a directory to save image files in.
By default it is ${user.dir}/lets-plot-images.
iframe : bool, default=True
Whether to wrap HTML page into a iFrame.
Only applicable when exporting to HTML.
Some browsers may not display some UTF-8 characters correctly when setting iframe=True
scale : float, default=2.0
Scaling factor for raster output.
Only applicable when exporting to PNG.
Returns
-------
str
Absolute pathname of created file.
Examples
--------
.. code-block::
:linenos:
:emphasize-lines: 4
from lets_plot import *
LetsPlot.setup_html()
plot = ggplot() + geom_point(x=0, y=0)
ggsave(plot, 'plot.html', path='.', iframe=False)
"""
if not (isinstance(plot, PlotSpec) or isinstance(plot, SupPlotsSpec) or isinstance(plot, GGBunch)):
raise ValueError("PlotSpec, SupPlotsSpec or GGBunch expected but was: {}".format(type(plot)))
filename = filename.strip()
name, ext = os.path.splitext(filename)
if not name:
raise ValueError("Malformed filename: '{}'.".format(filename))
if not ext:
raise ValueError("Missing file extension: '{}'.".format(filename))
if not path:
path = join(os.getcwd(), _DEF_EXPORT_DIR)
if not os.path.exists(path):
os.makedirs(path)
pathname = join(path, filename)
ext = ext[1:].lower()
if ext == 'svg':
_to_svg(plot, pathname)
elif ext in ['html', 'htm']:
_to_html(plot, pathname, iframe=iframe)
elif ext == 'png':
_to_png(plot, pathname, scale)
elif ext == 'pdf':
_to_pdf(plot, pathname, scale)
else:
raise ValueError(
"Unsupported file extension: '{}'\nPlease use one of: 'png', 'svg', 'pdf', 'html', 'htm'".format(ext)
)
return abspath(pathname)