Files
cibuildwheel/bin/update_how_it_works_image.py
T

59 lines
1.8 KiB
Python
Raw Normal View History

2022-09-18 16:12:57 +01:00
#!/usr/bin/env python3
import subprocess
import tempfile
2022-09-18 16:12:57 +01:00
from pathlib import Path
try:
from playwright.sync_api import sync_playwright # type: ignore[import-not-found]
2022-09-18 16:12:57 +01:00
except ImportError:
msg = """
playwright not found. Install it with:
pip install playwright
playwright install chromium
Or, run this script with:
nox -s update_how_it_works_image
2022-09-18 16:12:57 +01:00
"""
raise SystemExit(msg) from None
2022-09-18 16:12:57 +01:00
2024-10-22 10:16:59 -04:00
def main() -> None:
with tempfile.TemporaryDirectory() as tmp_dir_str:
tmp_dir = Path(tmp_dir_str)
subprocess.run(["mkdocs", "build", "--site-dir", tmp_dir], check=True)
2022-09-18 16:12:57 +01:00
html_str = Path("docs/diagram.html").read_text()
css_tags = f"""
<style>{(tmp_dir / "css/theme.css").read_text()}</style>
<style>{(tmp_dir / "css/theme_extra.css").read_text()}</style>
<style>{(tmp_dir / "extra.css").read_text()}</style>
<style>
body {{
background: white;
}}
</style>
"""
html_str = f"<html><head>{css_tags}</head><body>{html_str}</body></html>"
2022-09-18 16:12:57 +01:00
html_path = Path(tmp_dir) / "diagram_screenshot.html"
html_path.write_text(html_str)
2022-09-18 16:12:57 +01:00
dest_path = Path("docs/data/how-it-works.png")
2022-09-18 16:12:57 +01:00
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page(device_scale_factor=2, viewport={"width": 830, "height": 600})
page.goto(html_path.as_uri())
page.wait_for_load_state("networkidle")
2022-09-18 16:12:57 +01:00
height = page.evaluate("document.body.scrollHeight")
page.set_viewport_size({"width": 830, "height": height})
page.screenshot(path=str(dest_path), full_page=True)
browser.close()
2022-09-18 16:12:57 +01:00
if __name__ == "__main__":
main()