File size: 774 Bytes
e42f219
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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