Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,43 +9,48 @@ from urllib.parse import quote
|
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
UPLOAD_FOLDER = "static/generated"
|
| 13 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 14 |
|
| 15 |
# ==============================
|
| 16 |
-
#
|
| 17 |
# ==============================
|
| 18 |
SHEET_ID = "1F90Q1scHpX4U_OIBDA_yZF3BozUGfJtl-1XucV4XBCM"
|
| 19 |
GID = "0"
|
| 20 |
|
| 21 |
-
# π₯ More reliable export format
|
| 22 |
GOOGLE_SHEET_CSV = f"https://docs.google.com/spreadsheets/d/{SHEET_ID}/gviz/tq?tqx=out:csv&gid={GID}"
|
| 23 |
|
| 24 |
|
| 25 |
# ==============================
|
| 26 |
-
# Fetch
|
| 27 |
# ==============================
|
| 28 |
def fetch_sheet():
|
| 29 |
try:
|
| 30 |
response = requests.get(GOOGLE_SHEET_CSV)
|
| 31 |
-
print("Status Code:", response.status_code)
|
| 32 |
|
| 33 |
if response.status_code != 200:
|
| 34 |
-
print("Sheet not accessible")
|
| 35 |
return None
|
| 36 |
|
| 37 |
-
if
|
| 38 |
-
print("
|
| 39 |
return None
|
| 40 |
|
| 41 |
df = pd.read_csv(StringIO(response.text))
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
|
|
|
|
| 45 |
df.columns = df.columns.str.strip().str.lower()
|
| 46 |
|
| 47 |
if "asset id" not in df.columns:
|
| 48 |
-
print("Column '
|
|
|
|
| 49 |
return None
|
| 50 |
|
| 51 |
df.rename(columns={"asset id": "Asset ID"}, inplace=True)
|
|
@@ -53,12 +58,12 @@ def fetch_sheet():
|
|
| 53 |
return df
|
| 54 |
|
| 55 |
except Exception as e:
|
| 56 |
-
print("
|
| 57 |
return None
|
| 58 |
|
| 59 |
|
| 60 |
# ==============================
|
| 61 |
-
# Debug Route
|
| 62 |
# ==============================
|
| 63 |
@app.route("/test")
|
| 64 |
def test():
|
|
@@ -81,104 +86,118 @@ def home():
|
|
| 81 |
# ==============================
|
| 82 |
@app.route("/generate", methods=["POST"])
|
| 83 |
def generate_qr():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
back_color = request.form["back_color"]
|
| 88 |
-
size = int(request.form["size"])
|
| 89 |
-
logo = request.files.get("logo")
|
| 90 |
-
|
| 91 |
-
df = fetch_sheet()
|
| 92 |
-
if df is None:
|
| 93 |
-
return render_template("index.html", error="Unable to fetch sheet")
|
| 94 |
-
|
| 95 |
-
df["Asset ID"] = df["Asset ID"].astype(str).str.strip()
|
| 96 |
-
|
| 97 |
-
result = df[df["Asset ID"].str.lower() == asset_id.lower()]
|
| 98 |
-
|
| 99 |
-
if result.empty:
|
| 100 |
-
return render_template("index.html", error="β Asset Not Found")
|
| 101 |
|
| 102 |
-
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
|
| 105 |
-
asset_url = request.url_root + "asset/" + quote(asset_id)
|
| 106 |
|
| 107 |
-
|
| 108 |
-
version=None,
|
| 109 |
-
error_correction=qrcode.constants.ERROR_CORRECT_H,
|
| 110 |
-
box_size=size,
|
| 111 |
-
border=4,
|
| 112 |
-
)
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
|
| 117 |
-
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
logo_img = Image.open(logo)
|
| 122 |
-
logo_size = img.size[0] // 4
|
| 123 |
-
logo_img = logo_img.resize((logo_size, logo_size))
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
| 128 |
)
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
-
|
| 144 |
-
"
|
| 145 |
-
qr_image="generated/qr.png",
|
| 146 |
-
asset=asset_info
|
| 147 |
-
)
|
| 148 |
|
| 149 |
|
| 150 |
# ==============================
|
| 151 |
-
# Asset
|
| 152 |
# ==============================
|
| 153 |
@app.route("/asset/<path:asset_id>")
|
| 154 |
def show_asset(asset_id):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
-
|
| 157 |
-
if df is None:
|
| 158 |
-
return "<h3>Unable to fetch data</h3>"
|
| 159 |
|
| 160 |
-
|
| 161 |
|
| 162 |
-
|
|
|
|
| 163 |
|
| 164 |
-
|
| 165 |
-
return "<h3>Asset Not Found</h3>"
|
| 166 |
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
-
|
| 170 |
-
"Asset ID": row.get("Asset ID", ""),
|
| 171 |
-
"Asset Type": row.get("asset type", ""),
|
| 172 |
-
"Asset Name": row.get("asset name", ""),
|
| 173 |
-
"Allotted To": row.get("allotted to", ""),
|
| 174 |
-
"Department": row.get("department", ""),
|
| 175 |
-
}
|
| 176 |
|
| 177 |
-
|
|
|
|
| 178 |
|
| 179 |
|
| 180 |
# ==============================
|
| 181 |
-
# Run
|
| 182 |
# ==============================
|
| 183 |
if __name__ == "__main__":
|
| 184 |
-
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
|
| 12 |
+
# ==============================
|
| 13 |
+
# Folder Setup
|
| 14 |
+
# ==============================
|
| 15 |
UPLOAD_FOLDER = "static/generated"
|
| 16 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 17 |
|
| 18 |
# ==============================
|
| 19 |
+
# Google Sheet Configuration
|
| 20 |
# ==============================
|
| 21 |
SHEET_ID = "1F90Q1scHpX4U_OIBDA_yZF3BozUGfJtl-1XucV4XBCM"
|
| 22 |
GID = "0"
|
| 23 |
|
|
|
|
| 24 |
GOOGLE_SHEET_CSV = f"https://docs.google.com/spreadsheets/d/{SHEET_ID}/gviz/tq?tqx=out:csv&gid={GID}"
|
| 25 |
|
| 26 |
|
| 27 |
# ==============================
|
| 28 |
+
# Fetch Sheet Data (Safe Version)
|
| 29 |
# ==============================
|
| 30 |
def fetch_sheet():
|
| 31 |
try:
|
| 32 |
response = requests.get(GOOGLE_SHEET_CSV)
|
|
|
|
| 33 |
|
| 34 |
if response.status_code != 200:
|
| 35 |
+
print("β Google Sheet not accessible")
|
| 36 |
return None
|
| 37 |
|
| 38 |
+
if len(response.text.strip()) == 0:
|
| 39 |
+
print("β Empty Sheet Response")
|
| 40 |
return None
|
| 41 |
|
| 42 |
df = pd.read_csv(StringIO(response.text))
|
| 43 |
|
| 44 |
+
if df.empty:
|
| 45 |
+
print("β Sheet is empty")
|
| 46 |
+
return None
|
| 47 |
|
| 48 |
+
# Clean column names
|
| 49 |
df.columns = df.columns.str.strip().str.lower()
|
| 50 |
|
| 51 |
if "asset id" not in df.columns:
|
| 52 |
+
print("β Column 'Asset ID' not found")
|
| 53 |
+
print("Available columns:", df.columns.tolist())
|
| 54 |
return None
|
| 55 |
|
| 56 |
df.rename(columns={"asset id": "Asset ID"}, inplace=True)
|
|
|
|
| 58 |
return df
|
| 59 |
|
| 60 |
except Exception as e:
|
| 61 |
+
print("β Sheet Error:", e)
|
| 62 |
return None
|
| 63 |
|
| 64 |
|
| 65 |
# ==============================
|
| 66 |
+
# Debug Route
|
| 67 |
# ==============================
|
| 68 |
@app.route("/test")
|
| 69 |
def test():
|
|
|
|
| 86 |
# ==============================
|
| 87 |
@app.route("/generate", methods=["POST"])
|
| 88 |
def generate_qr():
|
| 89 |
+
try:
|
| 90 |
+
asset_id = request.form.get("data", "").strip()
|
| 91 |
+
fill_color = request.form.get("fill_color", "black")
|
| 92 |
+
back_color = request.form.get("back_color", "white")
|
| 93 |
+
size = int(request.form.get("size", 10))
|
| 94 |
+
logo = request.files.get("logo")
|
| 95 |
|
| 96 |
+
if not asset_id:
|
| 97 |
+
return render_template("index.html", error="Enter Asset ID")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
df = fetch_sheet()
|
| 100 |
+
if df is None:
|
| 101 |
+
return render_template("index.html", error="Unable to fetch sheet")
|
| 102 |
|
| 103 |
+
df["Asset ID"] = df["Asset ID"].astype(str).str.strip()
|
|
|
|
| 104 |
|
| 105 |
+
result = df[df["Asset ID"].str.lower() == asset_id.lower()]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
if result.empty:
|
| 108 |
+
return render_template("index.html", error="β Asset Not Found")
|
| 109 |
|
| 110 |
+
row = result.iloc[0]
|
| 111 |
|
| 112 |
+
# Generate URL for QR
|
| 113 |
+
asset_url = request.url_root + "asset/" + quote(asset_id)
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
qr = qrcode.QRCode(
|
| 116 |
+
error_correction=qrcode.constants.ERROR_CORRECT_H,
|
| 117 |
+
box_size=size,
|
| 118 |
+
border=4,
|
| 119 |
)
|
| 120 |
|
| 121 |
+
qr.add_data(asset_url)
|
| 122 |
+
qr.make(fit=True)
|
| 123 |
+
|
| 124 |
+
img = qr.make_image(
|
| 125 |
+
fill_color=fill_color,
|
| 126 |
+
back_color=back_color
|
| 127 |
+
).convert("RGB")
|
| 128 |
+
|
| 129 |
+
# Add Logo if uploaded
|
| 130 |
+
if logo and logo.filename != "":
|
| 131 |
+
try:
|
| 132 |
+
logo_img = Image.open(logo)
|
| 133 |
+
logo_size = img.size[0] // 4
|
| 134 |
+
logo_img = logo_img.resize((logo_size, logo_size))
|
| 135 |
+
|
| 136 |
+
pos = (
|
| 137 |
+
(img.size[0] - logo_size) // 2,
|
| 138 |
+
(img.size[1] - logo_size) // 2
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
img.paste(logo_img, pos)
|
| 142 |
+
except:
|
| 143 |
+
print("Logo Error - Skipping logo")
|
| 144 |
+
|
| 145 |
+
qr_path = os.path.join(UPLOAD_FOLDER, "qr.png")
|
| 146 |
+
img.save(qr_path)
|
| 147 |
+
|
| 148 |
+
asset_info = {
|
| 149 |
+
"Asset ID": row.get("Asset ID", ""),
|
| 150 |
+
"Asset Type": row.get("asset type", ""),
|
| 151 |
+
"Asset Name": row.get("asset name", ""),
|
| 152 |
+
"Allotted To": row.get("allotted to", ""),
|
| 153 |
+
"Department": row.get("department", ""),
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
return render_template(
|
| 157 |
+
"index.html",
|
| 158 |
+
qr_image="generated/qr.png",
|
| 159 |
+
asset=asset_info
|
| 160 |
+
)
|
| 161 |
|
| 162 |
+
except Exception as e:
|
| 163 |
+
return f"β Application Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
|
| 166 |
# ==============================
|
| 167 |
+
# Asset Details Page (QR Opens This)
|
| 168 |
# ==============================
|
| 169 |
@app.route("/asset/<path:asset_id>")
|
| 170 |
def show_asset(asset_id):
|
| 171 |
+
try:
|
| 172 |
+
df = fetch_sheet()
|
| 173 |
+
if df is None:
|
| 174 |
+
return "<h3>Unable to fetch data</h3>"
|
| 175 |
|
| 176 |
+
df["Asset ID"] = df["Asset ID"].astype(str).str.strip()
|
|
|
|
|
|
|
| 177 |
|
| 178 |
+
result = df[df["Asset ID"].str.lower() == asset_id.lower()]
|
| 179 |
|
| 180 |
+
if result.empty:
|
| 181 |
+
return "<h3>Asset Not Found</h3>"
|
| 182 |
|
| 183 |
+
row = result.iloc[0]
|
|
|
|
| 184 |
|
| 185 |
+
asset_info = {
|
| 186 |
+
"Asset ID": row.get("Asset ID", ""),
|
| 187 |
+
"Asset Type": row.get("asset type", ""),
|
| 188 |
+
"Asset Name": row.get("asset name", ""),
|
| 189 |
+
"Allotted To": row.get("allotted to", ""),
|
| 190 |
+
"Department": row.get("department", ""),
|
| 191 |
+
}
|
| 192 |
|
| 193 |
+
return render_template("asset_card.html", asset=asset_info)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
+
except Exception as e:
|
| 196 |
+
return f"<h3>Error: {str(e)}</h3>"
|
| 197 |
|
| 198 |
|
| 199 |
# ==============================
|
| 200 |
+
# Run Server
|
| 201 |
# ==============================
|
| 202 |
if __name__ == "__main__":
|
| 203 |
+
app.run(host="0.0.0.0", port=7860, debug=True)
|