Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export to file-like object #938

Merged
merged 20 commits into from
Dec 18, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Modifying _makedirs function
  • Loading branch information
RYangazov committed Dec 5, 2023
commit a93455ec0f67d208953a87c83529381a22bf5a00
43 changes: 23 additions & 20 deletions python-package/lets_plot/plot/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,12 +797,10 @@ def _to_svg(spec, path) -> str | None:

svg = kbr._generate_svg(spec.as_dict())
if isinstance(path, str):
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:
abspath = _makedirs(path)
with io.open(abspath, mode="w", encoding="utf-8") as f:
f.write(svg)
return os.path.abspath(path)
return abspath
else:
path.write(svg.encode())
return None
Expand All @@ -816,12 +814,10 @@ def _to_html(spec, path, iframe: bool) -> str | None:
html_page = kbr._generate_static_html_page(spec.as_dict(), iframe)

if isinstance(path, str):
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:
abspath = _makedirs(path)
with io.open(abspath, mode="w", encoding="utf-8") as f:
f.write(html_page)
return os.path.abspath(path)
return abspath
else:
path.write(html_page.encode())
return None
Expand All @@ -844,12 +840,11 @@ 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)

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)
abspath = _makedirs(path)
cairosvg.svg2png(bytestring=svg, write_to=abspath, scale=scale)
return abspath
else:
cairosvg.svg2png(bytestring=svg, write_to=path, scale=scale)
return None
Expand All @@ -872,12 +867,20 @@ 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)

if isinstance(path, str):
dirname = os.path.dirname(path)
if dirname and not os.path.exists(dirname):
os.makedirs(dirname)
cairosvg.svg2pdf(bytestring=svg, write_to=path, scale=scale)
return os.path.abspath(path)
abspath = _makedirs(path)
cairosvg.svg2pdf(bytestring=svg, write_to=abspath, scale=scale)
return abspath
else:
cairosvg.svg2pdf(bytestring=svg, write_to=path, scale=scale)
return None


def _makedirs(path: str) -> str:
"""Return absolute path to a file after creating all directories in the path."""
abspath = os.path.abspath(path)
dirname = os.path.dirname(abspath)
if dirname and not os.path.exists(dirname):
os.makedirs(dirname)
return abspath