Skip to content

Conversation

Copy link

Copilot AI commented Oct 11, 2025

Problem

The current implementation enforces file:// protocol for the template_path parameter in html_to_pic, which prevents the use of remote browsers configured via htmlrender_connect or htmlrender_connect_over_cdp. Remote browsers cannot access local file systems through the file:// protocol, making the plugin incompatible with remote deployment scenarios.

# This check blocked non-file:// URLs
if "file:" not in template_path:
    raise Exception("template_path should be file:///path/to/template")

Solution

This PR removes the hardcoded file:// protocol enforcement and makes the interface flexible to support various URL schemes needed for remote browser scenarios.

Changes Made

1. html_to_pic function

  • Removed the validation that enforced file:// protocol
  • Updated docstring to document support for multiple URL schemes:
    • file:// - Local file paths (existing behavior)
    • http:// / https:// - Remote URLs (for remote browsers)
    • data: - Data URLs for inline content
    • about:blank - Blank page (recommended for pure HTML content)

2. template_to_pic function

  • Changed implementation to use base_url from the pages parameter instead of hardcoding file://{template_path}
  • Maintains backward compatibility by defaulting to file://{template_path} when base_url is not provided
  • Updated docstring to clarify the distinction between:
    • template_path: Local filesystem path for jinja2 template loading
    • base_url: Browser base URL for resolving relative resource paths

3. Documentation

Added comprehensive section in README explaining remote browser usage with examples:

# Using html_to_pic with remote browser
pic = await html_to_pic(
    html="<html><body><h1>Hello</h1></body></html>",
    template_path="about:blank"
)

# Using template_to_pic with remote browser
pic = await template_to_pic(
    template_path="/local/templates",  # For jinja2 loading
    template_name="my_template.html",
    templates={"data": "value"},
    pages={
        "viewport": {"width": 600, "height": 300},
        "base_url": "http://your-server.com/static/",  # For browser
    },
)

4. Tests

  • Added test_html_to_pic_with_different_url_schemes to verify support for about:blank and data: URLs
  • Added test_template_to_pic_with_different_base_url to verify custom base_url parameter usage

Backward Compatibility

Fully backward compatible - All existing code continues to work without modification:

  • Default behavior unchanged (still uses file:// URLs by default)
  • All existing examples and tests remain valid
  • No breaking changes to the API

Benefits

  • Enables remote browser deployments (Docker, cloud services, etc.)
  • More flexible URL handling for different deployment scenarios
  • Better separation of concerns (template loading vs. browser navigation)
  • Maintains existing functionality while adding new capabilities

Fixes the issue raised in the problem statement about adapting data_source methods for remote browser capability.

Original prompt

你觉得为data_sources中的方法适配远程浏览器的能力,应该怎么改接口好,现在有比如async def html_to_pic(
html: str,
wait: int = 0,
template_path: str = f"file://{getcwd()}",
type: Literal["jpeg", "png"] = "png",
quality: Union[int, None] = None,
device_scale_factor: float = 2,
screenshot_timeout: Optional[float] = 30_000,
full_page: Optional[bool] = True,
**kwargs,
) -> bytes:
"""html转图片

Args:
    screenshot_timeout (float, optional): 截图超时时间,默认30000ms
    html (str): html文本
    wait (int, optional): 等待时间. Defaults to 0.
    template_path (str, optional): 模板路径 如 "file:///path/to/template/"
    type (Literal["jpeg", "png"]): 图片类型, 默认 png
    quality (int, optional): 图片质量 0-100 当为`png`时无效
    device_scale_factor: 缩放比例,类型为float,值越大越清晰
    **kwargs: 传入 page 的参数

Returns:
    bytes: 图片, 可直接发送
"""
# logger.debug(f"html:\n{html}")
if "file:" not in template_path:
    raise Exception("template_path should be file:///path/to/template")
async with get_new_page(device_scale_factor, **kwargs) as page:
    page.on("console", lambda msg: logger.debug(f"[Browser Console]: {msg.text}"))
    await page.goto(template_path)
    await page.set_content(html, wait_until="networkidle")
    await page.wait_for_timeout(wait)
    return await page.screenshot(
        full_page=full_page,
        type=type,
        quality=quality,
        timeout=screenshot_timeout,
    )

这种强制写死的本地文件路径的


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 4 commits October 11, 2025 12:07
Co-authored-by: BalconyJH <73932916+BalconyJH@users.noreply.github.com>
…es parameter

Co-authored-by: BalconyJH <73932916+BalconyJH@users.noreply.github.com>
Co-authored-by: BalconyJH <73932916+BalconyJH@users.noreply.github.com>
Co-authored-by: BalconyJH <73932916+BalconyJH@users.noreply.github.com>
Copilot AI changed the title [WIP] Adapt html_to_pic method for remote browser support Support remote browsers by removing file:// protocol enforcement Oct 11, 2025
Copilot AI requested a review from BalconyJH October 11, 2025 12:13
Copilot finished work on behalf of BalconyJH October 11, 2025 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants