Spaces:
Sleeping
Sleeping
Download export/png_export.py from yania-n/ProjectCraft: direct link, hf CLI and curl.
- Browser
- Download file 930 Bytes
-
https://huggingface.co/spaces/yania-n/ProjectCraft/resolve/main/export/png_export.py
- Command line
-
hf download hf://spaces/yania-n/ProjectCraft/export/png_export.py
-
curl -L -o png_export.py https://huggingface.co/spaces/yania-n/ProjectCraft/resolve/main/export/png_export.py
930 Bytes
| """PNG export — saves Plotly figures as PNG files using kaleido.""" | |
| import asyncio | |
| import os | |
| import plotly.graph_objects as go | |
| async def export_png(fig: go.Figure, filename: str) -> str: | |
| """ | |
| Export a Plotly figure to PNG. Save to /tmp/{filename}.png. | |
| Uses asyncio.to_thread to avoid blocking the event loop. | |
| Returns the file path on success, empty string on failure. | |
| """ | |
| path = f"/tmp/{filename}.png" | |
| try: | |
| await asyncio.to_thread( | |
| fig.write_image, path, format="png", width=1200, height=600 | |
| ) | |
| return path | |
| except Exception: | |
| return "" | |
| async def export_multiple_png(figures: dict[str, go.Figure]) -> dict[str, str]: | |
| """Export multiple figures to PNG in parallel. Returns {name: filepath}.""" | |
| results = await asyncio.gather( | |
| *[export_png(fig, name) for name, fig in figures.items()] | |
| ) | |
| return dict(zip(figures.keys(), results)) | |