12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- from flask import (
- Flask, request, render_template, send_file,
- redirect, url_for
- )
- import os
- app = Flask(__name__)
- app.debug = True
- @app.route('/')
- def index():
- return render_template('index.html')
- def imgpath(p):
- return os.path.join('imgs/', p)
- @app.route('/img')
- def imgs():
- files = [
- f for f in os.listdir('imgs/')
- if os.path.isfile(imgpath(f))
- ]
- ordered = sorted(
- files, key=lambda f: os.stat(imgpath(f)).st_mtime,
- reverse=True
- )
- return '\n'.join([
- f'<img src="img/{f}" style="width: 100%"/>'
- for f in ordered
- ])
- @app.route('/img/<path:path>')
- def img(path):
- return send_file(os.path.join('imgs/', path))
- @app.route('/upload', methods=['POST'])
- def upload():
- file = request.files['file']
- file.save(os.path.join('imgs/', file.filename))
- return redirect(url_for('imgs'))
- app.run()
|