import io | |
import time | |
import pandas as pd | |
import streamlit as st | |
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) | |