import os from flask import Flask, request, render_template, redirect, url_for, send_from_directory app = Flask(__name__) app.config['UPLOAD_FOLDER'] = 'static/uploads' app.config['ALLOWED_EXTENSIONS'] = {'mp3'} if not os.path.exists(app.config['UPLOAD_FOLDER']): os.makedirs(app.config['UPLOAD_FOLDER']) def allowed_file(filename): return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS'] @app.route('/') def index(): # Display the list of uploaded MP3 files files = os.listdir(app.config['UPLOAD_FOLDER']) return render_template('index.html', files=files) @app.route('/upload', methods=['GET', 'POST']) def upload_file(): if request.method == 'POST': if 'file' not in request.files: return redirect(request.url) file = request.files['file'] if file and allowed_file(file.filename): filename = file.filename file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) return redirect(url_for('index')) return render_template('upload.html') @app.route('/play/') def play_file(filename): # Page to play an uploaded MP3 file return render_template('play.html', filename=filename) @app.route('/uploads/') def uploaded_file(filename): return send_from_directory(app.config['UPLOAD_FOLDER'], filename) if __name__ == '__main__': app.run(debug=True)