File size: 1,627 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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_dataframes(dfs: list[pd.DataFrame], sheet_names: list[str], folder_path: str):
#     """
#     Save the dataframes to an excel file. The excel file will be saved in the
#     folder_path directory.

#     Args:
#         dfs (list[pd.DataFrame]): The list of dataframes to save.
#         sheet_names (list[str]): The list of names for each sheet.
#         folder_path (str): The path to the folder where the excel file will be saved.
#     """
#     bytes_data = convert_dfs(dfs, sheet_names)
#     timestamp = int(time.time())
#     file_name = f"{folder_path}/data_{timestamp}.xlsx"
#     with open(file_name, "wb") as f:
#         f.write(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)