Spaces:
Sleeping
Sleeping
File size: 5,332 Bytes
4204616 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
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)
|