Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import os
|
4 |
+
from datetime import datetime
|
5 |
+
from PIL import Image
|
6 |
+
from fpdf import FPDF
|
7 |
+
|
8 |
+
# Load data
|
9 |
+
def load_data():
|
10 |
+
if os.path.exists("data.csv"):
|
11 |
+
return pd.read_csv("data.csv")
|
12 |
+
return pd.DataFrame(columns=["Name", "Phone", "SizingText", "ImagePath", "StitchingHistory"])
|
13 |
+
|
14 |
+
# Save data
|
15 |
+
def save_data(df):
|
16 |
+
df.to_csv("data.csv", index=False)
|
17 |
+
|
18 |
+
# Export customer record as PDF
|
19 |
+
def export_as_pdf(customer):
|
20 |
+
pdf = FPDF()
|
21 |
+
pdf.add_page()
|
22 |
+
pdf.set_font("Arial", size=12)
|
23 |
+
|
24 |
+
pdf.cell(200, 10, txt="Tailor Record", ln=True, align="C")
|
25 |
+
pdf.ln(10)
|
26 |
+
for key, value in customer.items():
|
27 |
+
pdf.multi_cell(0, 10, f"{key}: {value}")
|
28 |
+
filename = f"{customer['Name']}_record.pdf"
|
29 |
+
pdf.output(filename)
|
30 |
+
return filename
|
31 |
+
|
32 |
+
# Initialize app
|
33 |
+
st.set_page_config("Tailor Record App", layout="wide")
|
34 |
+
st.title("π Tailor Record Management System")
|
35 |
+
|
36 |
+
df = load_data()
|
37 |
+
|
38 |
+
# Add Record
|
39 |
+
st.subheader("β Add New Customer Record")
|
40 |
+
with st.form("new_customer"):
|
41 |
+
col1, col2 = st.columns(2)
|
42 |
+
with col1:
|
43 |
+
name = st.text_input("Customer Name*", max_chars=100)
|
44 |
+
phone = st.text_input("Phone Number*", max_chars=15)
|
45 |
+
with col2:
|
46 |
+
sizing_text = st.text_area("Sizing Details (Text)", height=200)
|
47 |
+
image = st.file_uploader("Optional: Upload sizing photo", type=["png", "jpg", "jpeg"])
|
48 |
+
|
49 |
+
st.markdown("**Stitching History (Optional):**")
|
50 |
+
history_entries = []
|
51 |
+
with st.expander("β Add Stitching History Entries (Optional)"):
|
52 |
+
num_entries = st.number_input("Number of entries", min_value=0, max_value=10, step=1)
|
53 |
+
for i in range(num_entries):
|
54 |
+
date = st.date_input(f"Stitch Date {i+1}", key=f"date_{i}")
|
55 |
+
desc = st.text_input(f"Description (optional) for Stitch {i+1}", key=f"desc_{i}")
|
56 |
+
history_entries.append(f"{date} - {desc}")
|
57 |
+
|
58 |
+
submitted = st.form_submit_button("πΎ Save Record")
|
59 |
+
|
60 |
+
if submitted:
|
61 |
+
if not name or not phone or not phone.isnumeric():
|
62 |
+
st.error("Customer Name and valid Phone Number are required!")
|
63 |
+
else:
|
64 |
+
image_path = ""
|
65 |
+
if image:
|
66 |
+
os.makedirs("images", exist_ok=True)
|
67 |
+
image_path = f"images/{datetime.now().strftime('%Y%m%d%H%M%S')}_{image.name}"
|
68 |
+
with open(image_path, "wb") as f:
|
69 |
+
f.write(image.read())
|
70 |
+
new_record = {
|
71 |
+
"Name": name,
|
72 |
+
"Phone": phone,
|
73 |
+
"SizingText": sizing_text,
|
74 |
+
"ImagePath": image_path,
|
75 |
+
"StitchingHistory": " || ".join(history_entries)
|
76 |
+
}
|
77 |
+
df = pd.concat([df, pd.DataFrame([new_record])], ignore_index=True)
|
78 |
+
df = df.sort_values(by="Name").reset_index(drop=True)
|
79 |
+
save_data(df)
|
80 |
+
st.success("Customer record saved successfully.")
|
81 |
+
|
82 |
+
# View Records
|
83 |
+
st.subheader("π View / Manage Customer Records")
|
84 |
+
|
85 |
+
if df.empty:
|
86 |
+
st.info("No customer records found.")
|
87 |
+
else:
|
88 |
+
selected_index = st.selectbox("Select customer to manage", df.index, format_func=lambda i: df.loc[i, "Name"])
|
89 |
+
customer = df.loc[selected_index]
|
90 |
+
|
91 |
+
st.markdown(f"### π Details for **{customer['Name']}**")
|
92 |
+
st.write(f"**Phone:** {customer['Phone']}")
|
93 |
+
st.write(f"**Sizing Text:** {customer['SizingText']}")
|
94 |
+
if customer["ImagePath"] and os.path.exists(customer["ImagePath"]):
|
95 |
+
st.image(customer["ImagePath"], caption="Sizing Image", use_column_width=True)
|
96 |
+
if customer["StitchingHistory"]:
|
97 |
+
st.markdown("**π§΅ Stitching History:**")
|
98 |
+
for entry in customer["StitchingHistory"].split(" || "):
|
99 |
+
st.markdown(f"- {entry}")
|
100 |
+
|
101 |
+
col1, col2, col3 = st.columns(3)
|
102 |
+
with col1:
|
103 |
+
if st.button("π Update Record"):
|
104 |
+
with st.form("update_form"):
|
105 |
+
updated_name = st.text_input("Name", value=customer["Name"])
|
106 |
+
updated_phone = st.text_input("Phone", value=customer["Phone"])
|
107 |
+
updated_sizing = st.text_area("Sizing Text", value=customer["SizingText"], height=200)
|
108 |
+
submitted_update = st.form_submit_button("Update Now")
|
109 |
+
if submitted_update:
|
110 |
+
if not updated_name or not updated_phone.isnumeric():
|
111 |
+
st.error("Name and valid phone number are required.")
|
112 |
+
else:
|
113 |
+
df.at[selected_index, "Name"] = updated_name
|
114 |
+
df.at[selected_index, "Phone"] = updated_phone
|
115 |
+
df.at[selected_index, "SizingText"] = updated_sizing
|
116 |
+
save_data(df)
|
117 |
+
st.success("Record updated.")
|
118 |
+
|
119 |
+
with col2:
|
120 |
+
if st.button("ποΈ Delete Record"):
|
121 |
+
confirm = st.checkbox("Confirm delete")
|
122 |
+
if confirm:
|
123 |
+
if customer["ImagePath"] and os.path.exists(customer["ImagePath"]):
|
124 |
+
os.remove(customer["ImagePath"])
|
125 |
+
df = df.drop(index=selected_index).reset_index(drop=True)
|
126 |
+
save_data(df)
|
127 |
+
st.success("Record deleted.")
|
128 |
+
|
129 |
+
with col3:
|
130 |
+
if st.button("π€ Export as PDF"):
|
131 |
+
filename = export_as_pdf(customer)
|
132 |
+
with open(filename, "rb") as f:
|
133 |
+
st.download_button("Download PDF", f, file_name=filename)
|
134 |
+
|