Serve a random image from imgfolder on /random_image endpoint

This commit is contained in:
Michael Mandl 2018-11-03 14:00:06 +01:00
parent f0fb5f0d9f
commit 96993ac3d0
3 changed files with 11 additions and 2 deletions

View file

@ -1,7 +1,9 @@
#!/usr/bin/python3
from flask import Flask, render_template, send_from_directory
from flask import Flask, render_template, send_from_directory, redirect, url_for
from random import randint
from os import listdir
import random
import config
@ -13,6 +15,12 @@ def hello():
return render_template("hello.html", num=randint(1, 23))
@app.route("/random_image")
def random_image():
filename = random.choice(listdir(config.imgdir))
return redirect(url_for("image", filename=filename))
@app.route("/img/<path:filename>")
def image(filename):
return send_from_directory(config.imgdir, filename)