ProjectCraft / export /png_export.py
yania-n's picture
Initial deployment of ProjectCraft
3c5146b
Raw History Blame Contribute Delete
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))