Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,31 +19,50 @@ GID = "0"
|
|
| 19 |
GOOGLE_SHEET_CSV = f"https://docs.google.com/spreadsheets/d/{SHEET_ID}/export?format=csv&gid={GID}"
|
| 20 |
|
| 21 |
|
|
|
|
|
|
|
|
|
|
| 22 |
def fetch_sheet():
|
| 23 |
try:
|
| 24 |
response = requests.get(GOOGLE_SHEET_CSV)
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
df = pd.read_csv(StringIO(response.text))
|
|
|
|
|
|
|
| 28 |
df.columns = df.columns.str.strip().str.lower()
|
| 29 |
|
| 30 |
if "asset id" not in df.columns:
|
|
|
|
|
|
|
| 31 |
return None
|
| 32 |
|
| 33 |
df.rename(columns={"asset id": "Asset ID"}, inplace=True)
|
|
|
|
| 34 |
return df
|
| 35 |
|
| 36 |
-
except:
|
|
|
|
| 37 |
return None
|
| 38 |
|
| 39 |
|
|
|
|
|
|
|
|
|
|
| 40 |
@app.route("/")
|
| 41 |
def home():
|
| 42 |
return render_template("index.html")
|
| 43 |
|
| 44 |
|
|
|
|
|
|
|
|
|
|
| 45 |
@app.route("/generate", methods=["POST"])
|
| 46 |
def generate_qr():
|
|
|
|
| 47 |
asset_id = request.form["data"].strip()
|
| 48 |
fill_color = request.form["fill_color"]
|
| 49 |
back_color = request.form["back_color"]
|
|
@@ -63,8 +82,8 @@ def generate_qr():
|
|
| 63 |
|
| 64 |
row = result.iloc[0]
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
asset_url = request.
|
| 68 |
|
| 69 |
qr = qrcode.QRCode(
|
| 70 |
version=None,
|
|
@@ -78,8 +97,10 @@ def generate_qr():
|
|
| 78 |
|
| 79 |
img = qr.make_image(fill_color=fill_color, back_color=back_color).convert("RGB")
|
| 80 |
|
|
|
|
| 81 |
if logo and logo.filename != "":
|
| 82 |
logo_img = Image.open(logo)
|
|
|
|
| 83 |
logo_size = img.size[0] // 4
|
| 84 |
logo_img = logo_img.resize((logo_size, logo_size))
|
| 85 |
|
|
@@ -108,8 +129,12 @@ def generate_qr():
|
|
| 108 |
)
|
| 109 |
|
| 110 |
|
|
|
|
|
|
|
|
|
|
| 111 |
@app.route("/asset/<path:asset_id>")
|
| 112 |
def show_asset(asset_id):
|
|
|
|
| 113 |
df = fetch_sheet()
|
| 114 |
if df is None:
|
| 115 |
return "<h3>Unable to fetch data</h3>"
|
|
@@ -134,5 +159,8 @@ def show_asset(asset_id):
|
|
| 134 |
return render_template("asset_card.html", asset=asset_info)
|
| 135 |
|
| 136 |
|
|
|
|
|
|
|
|
|
|
| 137 |
if __name__ == "__main__":
|
| 138 |
-
app.run(
|
|
|
|
| 19 |
GOOGLE_SHEET_CSV = f"https://docs.google.com/spreadsheets/d/{SHEET_ID}/export?format=csv&gid={GID}"
|
| 20 |
|
| 21 |
|
| 22 |
+
# ==============================
|
| 23 |
+
# Fetch Google Sheet Data
|
| 24 |
+
# ==============================
|
| 25 |
def fetch_sheet():
|
| 26 |
try:
|
| 27 |
response = requests.get(GOOGLE_SHEET_CSV)
|
| 28 |
+
|
| 29 |
+
if response.status_code != 200:
|
| 30 |
+
print("Google Sheet not accessible")
|
| 31 |
+
return None
|
| 32 |
|
| 33 |
df = pd.read_csv(StringIO(response.text))
|
| 34 |
+
|
| 35 |
+
# Clean column names
|
| 36 |
df.columns = df.columns.str.strip().str.lower()
|
| 37 |
|
| 38 |
if "asset id" not in df.columns:
|
| 39 |
+
print("Column 'Asset ID' not found")
|
| 40 |
+
print("Available columns:", df.columns.tolist())
|
| 41 |
return None
|
| 42 |
|
| 43 |
df.rename(columns={"asset id": "Asset ID"}, inplace=True)
|
| 44 |
+
|
| 45 |
return df
|
| 46 |
|
| 47 |
+
except Exception as e:
|
| 48 |
+
print("Error fetching sheet:", e)
|
| 49 |
return None
|
| 50 |
|
| 51 |
|
| 52 |
+
# ==============================
|
| 53 |
+
# Home Page
|
| 54 |
+
# ==============================
|
| 55 |
@app.route("/")
|
| 56 |
def home():
|
| 57 |
return render_template("index.html")
|
| 58 |
|
| 59 |
|
| 60 |
+
# ==============================
|
| 61 |
+
# Generate QR
|
| 62 |
+
# ==============================
|
| 63 |
@app.route("/generate", methods=["POST"])
|
| 64 |
def generate_qr():
|
| 65 |
+
|
| 66 |
asset_id = request.form["data"].strip()
|
| 67 |
fill_color = request.form["fill_color"]
|
| 68 |
back_color = request.form["back_color"]
|
|
|
|
| 82 |
|
| 83 |
row = result.iloc[0]
|
| 84 |
|
| 85 |
+
# 🔥 IMPORTANT: Use correct base URL
|
| 86 |
+
asset_url = request.url_root + "asset/" + quote(asset_id)
|
| 87 |
|
| 88 |
qr = qrcode.QRCode(
|
| 89 |
version=None,
|
|
|
|
| 97 |
|
| 98 |
img = qr.make_image(fill_color=fill_color, back_color=back_color).convert("RGB")
|
| 99 |
|
| 100 |
+
# Add logo if uploaded
|
| 101 |
if logo and logo.filename != "":
|
| 102 |
logo_img = Image.open(logo)
|
| 103 |
+
|
| 104 |
logo_size = img.size[0] // 4
|
| 105 |
logo_img = logo_img.resize((logo_size, logo_size))
|
| 106 |
|
|
|
|
| 129 |
)
|
| 130 |
|
| 131 |
|
| 132 |
+
# ==============================
|
| 133 |
+
# Asset Detail Page (QR Opens This)
|
| 134 |
+
# ==============================
|
| 135 |
@app.route("/asset/<path:asset_id>")
|
| 136 |
def show_asset(asset_id):
|
| 137 |
+
|
| 138 |
df = fetch_sheet()
|
| 139 |
if df is None:
|
| 140 |
return "<h3>Unable to fetch data</h3>"
|
|
|
|
| 159 |
return render_template("asset_card.html", asset=asset_info)
|
| 160 |
|
| 161 |
|
| 162 |
+
# ==============================
|
| 163 |
+
# Run App
|
| 164 |
+
# ==============================
|
| 165 |
if __name__ == "__main__":
|
| 166 |
+
app.run(host="0.0.0.0", port=7860)
|