Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-primitive.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-primitive.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-primitive.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Dan Q Consolidate duplicate QUICKSTARTs on the top-level file 503f830 7 months ago History
image_gallery.py · 4.5 KB · 164 lines
1"""
2Image Gallery Object
3
4Demonstrates file storage with image display.
5Shows uploaded images in an HTML gallery.
6"""
7
8
9def GET(request):
10 """Return HTML gallery of uploaded images"""
11
12 # Get list of files
13 files = _files.list()
14
15 # Filter for images
16 image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg']
17 images = [f for f in files if any(f['name'].lower().endswith(ext) for ext in image_extensions)]
18
19 # Build HTML gallery
20 html = """
21 <!DOCTYPE html>
22 <html>
23 <head>
24 <title>Image Gallery</title>
25 <style>
26 body {
27 font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
28 background: #0a0a0a;
29 color: #e0e0e0;
30 padding: 40px;
31 margin: 0;
32 }
33 h1 {
34 color: #fff;
35 margin-bottom: 30px;
36 }
37 .gallery {
38 display: grid;
39 grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
40 gap: 20px;
41 margin-top: 30px;
42 }
43 .image-card {
44 background: #1a1a1a;
45 border-radius: 8px;
46 overflow: hidden;
47 border: 1px solid #333;
48 transition: transform 0.2s;
49 }
50 .image-card:hover {
51 transform: scale(1.02);
52 border-color: #0ea5e9;
53 }
54 .image-card img {
55 width: 100%;
56 height: 200px;
57 object-fit: cover;
58 display: block;
59 }
60 .image-info {
61 padding: 15px;
62 }
63 .image-name {
64 font-weight: 600;
65 color: #fff;
66 margin-bottom: 8px;
67 word-break: break-word;
68 }
69 .image-size {
70 color: #999;
71 font-size: 13px;
72 }
73 .upload-form {
74 background: #1a1a1a;
75 padding: 25px;
76 border-radius: 8px;
77 border: 1px solid #333;
78 margin-bottom: 30px;
79 }
80 .upload-form h2 {
81 margin-top: 0;
82 color: #fff;
83 }
84 input[type="file"] {
85 display: block;
86 margin: 15px 0;
87 color: #e0e0e0;
88 }
89 button {
90 background: #0ea5e9;
91 color: white;
92 border: none;
93 padding: 10px 20px;
94 border-radius: 6px;
95 cursor: pointer;
96 font-size: 14px;
97 font-weight: 500;
98 }
99 button:hover {
100 background: #0284c7;
101 }
102 .empty {
103 text-align: center;
104 padding: 60px 20px;
105 color: #999;
106 }
107 </style>
108 </head>
109 <body>
110 <h1>Image Gallery</h1>
111
112 <div class="upload-form">
113 <h2>Upload Image</h2>
114 <form action="/objects/basics_image_gallery" method="POST" enctype="multipart/form-data">
115 <input type="file" name="file" accept="image/*" required>
116 <button type="submit">Upload</button>
117 </form>
118 </div>
119
120 <div class="gallery">
121 """
122
123 if len(images) == 0:
124 html += """
125 <div class="empty">No images uploaded yet. Use the form above to upload images.</div>
126 """
127 else:
128 for img in images:
129 size_kb = img['size'] / 1024
130 html += f"""
131 <div class="image-card">
132 <a href="/objects/basics_image_gallery?file={img['name']}" target="_blank">
133 <img src="/objects/basics_image_gallery?file={img['name']}" alt="{img['name']}">
134 </a>
135 <div class="image-info">
136 <div class="image-name">{img['name']}</div>
137 <div class="image-size">{size_kb:.1f} KB</div>
138 </div>
139 </div>
140 """
141
142 html += """
143 </div>
144 </body>
145 </html>
146 """
147
148 return {
149 'status': 'ok',
150 'content_type': 'text/html',
151 'body': html
152 }
153
154
155def POST(request):
156 """Handle image upload"""
157 # The API handler will process file uploads automatically
158 # and store them using _files.put()
159
160 return {
161 'status': 'ok',
162 'message': 'Image uploaded successfully',
163 'redirect': '/objects/basics_image_gallery'
164 }