File size: 958 Bytes
939b332
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
31
32
33
34
35
36
37
38
import io
import time

import pandas as pd
import streamlit as st


@st.cache_data
def convert_dfs(dfs: list[pd.DataFrame], sheet_names: list[str]) -> bytes:
    # IMPORTANT: Cache the conversion to prevent computation on every rerun

    # Create a BytesIO object
    bytes_io = io.BytesIO()

    # Write the dataframes to the BytesIO object
    with pd.ExcelWriter(bytes_io, engine="xlsxwriter") as writer:
        for df, sheet_name in zip(dfs, sheet_names):
            df.to_excel(writer, sheet_name=sheet_name, index=False)

    # Get the bytes data
    bytes_data = bytes_io.getvalue()

    # Close the BytesIO object
    bytes_io.close()

    return bytes_data


def save_dataframe(df: pd.DataFrame, sheet_name: str):
    """
    Save the dataframe to a csv file.

    Args:
        df (pd.DataFrame): The dataframe to save.
        sheet_name (str): The name of the sheet.
    """
    df.to_csv(f"data2/{sheet_name}_{time.time()}.csv", index=False)