|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 |
| -import typing |
| 3 | +import os |
| 4 | +from typing import TYPE_CHECKING |
4 | 5 |
|
5 |
| -if typing.TYPE_CHECKING: |
| 6 | +if TYPE_CHECKING: |
6 | 7 | from typing import Any, Literal, Optional, Type
|
7 | 8 |
|
8 | 9 | from plotnine import theme
|
@@ -105,3 +106,35 @@ def set_option(name: str, value: Any) -> Any:
|
105 | 106 | old = d[name]
|
106 | 107 | d[name] = value
|
107 | 108 | 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