main.py 895 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from flask import (
  2. Flask, request, render_template, send_file,
  3. redirect, url_for
  4. )
  5. import os
  6. app = Flask(__name__)
  7. app.debug = True
  8. @app.route('/')
  9. def index():
  10. return render_template('index.html')
  11. def imgpath(p):
  12. return os.path.join('imgs/', p)
  13. @app.route('/img')
  14. def imgs():
  15. files = [
  16. f for f in os.listdir('imgs/')
  17. if os.path.isfile(imgpath(f))
  18. ]
  19. ordered = sorted(
  20. files, key=lambda f: os.stat(imgpath(f)).st_mtime,
  21. reverse=True
  22. )
  23. return '\n'.join([
  24. f'<img src="img/{f}" style="width: 100%"/>'
  25. for f in ordered
  26. ])
  27. @app.route('/img/<path:path>')
  28. def img(path):
  29. return send_file(os.path.join('imgs/', path))
  30. @app.route('/upload', methods=['POST'])
  31. def upload():
  32. file = request.files['file']
  33. file.save(os.path.join('imgs/', file.filename))
  34. return redirect(url_for('imgs'))
  35. app.run()