Spaces:
Configuration error
Configuration error
getad72493
commited on
Commit
•
97c207d
1
Parent(s):
5e38a18
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,50 @@
|
|
1 |
-
|
2 |
-
|
|
|
|
|
3 |
|
4 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
@app.route('/')
|
7 |
-
def
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
if __name__ == '__main__':
|
11 |
-
app.run(debug=True
|
|
|
1 |
+
from flask import Flask, render_template, request, redirect, url_for, flash
|
2 |
+
import os
|
3 |
+
from werkzeug.utils import secure_filename
|
4 |
+
from math import ceil
|
5 |
|
6 |
app = Flask(__name__)
|
7 |
+
app.config['UPLOAD_FOLDER'] = 'uploads/'
|
8 |
+
app.config['STATIC_FOLDER'] = 'static/images'
|
9 |
+
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16 MB file upload limit
|
10 |
+
app.config['ALLOWED_EXTENSIONS'] = {'jpg', 'jpeg', 'png', 'gif'}
|
11 |
|
12 |
+
# Create folder if doesn't exist
|
13 |
+
os.makedirs(app.config['STATIC_FOLDER'], exist_ok=True)
|
14 |
+
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
15 |
+
|
16 |
+
# Check allowed file extensions
|
17 |
+
def allowed_file(filename):
|
18 |
+
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
|
19 |
+
|
20 |
+
# Route for the homepage (gallery)
|
21 |
@app.route('/')
|
22 |
+
def index():
|
23 |
+
image_files = os.listdir(app.config['STATIC_FOLDER'])
|
24 |
+
images_per_page = 6
|
25 |
+
page = request.args.get('page', 1, type=int)
|
26 |
+
total_images = len(image_files)
|
27 |
+
total_pages = ceil(total_images / images_per_page)
|
28 |
+
image_files = image_files[(page-1) * images_per_page : page * images_per_page]
|
29 |
+
return render_template('index.html', images=image_files, total_pages=total_pages, page=page)
|
30 |
+
|
31 |
+
# Route for the upload page
|
32 |
+
@app.route('/upload', methods=['GET', 'POST'])
|
33 |
+
def upload():
|
34 |
+
if request.method == 'POST':
|
35 |
+
if 'file' not in request.files:
|
36 |
+
flash('No file part')
|
37 |
+
return redirect(request.url)
|
38 |
+
|
39 |
+
files = request.files.getlist('file')
|
40 |
+
for file in files:
|
41 |
+
if file and allowed_file(file.filename):
|
42 |
+
filename = secure_filename(file.filename)
|
43 |
+
file.save(os.path.join(app.config['STATIC_FOLDER'], filename))
|
44 |
+
flash('Images successfully uploaded')
|
45 |
+
return redirect(url_for('index'))
|
46 |
+
|
47 |
+
return render_template('upload.html')
|
48 |
|
49 |
if __name__ == '__main__':
|
50 |
+
app.run(debug=True)
|