Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
import os | |
from datetime import datetime | |
from PIL import Image | |
from fpdf import FPDF | |
# Load data | |
def load_data(): | |
if os.path.exists("data.csv"): | |
return pd.read_csv("data.csv") | |
return pd.DataFrame(columns=["Name", "Phone", "SizingText", "ImagePath", "StitchingHistory"]) | |
# Save data | |
def save_data(df): | |
df.to_csv("data.csv", index=False) | |
# Export customer record as PDF | |
def export_as_pdf(customer): | |
pdf = FPDF() | |
pdf.add_page() | |
pdf.set_font("Arial", size=12) | |
pdf.cell(200, 10, txt="Tailor Record", ln=True, align="C") | |
pdf.ln(10) | |
for key, value in customer.items(): | |
pdf.multi_cell(0, 10, f"{key}: {value}") | |
filename = f"{customer['Name']}_record.pdf" | |
pdf.output(filename) | |
return filename | |
# Initialize app | |
st.set_page_config("Tailor Record App", layout="wide") | |
st.title("π Tailor Record Management System") | |
df = load_data() | |
# Add Record | |
st.subheader("β Add New Customer Record") | |
with st.form("new_customer"): | |
col1, col2 = st.columns(2) | |
with col1: | |
name = st.text_input("Customer Name*", max_chars=100) | |
phone = st.text_input("Phone Number*", max_chars=15) | |
with col2: | |
sizing_text = st.text_area("Sizing Details (Text)", height=200) | |
image = st.file_uploader("Optional: Upload sizing photo", type=["png", "jpg", "jpeg"]) | |
st.markdown("**Stitching History (Optional):**") | |
history_entries = [] | |
with st.expander("β Add Stitching History Entries (Optional)"): | |
num_entries = st.number_input("Number of entries", min_value=0, max_value=10, step=1) | |
for i in range(num_entries): | |
date = st.date_input(f"Stitch Date {i+1}", key=f"date_{i}") | |
desc = st.text_input(f"Description (optional) for Stitch {i+1}", key=f"desc_{i}") | |
history_entries.append(f"{date} - {desc}") | |
submitted = st.form_submit_button("πΎ Save Record") | |
if submitted: | |
if not name or not phone or not phone.isnumeric(): | |
st.error("Customer Name and valid Phone Number are required!") | |
else: | |
image_path = "" | |
if image: | |
os.makedirs("images", exist_ok=True) | |
image_path = f"images/{datetime.now().strftime('%Y%m%d%H%M%S')}_{image.name}" | |
with open(image_path, "wb") as f: | |
f.write(image.read()) | |
new_record = { | |
"Name": name, | |
"Phone": phone, | |
"SizingText": sizing_text, | |
"ImagePath": image_path, | |
"StitchingHistory": " || ".join(history_entries) | |
} | |
df = pd.concat([df, pd.DataFrame([new_record])], ignore_index=True) | |
df = df.sort_values(by="Name").reset_index(drop=True) | |
save_data(df) | |
st.success("Customer record saved successfully.") | |
# View Records | |
st.subheader("π View / Manage Customer Records") | |
if df.empty: | |
st.info("No customer records found.") | |
else: | |
selected_index = st.selectbox("Select customer to manage", df.index, format_func=lambda i: df.loc[i, "Name"]) | |
customer = df.loc[selected_index] | |
st.markdown(f"### π Details for **{customer['Name']}**") | |
st.write(f"**Phone:** {customer['Phone']}") | |
st.write(f"**Sizing Text:** {customer['SizingText']}") | |
if customer["ImagePath"] and os.path.exists(customer["ImagePath"]): | |
st.image(customer["ImagePath"], caption="Sizing Image", use_column_width=True) | |
if customer["StitchingHistory"]: | |
st.markdown("**π§΅ Stitching History:**") | |
for entry in customer["StitchingHistory"].split(" || "): | |
st.markdown(f"- {entry}") | |
col1, col2, col3 = st.columns(3) | |
with col1: | |
if st.button("π Update Record"): | |
with st.form("update_form"): | |
updated_name = st.text_input("Name", value=customer["Name"]) | |
updated_phone = st.text_input("Phone", value=customer["Phone"]) | |
updated_sizing = st.text_area("Sizing Text", value=customer["SizingText"], height=200) | |
submitted_update = st.form_submit_button("Update Now") | |
if submitted_update: | |
if not updated_name or not updated_phone.isnumeric(): | |
st.error("Name and valid phone number are required.") | |
else: | |
df.at[selected_index, "Name"] = updated_name | |
df.at[selected_index, "Phone"] = updated_phone | |
df.at[selected_index, "SizingText"] = updated_sizing | |
save_data(df) | |
st.success("Record updated.") | |
with col2: | |
if st.button("ποΈ Delete Record"): | |
confirm = st.checkbox("Confirm delete") | |
if confirm: | |
if customer["ImagePath"] and os.path.exists(customer["ImagePath"]): | |
os.remove(customer["ImagePath"]) | |
df = df.drop(index=selected_index).reset_index(drop=True) | |
save_data(df) | |
st.success("Record deleted.") | |
with col3: | |
if st.button("π€ Export as PDF"): | |
filename = export_as_pdf(customer) | |
with open(filename, "rb") as f: | |
st.download_button("Download PDF", f, file_name=filename) | |