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 headless Chrome.

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
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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 headless Chrome.

    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()

    from selenium import webdriver  # noqa: PLC0415  # ty: ignore[unresolved-import]
    from selenium.webdriver.chrome.options import Options  # noqa: PLC0415  # ty: ignore[unresolved-import]

    options = Options()
    options.add_argument("--headless")
    options.add_argument("--no-sandbox")
    options.add_argument("--disable-dev-shm-usage")
    options.add_argument("--disable-gpu")
    options.add_argument(f"--window-size={width},{height}")
    if scale != 1.0:
        options.add_argument(f"--force-device-scale-factor={scale}")

    driver = None
    try:
        driver = webdriver.Chrome(options=options)
        driver.set_window_size(width, height)
        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 Chrome driver availability.

Raises:

  • ImportError

    If selenium is not installed.

  • RuntimeError

    If Chrome/chromedriver is not found.

Source code in mapyta/export.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def check_selenium() -> None:
    """Verify Selenium and Chrome driver availability.

    Raises
    ------
    ImportError
        If selenium is not installed.
    RuntimeError
        If Chrome/chromedriver is not found.
    """
    try:
        from selenium import webdriver  # noqa: PLC0415, F401  # ty: ignore[unresolved-import]
    except ImportError:
        raise ImportError(
            "Image export requires selenium. Install it with:\n  "
            "pip install selenium chromedriver-autoinstaller\n"
            "or\n"
            "uv add selenium chromedriver-autoinstaller"
        ) from None

    chrome_paths = [
        shutil.which("google-chrome"),
        shutil.which("google-chrome-stable"),
        shutil.which("chromium"),
        shutil.which("chromium-browser"),
        shutil.which("chrome"),
        shutil.which("googlechrome"),
        shutil.which("chromium.exe"),
        shutil.which("chrome_proxy.exe"),
        shutil.which("chromedriver"),
    ]
    if not any(chrome_paths):
        try:
            import chromedriver_autoinstaller  # noqa: PLC0415  # ty: ignore[unresolved-import]

            chromedriver_autoinstaller.install()
        except (ImportError, ModuleNotFoundError, ValueError):
            pass  # Will be caught by the chromedriver check below

    if not shutil.which("chromedriver"):
        raise RuntimeError(
            "Chrome or Chromium not found. Image export requires Chrome.\n"
            "  Ubuntu/Debian: sudo apt install chromium-browser\n"
            "  macOS:         brew install --cask google-chrome\n"
            "  Windows:       Download from https://www.google.com/chrome/\n"
            "chromedriver not found on PATH.\n"
            "  pip install chromedriver-autoinstaller\n"
            "  Or download: https://googlechromelabs.github.io/chrome-for-testing/"
        )