Serve a random image from imgfolder on /random_image endpoint

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

View File

@ -1 +1 @@
imgdir = "/home/mandlm/Foto Export/2018/09/2018-09-03" imgdir = "."

View File

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

View File

@ -3,5 +3,6 @@
<p>Hello slider number {{ num }}!</p> <p>Hello slider number {{ num }}!</p>
<p><img src="{{ url_for("static", filename="cat.jpg") }}"></p> <p><img src="{{ url_for("static", filename="cat.jpg") }}"></p>
<p><a href="{{ url_for("image", filename="IMG_0001.jpg") }}">Image</a></p> <p><a href="{{ url_for("image", filename="IMG_0001.jpg") }}">Image</a></p>
<p><a href="{{ url_for("random_image") }}">Random Image</a></p>
</body> </body>
</html> </html>