Skip to content

export

export

Map export functionality (HTML, PNG, SVG).

export.capture_screenshot

capture_screenshot(
    html_path: str,
    width: int = 1200,
    height: int = 800,
    delay: float = 2.0,
    scale: float = 1.0,
) -> bytes

Capture a screenshot of an HTML file using a headless browser.

Uses Chrome/Chromium if available, otherwise falls back to Microsoft Edge.

Parameters:

  • html_path (str) –

    Path to the HTML file.

  • width (int, default: 1200 ) –

    Viewport width in pixels.

  • height (int, default: 800 ) –

    Viewport height in pixels.

  • delay (float, default: 2.0 ) –

    Seconds to wait for tile loading.

  • scale (float, default: 1.0 ) –

    Device pixel ratio. scale=2.0 renders at 2× pixel density (Retina), producing a width * 2 × height * 2 pixel image while keeping the map layout identical to scale=1.0.

Returns:

  • bytes

    PNG image bytes.

Source code in mapyta/export.py
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
def capture_screenshot(
    html_path: str,
    width: int = 1200,
    height: int = 800,
    delay: float = 2.0,
    scale: float = 1.0,
) -> bytes:
    """Capture a screenshot of an HTML file using a headless browser.

    Uses Chrome/Chromium if available, otherwise falls back to Microsoft Edge.

    Parameters
    ----------
    html_path : str
        Path to the HTML file.
    width : int
        Viewport width in pixels.
    height : int
        Viewport height in pixels.
    delay : float
        Seconds to wait for tile loading.
    scale : float
        Device pixel ratio. ``scale=2.0`` renders at 2× pixel density (Retina),
        producing a ``width * 2`` × ``height * 2`` pixel image while keeping the
        map layout identical to ``scale=1.0``.

    Returns
    -------
    bytes
        PNG image bytes.
    """
    if scale <= 0:
        raise ValueError(f"scale must be greater than 0, got {scale!r}")

    check_selenium()
    backend = _select_backend()
    builder = _build_chrome_driver if backend == "chrome" else _build_edge_driver

    driver = None
    try:
        driver = builder(width, height, scale)
        driver.get(f"file://{html_path}")
        time.sleep(delay)
        return driver.get_screenshot_as_png()
    finally:
        if driver:
            driver.quit()

export.check_selenium

check_selenium() -> None

Verify Selenium and a supported browser are available.

Raises:

  • ImportError

    If selenium is not installed.

  • RuntimeError

    If no Chromium-based browser (Chrome, Chromium, or Edge) is installed.

Source code in mapyta/export.py
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
def check_selenium() -> None:
    """Verify Selenium and a supported browser are available.

    Raises
    ------
    ImportError
        If selenium is not installed.
    RuntimeError
        If no Chromium-based browser (Chrome, Chromium, or Edge) is installed.
    """
    try:
        from selenium import webdriver  # noqa: PLC0415, F401  # ty: ignore[unresolved-import]
    except ImportError:
        raise ImportError(
            "Image export requires selenium>=4.6. Install it with:\n  pip install 'selenium>=4.6'\nor\n  uv add 'selenium>=4.6'"
        ) from None

    _select_backend()