-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
56 lines (44 loc) · 1.38 KB
/
app.py
File metadata and controls
56 lines (44 loc) · 1.38 KB
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
import os
from flask import Flask, render_template
from dotenv import load_dotenv
import cloudinary
from cloudinary import CloudinaryImage
load_dotenv()
cloudinary.config(cloudinary_url=os.getenv("CLOUDINARY_URL"), secure=True)
GALLERY_FOLDER = os.getenv("GALLERY_FOLDER", "pets")
MAX_RESULTS = int(os.getenv("MAX_RESULTS", "20"))
app = Flask(__name__)
def _thumb_url(public_id):
return CloudinaryImage(public_id).build_url(
transformation=[
{"crop": "fill", "width": 300, "height": 300},
]
)
def _full_url(public_id):
return CloudinaryImage(public_id).build_url(
transformation=[
{"crop": "fill", "width": 800, "height": 600},
]
)
@app.route("/")
def index():
result = (
cloudinary.Search()
.expression(f"folder={GALLERY_FOLDER}")
.sort_by("created_at", "desc")
.max_results(MAX_RESULTS)
.execute()
)
resources = result.get("resources", [])
items = []
for r in resources:
pid = r.get("public_id")
if pid:
items.append({
"public_id": pid,
"thumb": _thumb_url(pid),
"full": _full_url(pid)
})
return render_template("index.html", items=items, folder=GALLERY_FOLDER)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=int(os.getenv("PORT", "5000")), debug=True)