Skip to content

Commit a3d893f

Browse files
committed
ENH: Read quarto fig-width, fig-height, fig-format
1 parent c3678ec commit a3d893f

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

doc/changelog.qmd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ title: Changelog
1010
- The `family`, `fontstyle` and `fontweight` parameters of
1111
[](:class:`~plotnine.geom_text`) are now aesthetics ({{< issue 790 >}}).
1212

13+
- plotnine now responds to the `fig-width`, `fig-height` and `fig-format`
14+
settings in the meta section of a quarto document.
15+
1316
### New Features
1417

1518
- [](:class:`~plotnine.geom_text`) has gained new aesthetics

plotnine/options.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from __future__ import annotations
22

3-
import typing
3+
import os
4+
from typing import TYPE_CHECKING
45

5-
if typing.TYPE_CHECKING:
6+
if TYPE_CHECKING:
67
from typing import Any, Literal, Optional, Type
78

89
from plotnine import theme
@@ -105,3 +106,35 @@ def set_option(name: str, value: Any) -> Any:
105106
old = d[name]
106107
d[name] = value
107108
return old
109+
110+
111+
# Quarto sets environment variables for the figure dpi, size and format
112+
# for the project or document.
113+
#
114+
# https://quarto.org/docs/computations/execution-options.html#figure-options
115+
#
116+
# If we are in quarto, we read those and make them the default values for
117+
# the options.
118+
# Note that, reading the variables and setting them in a context manager
119+
# cannot not work since the option values would be set after the original
120+
# defaults have been used by the theme.
121+
if "QUARTO_FIG_WIDTH" in os.environ:
122+
123+
def _set_options_from_quarto():
124+
"""
125+
Set options from quarto
126+
"""
127+
global dpi, figure_size, figure_format
128+
129+
dpi = int(os.environ["QUARTO_FIG_DPI"])
130+
figure_size = (
131+
float(os.environ["QUARTO_FIG_WIDTH"]),
132+
float(os.environ["QUARTO_FIG_HEIGHT"]),
133+
)
134+
135+
# quarto verifies the format
136+
# If is retina, it doubles the original dpi and changes the
137+
# format to png
138+
figure_format = os.environ["QUARTO_FIG_FORMAT"] # pyright: ignore
139+
140+
_set_options_from_quarto()

0 commit comments

Comments
 (0)