Skip to content

Commit 58096d8

Browse files
authored
File formats enum (#27)
* 🎨 Format(vuecore/constants.py): create enum for output format types * 🎨 Format(vuecore/engines/plotly/saver.py): update script with the output file format enum
1 parent 2fcfae2 commit 58096d8

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

src/vuecore/constants.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,38 @@
33
try:
44
from enum import StrEnum
55
except ImportError:
6+
# Fallback for Python versions < 3.11 that don't have StrEnum built-in
67
from strenum import StrEnum
78

89

910
class PlotType(StrEnum):
11+
"""Enum representing supported plot types."""
12+
1013
SCATTER = auto()
1114
LINE = auto()
1215
BAR = auto()
1316

1417

1518
class EngineType(StrEnum):
19+
"""Enum representing supported plotting engines."""
20+
1621
PLOTLY = auto()
1722
# Add other engines as needed
23+
24+
25+
class OutputFileFormat(StrEnum):
26+
"""Enum representing supported output file formats."""
27+
28+
PNG = auto()
29+
JPG = auto()
30+
JPEG = auto()
31+
SVG = auto()
32+
PDF = auto()
33+
HTML = auto()
34+
JSON = auto()
35+
WEBP = auto()
36+
37+
@property
38+
def value_with_dot(self):
39+
"""Return the file extension with the dot (e.g., '.png')."""
40+
return f".{self.value}"

src/vuecore/engines/plotly/saver.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import kaleido
33
from pathlib import Path
44

5+
from vuecore.constants import OutputFileFormat
6+
57

68
def save(fig: go.Figure, filepath: str) -> None:
79
"""
@@ -33,34 +35,53 @@ def save(fig: go.Figure, filepath: str) -> None:
3335
suffix = path.suffix.lower()
3436

3537
try:
36-
if suffix in [".png", ".jpg", ".jpeg", ".webp", ".svg", ".pdf"]:
38+
# Define static suffixes from the OutputFileFormat enum
39+
image_suffixes = [
40+
OutputFileFormat.PNG.value_with_dot,
41+
OutputFileFormat.JPG.value_with_dot,
42+
OutputFileFormat.JPEG.value_with_dot,
43+
OutputFileFormat.WEBP.value_with_dot,
44+
OutputFileFormat.SVG.value_with_dot,
45+
OutputFileFormat.PDF.value_with_dot,
46+
]
47+
48+
if suffix in image_suffixes:
3749
try:
3850
fig.write_image(filepath)
3951
except RuntimeError as e:
52+
# Handle specific Kaleido errors for Chrome installation
4053
if "Kaleido requires Google Chrome" in str(e):
4154
print(
4255
"[VueCore] Chrome not found. Attempting automatic install using `kaleido.get_chrome_sync()`..."
4356
)
4457
try:
4558
kaleido.get_chrome_sync()
46-
fig.write_image(filepath) # Retry after installing Chrome
59+
# Retry after installing Chrome
60+
fig.write_image(filepath)
4761
except Exception as install_error:
4862
raise RuntimeError(
4963
"[VueCore] Failed to install Chrome automatically. "
5064
"Please install it manually or run `plotly_get_chrome`."
5165
) from install_error
5266
else:
53-
raise
54-
elif suffix == ".html":
67+
raise # Re-raise other RuntimeError exceptions
68+
elif suffix == OutputFileFormat.HTML.value_with_dot:
5569
fig.write_html(filepath, include_plotlyjs="cdn")
56-
elif suffix == ".json":
57-
fig.write_json(filepath, pretty=True)
70+
elif suffix == OutputFileFormat.JSON.value_with_dot:
71+
fig.write_json(
72+
filepath, pretty=True
73+
) # Added pretty=True for readable JSON output
5874
else:
75+
# Generate a dynamic list of supported formats for the error message
76+
supported_suffixes = ", ".join(
77+
[f"'{f.value_with_dot}'" for f in OutputFileFormat]
78+
)
5979
raise ValueError(
6080
f"Unsupported file format: '{suffix}'. "
61-
"Supported formats: .png, .jpg, .jpeg, .webp, .svg, .pdf, .html, .json"
81+
f"Supported formats: {supported_suffixes}"
6282
)
6383
except Exception as e:
84+
# Catch any exceptions during the saving process and re-raise as a RuntimeError
6485
raise RuntimeError(f"[VueCore] Failed to save plot: {filepath}") from e
6586

6687
print(f"[VueCore] Plot saved to {filepath}")

0 commit comments

Comments
 (0)