import tempfile import pandas as pd import gradio as gr import json import plotly.io as pio class DownloadHelper: @staticmethod def df2csv(df: pd.DataFrame, prefix: str): with tempfile.NamedTemporaryFile(mode="w", delete=False, prefix=f"{prefix}_", suffix=".csv", encoding="utf-8") as tmpfile: df.to_csv(tmpfile.name, index=False) return tmpfile.name @staticmethod def fig2png(fig: gr.components.plot.PlotData, prefix: str): with tempfile.NamedTemporaryFile(mode="wb", delete=False, prefix=f"{prefix}_", suffix=".png") as tmpfile: pio.write_image(json.loads(fig.plot), tmpfile.name, format="png") return tmpfile.name