概述
使用无头浏览器来截图 Grafana 页面实现渲染仪表盘图像的目的。
安装
环境需要 playwright 或者远程 playwright
pip instll playwright py-grafana-render
playwright install
playwright install-dev
使用
在你的代码中引用:
from py_grafana_render import GrafanaRender
# 此处传入 Grafana 服务账户的 Token
gf = GrafanaRender(token="<your-grafana-service-token>", browser="firefox")
gf_ws = GrafanaRender(token="<your-grafana-service-token>", browser="firefox", remote_browser_ws = "ws://<your-host>:port")
# 本地安装了 playwright 浏览器
image_bytes = gf.snapshot(
url="https://<your-domain>/d/<dashboard-uid>?xxx=xxx&kiosk",
goto_timeout=60000,
wait_for_selector_timeout=60000,
wait_for_timeout=5000,
file_path="./test.png"
)
参数说明
GrafanaRender
| 参数名 | 必填 | 类型 | 默认值 | 说明 |
|---|---|---|---|---|
| token | 是 | str | Grafana 服务账户的 Token | |
| browser | str | firefox | 使用的无头浏览器,可选值: - chrome - firefox |
snapshot
参数:
| 参数名 | 必填 | 类型 | 默认值 | 说明 |
|---|---|---|---|---|
| url | 是 | str | Grafana 的页面,可以包含查询字符串,不限仪表盘或面板。 | |
| width | int | 762 | 截图宽度。 | |
| height | int | 300 | 截图高度。若开启自动高度,则仪表盘将使用自动高度。 | |
| auto_height | bool | True | 自动获取实际高度,如果无法自动获取,仅会使用默认高度。面板不会自动获取高度。 | |
| auto_height_offset | int | 150 | 自动获取的高度,会因为存在顶部筛选器导致误差,使用该值对自动高度进行一定偏移。 | |
| data_load_timeout | int | 5000 | 页面加载完成后等待数据加载的时间,单位毫秒。未传 wait_for_timeout 时使用该值。 | |
| goto_timeout | int | None | page.goto 的超时时间,单位毫秒;不传则使用 Playwright 默认值。 | |
| wait_for_selector_timeout | int | 60000 | page.wait_for_selector 的超时时间,单位毫秒。 | |
| wait_for_timeout | int | None | page.wait_for_timeout 的等待时间,单位毫秒;不传则使用 data_load_timeout。 | |
| hide_class | list | None | 隐藏的样式选择器列表,比如 .css-k3l5qq 是 v11.3.1 的顶部筛选器栏。 | |
| filetype | str | png | 可选 png 或 jpeg | |
| file_path | str | None | 截图文件保存路径,需要包括文件名的完整路径。可以不传入,获取返回的字节流后自行保存。 |
返回:
- 图片字节流:bytes 当不传入 file_path 时,可以自行存储字节流,比如发送到 s3 存储等。
提示: 若希望全屏,需要自行在 url 中加入 kiosk 查询字符串。
Metadata
Release files for py-grafana-render 0.1.12
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| py_grafana_render-0.1.12.tar.gz | 26.8 kB | Without provenance | Details for py_grafana_render-0.1.12.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| py_grafana_render-0.1.12-py3-none-any.whl | Python 3 | none | any | Details for py_grafana_render-0.1.12-py3-none-any.whl |
Total release size: 31.7 kB
Release files / py_grafana_render-0.1.12.tar.gz
| Download URL | py_grafana_render-0.1.12.tar.gz |
|---|---|
| Size | 26.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d20deab414dc1aae1d4321fe941b348e011a5afdcc9f83a5c615d0e68699b2d4
|
|
BLAKE2b-256 checksum How to use checksums |
d075afc407f6a0a33c629a983fd58b81445b29df2a627585936c5112a86b2801
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.5.30
|
Release files / py_grafana_render-0.1.12-py3-none-any.whl
| Download URL | py_grafana_render-0.1.12-py3-none-any.whl |
|---|---|
| Size | 4.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a3bdc99155f7ec197c48f3a5be69901477c16f3257aa61437b38d64386586c39
|
|
BLAKE2b-256 checksum How to use checksums |
9dfc4a05bdc47d4fff5afb3f4507c16790857a0facf46d22a6e207af3fc75ebb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.5.30
|
Security for py-grafana-render 0.1.12
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.