Skip to content

Commit

Permalink
save_fig() util add style: str = "" keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
janosh committed Oct 9, 2023
1 parent 67cd129 commit e7136e1
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pymatviz/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ def save_fig(
plotly_config: dict[str, Any] | None = None,
env_disable: Sequence[str] = ("CI",),
pdf_sleep: float = 0.6,
style: str = "",
**kwargs: Any,
) -> None:
"""Write a plotly or matplotlib figure to disk (as HTML/PDF/SVG/...).
Expand All @@ -321,6 +322,8 @@ def save_fig(
plotly figure to PDF file. Workaround for this plotly issue
https://github.com/plotly/plotly.py/issues/3469. Defaults to 0.6. Has no
effect on matplotlib figures.
style (str, optional): CSS style string to be inserted into the HTML file.
Defaults to "". Only used if path ends with .svelte or .html.
**kwargs: Keyword arguments passed to fig.write_html().
"""
Expand Down Expand Up @@ -364,6 +367,10 @@ def save_fig(
with open(path, "w") as file:
# add trailing newline for pre-commit end-of-file commit hook
file.write(text + "\n")
if style:
with open(path, "r+") as file:
# replace first '<div ' with '<div {style=} '
file.write(file.read().replace("<div ", f"<div {style=} ", 1))
else:
if is_pdf:
orig_template = fig.layout.template
Expand Down

0 comments on commit e7136e1

Please sign in to comment.