Spaces:
Sleeping
Sleeping
File size: 889 Bytes
3c5146b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | """PDF export — saves Plotly figures as PDF files using kaleido."""
import asyncio
import os
import plotly.graph_objects as go
async def export_pdf(fig: go.Figure, filename: str) -> str:
"""
Export a Plotly figure to PDF. Save to /tmp/{filename}.pdf.
Uses asyncio.to_thread to avoid blocking.
Returns the file path on success, empty string on failure.
"""
path = f"/tmp/{filename}.pdf"
try:
await asyncio.to_thread(
fig.write_image, path, format="pdf", width=1200, height=600
)
return path
except Exception:
return ""
async def export_multiple_pdf(figures: dict[str, go.Figure]) -> dict[str, str]:
"""Export multiple figures to PDF in parallel."""
results = await asyncio.gather(
*[export_pdf(fig, name) for name, fig in figures.items()]
)
return dict(zip(figures.keys(), results))
|