Spaces:
Configuration error
Configuration error
getad72493
commited on
Commit
•
bfb8a4e
1
Parent(s):
eff4332
Update app.py
Browse files
app.py
CHANGED
@@ -3,48 +3,65 @@ 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
|
13 |
os.makedirs(app.config['STATIC_FOLDER'], exist_ok=True)
|
14 |
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
15 |
|
16 |
-
#
|
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 |
-
|
|
|
3 |
from werkzeug.utils import secure_filename
|
4 |
from math import ceil
|
5 |
|
6 |
+
# Initialize the Flask app
|
7 |
app = Flask(__name__)
|
8 |
+
|
9 |
+
# Set up configurations
|
10 |
app.config['UPLOAD_FOLDER'] = 'uploads/'
|
11 |
app.config['STATIC_FOLDER'] = 'static/images'
|
12 |
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16 MB file upload limit
|
13 |
app.config['ALLOWED_EXTENSIONS'] = {'jpg', 'jpeg', 'png', 'gif'}
|
14 |
+
app.config['SECRET_KEY'] = os.urandom(24) # Secret key for session management and flash messages
|
15 |
|
16 |
+
# Create necessary directories if they don't exist
|
17 |
os.makedirs(app.config['STATIC_FOLDER'], exist_ok=True)
|
18 |
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
19 |
|
20 |
+
# Function to check if the uploaded file has a valid extension
|
21 |
def allowed_file(filename):
|
22 |
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
|
23 |
|
24 |
# Route for the homepage (gallery)
|
25 |
@app.route('/')
|
26 |
def index():
|
27 |
+
# Get the list of image files in the static folder
|
28 |
image_files = os.listdir(app.config['STATIC_FOLDER'])
|
29 |
+
|
30 |
+
# Pagination setup
|
31 |
images_per_page = 6
|
32 |
page = request.args.get('page', 1, type=int)
|
33 |
total_images = len(image_files)
|
34 |
total_pages = ceil(total_images / images_per_page)
|
35 |
+
|
36 |
+
# Slice the image list to show only the images for the current page
|
37 |
image_files = image_files[(page-1) * images_per_page : page * images_per_page]
|
38 |
+
|
39 |
return render_template('index.html', images=image_files, total_pages=total_pages, page=page)
|
40 |
|
41 |
# Route for the upload page
|
42 |
@app.route('/upload', methods=['GET', 'POST'])
|
43 |
def upload():
|
44 |
if request.method == 'POST':
|
45 |
+
# Check if the file part exists
|
46 |
if 'file' not in request.files:
|
47 |
flash('No file part')
|
48 |
return redirect(request.url)
|
49 |
|
50 |
files = request.files.getlist('file')
|
51 |
for file in files:
|
52 |
+
# Check if the file is allowed
|
53 |
if file and allowed_file(file.filename):
|
54 |
+
# Secure the filename and save the file
|
55 |
filename = secure_filename(file.filename)
|
56 |
file.save(os.path.join(app.config['STATIC_FOLDER'], filename))
|
57 |
+
|
58 |
+
# Flash a success message and redirect to the index page
|
59 |
flash('Images successfully uploaded')
|
60 |
return redirect(url_for('index'))
|
61 |
|
62 |
+
# Render the upload page template
|
63 |
return render_template('upload.html')
|
64 |
|
65 |
+
# Start the Flask app in production mode (Hugging Face will handle WSGI)
|
66 |
if __name__ == '__main__':
|
67 |
+
app.run(debug=False, host='0.0.0.0', port=7860)
|