Skip to main content

Browser API service for Entari using Playwright

Project description

entari-plugin-browser

Browser API service for Entari using Playwright

使用

导入

from entari_plugin_browser import playwright_api, text2img


async def main():
    async with playwright_api.page() as page:
        await page.goto('https://www.baidu.com')
        await page.screenshot(path='baidu.png')
    
    img: bytes = await text2img('Hello, World!')

配置

在你的 Entari 配置文件中如下配置:(以 entari.yml 为例)

plugins:
  browser:
    browser_type: chromium
    channel: chrome
    headless: true
    # ...
    # 更多配置请参考 Playwright 文档,详见 <https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch>

HTMLRenderer

除了内置提供的 text2imgmd2img 方法外,你还可以使用 HTMLRenderer 等来自定义渲染器。

from entari_plugin_browser import HTMLRenderer, convert_md, PageOption, ScreenshotOption

md = """\
<div align="center">

# entari-plugin-browser

*Browser API service for Entari using Playwright*

</div>

## 使用

### 导入

from entari_plugin_browser import playwright_api, text2img


async def main():
    async with playwright_api.page() as page:
        await page.goto('https://www.baidu.com')
        await page.screenshot(path='baidu.png')
    
    img: bytes = await text2img('Hello, World!')
"""

async def function():
    image_bytes: bytes = await HTMLRenderer().render(
        convert_md(md),
        extra_page_option=PageOption(viewport={"width": 840, "height": 10}, device_scale_factor=1.5),
        extra_screenshot_option=ScreenshotOption(type="jpeg", quality=80, scale="device"),
    )

模板转图片(可选依赖 jinja2)

如需将 Jinja2 模板渲染为 HTML 并输出图片,安装可选依赖:

pip install "entari-plugin-browser[jinja2]"

示例:

from entari_plugin_browser import template2img


async def main():
    text_list = ["1", "2", "3", "4"]
    template_path = "path/to/templates"
    template_name = "example.html"
    
    img: bytes = await template2img(
        template_path=template_path,
        template_name=template_name,
        templates={"text_list": text_list},
    )

说明:默认启用 HTML 自动转义,如需插入已转义 HTML,请在模板中使用 |safe 过滤器。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

entari_plugin_browser-0.5.1.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

entari_plugin_browser-0.5.1-py3-none-any.whl (19.4 kB view details)

Uploaded Python 3

File details

Details for the file entari_plugin_browser-0.5.1.tar.gz.

File metadata

  • Download URL: entari_plugin_browser-0.5.1.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.25.6 CPython/3.11.7 Windows/10

File hashes

Hashes for entari_plugin_browser-0.5.1.tar.gz
Algorithm Hash digest
SHA256 cbc92acc90bec1fefcf269aa5e6a64c8e079d5d64b8b9f4a79ade149ed80fad5
MD5 5a3eb44c6b413cde0b926b7893478cbc
BLAKE2b-256 8d0dbe0e5a20e6c57939bd8be8c4debb0bc477c658356aad19f419b2ce387a28

See more details on using hashes here.

File details

Details for the file entari_plugin_browser-0.5.1-py3-none-any.whl.

File metadata

File hashes

Hashes for entari_plugin_browser-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f867e3a00af0fdb6bd5572377967a6ff37ed0d1da6289741c84cd377738386a8
MD5 6b672ad317809555bd4dab3d9181655c
BLAKE2b-256 615da185ba0e40e46f79e9af544b2e4df273034171da23b24d8bb7ecf84f8e71

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page