db_query / utils /convert_to_excel.py
DavMelchi's picture
check existing sheet if dump and DL for all BD
57fe85f
raw
history blame
958 Bytes
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)