Skip to content

Commit

Permalink
Some code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
RYangazov committed Dec 4, 2023
1 parent d95afc8 commit dce5468
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions python-package/lets_plot/plot/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,12 +797,15 @@ def _to_svg(spec, path) -> str | None:

svg = kbr._generate_svg(spec.as_dict())
if isinstance(path, str):
_makedirs(path)
dirname = os.path.dirname(path)
if dirname and not os.path.exists(dirname):
os.makedirs(dirname)
with io.open(path, mode="w", encoding="utf-8") as f:
f.write(svg)
return os.path.abspath(path)
else:
path.write(svg.encode())
return _abspath(path)
return None


def _to_html(spec, path, iframe: bool) -> str | None:
Expand All @@ -813,12 +816,15 @@ def _to_html(spec, path, iframe: bool) -> str | None:
html_page = kbr._generate_static_html_page(spec.as_dict(), iframe)

if isinstance(path, str):
_makedirs(path)
dirname = os.path.dirname(path)
if dirname and not os.path.exists(dirname):
os.makedirs(dirname)
with io.open(path, mode="w", encoding="utf-8") as f:
f.write(html_page)
return os.path.abspath(path)
else:
path.write(html_page.encode())
return _abspath(path)
return None


def _to_png(spec, path, scale: float) -> str | None:
Expand All @@ -838,9 +844,15 @@ def _to_png(spec, path, scale: float) -> str | None:
from .. import _kbridge
# Use SVG image-rendering style as Cairo doesn't support CSS image-rendering style,
svg = _kbridge._generate_svg(spec.as_dict(), use_css_pixelated_image_rendering=False)
_makedirs(path)
cairosvg.svg2png(bytestring=svg, write_to=path, scale=scale)
return _abspath(path)
if isinstance(path, str):
dirname = os.path.dirname(path)
if dirname and not os.path.exists(dirname):
os.makedirs(dirname)
cairosvg.svg2png(bytestring=svg, write_to=path, scale=scale)
return os.path.abspath(path)
else:
cairosvg.svg2png(bytestring=svg, write_to=path, scale=scale)
return None


def _to_pdf(spec, path, scale: float) -> str | None:
Expand All @@ -860,20 +872,12 @@ def _to_pdf(spec, path, scale: float) -> str | None:
from .. import _kbridge
# Use SVG image-rendering style as Cairo doesn't support CSS image-rendering style,
svg = _kbridge._generate_svg(spec.as_dict(), use_css_pixelated_image_rendering=False)
_makedirs(path)
cairosvg.svg2pdf(bytestring=svg, write_to=path, scale=scale)
return _abspath(path)


def _makedirs(path):
if isinstance(path, str):
dirname = os.path.dirname(path)
if dirname and not os.path.exists(dirname):
os.makedirs(dirname)


def _abspath(path) -> str | None:
if isinstance(path, str):
cairosvg.svg2pdf(bytestring=svg, write_to=path, scale=scale)
return os.path.abspath(path)
else:
cairosvg.svg2pdf(bytestring=svg, write_to=path, scale=scale)
return None

0 comments on commit dce5468

Please sign in to comment.