-
Notifications
You must be signed in to change notification settings - Fork 47
/
simple.py
168 lines (126 loc) · 5.21 KB
/
simple.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# 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 io
from os.path import abspath
from typing import Union
from ..plot.core import PlotSpec
from ..plot.plot import GGBunch
from ..plot.subplots import SupPlotsSpec
def export_svg(plot: Union[PlotSpec, SupPlotsSpec, GGBunch], filename: str) -> str:
"""
Export plot or `bunch` to a file in SVG format.
Parameters
----------
plot : `PlotSpec` or `SupPlotsSpec` or `GGBunch`
Plot specification to export.
filename : str
Filename to save SVG under.
Returns
-------
str
Absolute pathname of created SVG file.
"""
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)))
from .. import _kbridge as kbr
svg = kbr._generate_svg(plot.as_dict())
with io.open(filename, mode="w", encoding="utf-8") as f:
f.write(svg)
return abspath(filename)
def export_html(plot: Union[PlotSpec, SupPlotsSpec, GGBunch], filename: str, iframe: bool = False) -> str:
"""
Export plot or `bunch` to a file in HTML format.
Parameters
----------
plot : `PlotSpec` or `SupPlotsSpec` or `GGBunch`
Plot specification to export.
filename : str
Filename to save HTML page under.
iframe : bool, default=False
Whether to wrap HTML page into a iFrame.
Returns
-------
str
Absolute pathname of created HTML file.
"""
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)))
from .. import _kbridge as kbr
html_page = kbr._generate_static_html_page(plot.as_dict(), iframe)
with io.open(filename, mode="w", encoding="utf-8") as f:
f.write(html_page)
return abspath(filename)
def export_png(plot: Union[PlotSpec, SupPlotsSpec, GGBunch], filename: str, scale: float = 2.0) -> str:
"""
Export plot or `bunch` to a file in PNG format.
Parameters
----------
plot : `PlotSpec` or `SupPlotsSpec` or `GGBunch`
Plot specification to export.
filename : str
Filename to save PNG under.
scale : float, default=2.0
Scaling factor for raster output.
Returns
-------
str
Absolute pathname of created PNG file.
Notes
-----
Export to PNG file uses the CairoSVG library.
CairoSVG is free and distributed under the LGPL-3.0 license.
For more details visit: https://cairosvg.org/documentation/
"""
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)))
try:
import cairosvg
except ImportError:
import sys
print("\n"
"To export Lets-Plot figure to a PNG file please install CairoSVG library to your Python environment.\n"
"CairoSVG is free and distributed under the LGPL-3.0 license.\n"
"For more details visit: https://cairosvg.org/documentation/\n", file=sys.stderr)
return None
from .. import _kbridge
# Use SVG image-rendering style as Cairo doesn't support CSS image-rendering style,
svg = _kbridge._generate_svg(plot.as_dict(), use_css_pixelated_image_rendering=False)
cairosvg.svg2png(bytestring=svg, write_to=filename, scale=scale)
return abspath(filename)
def export_pdf(plot: Union[PlotSpec, SupPlotsSpec, GGBunch], filename: str, scale: float = 2.0) -> str:
"""
Export plot or `bunch` to a file in PDF format.
Parameters
----------
plot : `PlotSpec` or `SupPlotsSpec` or `GGBunch`
Plot specification to export.
filename : str
Filename to save PDF under.
scale : float, default=2.0
Scaling factor for raster output.
Returns
-------
str
Absolute pathname of created PDF file.
Notes
-----
Export to PDF file uses the CairoSVG library.
CairoSVG is free and distributed under the LGPL-3.0 license.
For more details visit: https://cairosvg.org/documentation/
"""
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)))
try:
import cairosvg
except ImportError:
import sys
print("\n"
"To export Lets-Plot figure to a PDF file please install CairoSVG library to your Python environment.\n"
"CairoSVG is free and distributed under the LGPL-3.0 license.\n"
"For more details visit: https://cairosvg.org/documentation/\n", file=sys.stderr)
return None
from .. import _kbridge
# Use SVG image-rendering style as Cairo doesn't support CSS image-rendering style,
svg = _kbridge._generate_svg(plot.as_dict(), use_css_pixelated_image_rendering=False)
cairosvg.svg2pdf(bytestring=svg, write_to=filename, scale=scale)
return abspath(filename)