Spaces:
Sleeping
Sleeping
Download export/pdf_export.py from yania-n/ProjectCraft: direct link, hf CLI and curl.
- Browser
- Download file 889 Bytes
-
https://huggingface.co/spaces/yania-n/ProjectCraft/resolve/main/export/pdf_export.py
- Command line
-
hf download hf://spaces/yania-n/ProjectCraft/export/pdf_export.py
-
curl -L -o pdf_export.py https://huggingface.co/spaces/yania-n/ProjectCraft/resolve/main/export/pdf_export.py
889 Bytes
| """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)) | |