From 96993ac3d0dadc91b3e9125b6e5bab7107c38e5e Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Sat, 3 Nov 2018 14:00:06 +0100 Subject: [PATCH] Serve a random image from imgfolder on /random_image endpoint --- config.py | 2 +- slider.py | 10 +++++++++- templates/hello.html | 1 + 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/config.py b/config.py index 0b692c8..533ba28 100644 --- a/config.py +++ b/config.py @@ -1 +1 @@ -imgdir = "/home/mandlm/Foto Export/2018/09/2018-09-03" +imgdir = "." diff --git a/slider.py b/slider.py index a695783..62456c3 100755 --- a/slider.py +++ b/slider.py @@ -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/") def image(filename): return send_from_directory(config.imgdir, filename) diff --git a/templates/hello.html b/templates/hello.html index 1a0f266..49add83 100644 --- a/templates/hello.html +++ b/templates/hello.html @@ -3,5 +3,6 @@

Hello slider number {{ num }}!

Image

+

Random Image