Skip to content

Commit

Permalink
ENH: Read quarto fig-width, fig-height, fig-format
Browse files Browse the repository at this point in the history
  • Loading branch information
has2k1 committed Jun 14, 2024
1 parent c3678ec commit a3d893f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
3 changes: 3 additions & 0 deletions doc/changelog.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ title: Changelog
- The `family`, `fontstyle` and `fontweight` parameters of
[](:class:`~plotnine.geom_text`) are now aesthetics ({{< issue 790 >}}).

- plotnine now responds to the `fig-width`, `fig-height` and `fig-format`
settings in the meta section of a quarto document.

### New Features

- [](:class:`~plotnine.geom_text`) has gained new aesthetics
Expand Down
37 changes: 35 additions & 2 deletions plotnine/options.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import annotations

import typing
import os
from typing import TYPE_CHECKING

if typing.TYPE_CHECKING:
if TYPE_CHECKING:
from typing import Any, Literal, Optional, Type

from plotnine import theme
Expand Down Expand Up @@ -105,3 +106,35 @@ def set_option(name: str, value: Any) -> Any:
old = d[name]
d[name] = value
return old


# Quarto sets environment variables for the figure dpi, size and format
# for the project or document.
#
# https://quarto.org/docs/computations/execution-options.html#figure-options
#
# If we are in quarto, we read those and make them the default values for
# the options.
# Note that, reading the variables and setting them in a context manager
# cannot not work since the option values would be set after the original
# defaults have been used by the theme.
if "QUARTO_FIG_WIDTH" in os.environ:

def _set_options_from_quarto():
"""
Set options from quarto
"""
global dpi, figure_size, figure_format

dpi = int(os.environ["QUARTO_FIG_DPI"])
figure_size = (
float(os.environ["QUARTO_FIG_WIDTH"]),
float(os.environ["QUARTO_FIG_HEIGHT"]),
)

# quarto verifies the format
# If is retina, it doubles the original dpi and changes the
# format to png
figure_format = os.environ["QUARTO_FIG_FORMAT"] # pyright: ignore

_set_options_from_quarto()

0 comments on commit a3d893f

Please sign in to comment.