Update app.py
Browse files4 unmodified lines56789101112131415161729 unmodified lines47484950515253545556575859606162636465666768697045 unmodified lines1161171181191201211224 unmodified linesfrom docx.enum.table import WD_ALIGN_VERTICALimport gradio as grimport difflib# -------- Helper functions --------def find_best_match(target, columns):"""Find the closest matching column name using fuzzy matching."""matches = difflib.get_close_matches(target.lower(), [c.lower() for c in columns], n=1, cutoff=0.4)if matches:# Return original column with same casefor col in columns:if col.lower() == matches[0]:return col29 unmodified linesif not all([name_col, regular_hours_col, total_col]):raise ValueError(f"Required columns missing. Found columns: {columns}")# Setup label page parameterslabels_per_page = 30cols = 3rows = 10doc = Document()# Generate labels in table format: 3 cols x 10 rows per pagefor i in range(0, len(df), labels_per_page):chunk = df.iloc[i:i+labels_per_page]table = doc.add_table(rows=rows, cols=cols)table.autofit = False# Set cell width (adjust as needed)for c_idx in range(cols):for r_idx in range(rows):cell = table.cell(r_idx, c_idx)cell.width = Pt(180)for idx, (_, row) in enumerate(chunk.iterrows()):r = idx // cols45 unmodified linesreturn output_fileexcept Exception as e:return f"❌ Error: {e}"# -------- Gradio App --------with gr.Blocks() as demo:4 unmodified lines56789101112131415161729 unmodified lines4748495051525354555657585960616263646566676845 unmodified lines1141151161171181191201214 unmodified linesfrom docx.enum.table import WD_ALIGN_VERTICALimport gradio as grimport difflibimport traceback# -------- Helper functions --------def find_best_match(target, columns):"""Find the closest matching column name using fuzzy matching."""matches = difflib.get_close_matches(target.lower(), [c.lower() for c in columns], n=1, cutoff=0.4)if matches:for col in columns:if col.lower() == matches[0]:return col29 unmodified linesif not all([name_col, regular_hours_col, total_col]):raise ValueError(f"Required columns missing. Found columns: {columns}")labels_per_page = 30cols = 3rows = 10doc = Document()for i in range(0, len(df), labels_per_page):chunk = df.iloc[i:i+labels_per_page]table = doc.add_table(rows=rows, cols=cols)table.autofit = False# Optional: set cell width (adjust as needed)for c_idx in range(cols):for r_idx in range(rows):cell = table.cell(r_idx, c_idx)cell.width = Pt(180)for idx, (_, row) in enumerate(chunk.iterrows()):r = idx // cols45 unmodified linesreturn output_fileexcept Exception as e:tb = traceback.format_exc()return f"❌ Error: {e}\nDetails:\n{tb}"# -------- Gradio App --------with gr.Blocks() as demo:
app.py+5-6