From d5f6b4bfd7a5f6a5598376f7d4319ec41d7e0a4b Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Sat, 10 Nov 2018 18:33:27 +0100 Subject: [PATCH] Handle empty input folders --- slider.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/slider.py b/slider.py index 556b926..700ac90 100755 --- a/slider.py +++ b/slider.py @@ -26,15 +26,18 @@ def random(): @app.route("/random_image/") def random_image(): - last_modified_time, last_modified_file = max( - (f.stat().st_mtime, f) for f in imgdir.glob(img_glob) - ) + try: + last_modified_time, last_modified_file = max( + (f.stat().st_mtime, f) for f in imgdir.glob(img_glob) + ) - if time() - last_modified_time <= 60: - selected_image = last_modified_file.relative_to(imgdir) - else: - images = list(imgdir.glob(img_glob)) - selected_image = choice(images).relative_to(imgdir) + if time() - last_modified_time <= 60: + selected_image = last_modified_file.relative_to(imgdir) + else: + images = list(imgdir.glob(img_glob)) + selected_image = choice(images).relative_to(imgdir) + except ValueError: + return redirect(url_for("static", filename="clear.gif")) return redirect( url_for("image", filename=selected_image)