chandanzeon's picture
First Commit
2d3bc6e
raw
history blame
4.61 kB
import streamlit as st
import pandas as pd
from io import BytesIO
from helper import get_res_df
def to_excel(df):
"""
Convert a Pandas DataFrame to an Excel file in memory.
Parameters:
df (DataFrame): The DataFrame to be converted to Excel format.
Returns:
bytes: The in-memory Excel file data.
"""
output = BytesIO()
# Use the Pandas ExcelWriter to write the DataFrame to an in-memory file
with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
df.to_excel(writer, index=False)
processed_data = output.getvalue()
return processed_data
def process_files(excel_file, text_file):
"""
Process the uploaded Excel/CSV and text files and return cleaned dataframes.
Parameters:
excel_file (UploadedFile): The uploaded Excel or CSV file.
text_file (UploadedFile): The uploaded text file.
Returns:
Tuple[DataFrame, DataFrame]: A tuple containing the cleaned DataFrame from the Excel/CSV file
and a DataFrame created from the text file data.
"""
print(excel_file, text_file) # Debugging information
# Read the Excel/CSV file into a DataFrame
if excel_file.name.endswith('.csv'):
df_excel = pd.read_csv(excel_file)
else:
df_excel = pd.read_excel(excel_file)
# Ensure the 'cfcf' column values are formatted as zero-padded 6-digit strings
df_excel['cfcf'] = [str(number).zfill(6) for number in df_excel['cfcf']]
# Read and process the text file content into a list of lines
lines = text_file.read().decode('utf-8').splitlines()
data = [line.strip().split(',') for line in lines] # Split each line by commas
# Create a DataFrame from the parsed text file data
df = pd.DataFrame(data)
return df_excel, df
# Streamlit UI section
st.title("Fetch Employer") # Application title
# File uploader widgets to allow users to upload an Excel/CSV file and a text file
uploaded_excel = st.file_uploader("Upload the Master file(.xls or .csv)", type=["csv", "xls", "xlsx"])
uploaded_text = st.file_uploader("Upload your Text file(.txt)", type=["txt"])
# Check if both files are uploaded
if uploaded_excel and uploaded_text:
st.write("Processing the files...") # Inform the user that the files are being processed
master_data, df = process_files(uploaded_excel, uploaded_text) # Process the files
st.write("Final Output") # Display the result of file processing
res = get_res_df(master_data, df) # Generate the result DataFrame using the helper function
st.dataframe(res) # Show the result in a table format on the web app
# Convert the result DataFrame to an Excel file for download
excel_data = to_excel(res)
# Provide a button for the user to download the result as an Excel file
st.download_button(label="Download Excel",
data=excel_data,
file_name='Fetch_Employer_Output.xlsx',
mime='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
# import streamlit as st
# import pandas as pd
# from io import BytesIO
# from helper import get_res_df
# def to_excel(df):
# output = BytesIO()
# with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
# df.to_excel(writer, index=False)
# processed_data = output.getvalue()
# return processed_data
# def process_files(excel_file, text_file):
# print(excel_file,text_file)
# if excel_file.name.endswith('.csv'):
# df_excel = pd.read_csv(excel_file)
# else:
# df_excel = pd.read_excel(excel_file)
# df_excel['cfcf']=[str(number).zfill(6) for number in df_excel['cfcf']]
# lines = text_file.read().decode('utf-8').splitlines()
# data = [line.strip().split(',') for line in lines]
# df = pd.DataFrame(data)
# return df_excel,df
# st.title("Fetch Employer")
# uploaded_excel = st.file_uploader("Upload the Master file(.xls or .csv)", type=["csv", "xls", "xlsx"])
# uploaded_text = st.file_uploader("Upload your Text file(.txt)", type=["txt"])
# if uploaded_excel and uploaded_text:
# st.write("Processing the files...")
# master_data, df = process_files(uploaded_excel, uploaded_text)
# st.write("Final Output")
# res = get_res_df(master_data,df)
# st.dataframe(res)
# excel_data = to_excel(res)
# st.download_button(label="Download Excel",
# data=excel_data,
# file_name='Fetch_Employer_Output.xlsx',
# mime='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')