Serve images from a separate direcory configured in an external config

file (config.py)
This commit is contained in:
Michael Mandl 2018-11-03 13:21:26 +01:00
parent d45d1347bd
commit f0fb5f0d9f
4 changed files with 11 additions and 1 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
__pycache__/

1
config.py Normal file
View file

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

View file

@ -1,8 +1,10 @@
#!/usr/bin/python3 #!/usr/bin/python3
from flask import Flask, render_template from flask import Flask, render_template, send_from_directory
from random import randint from random import randint
import config
app = Flask(__name__) app = Flask(__name__)
@ -11,5 +13,10 @@ def hello():
return render_template("hello.html", num=randint(1, 23)) return render_template("hello.html", num=randint(1, 23))
@app.route("/img/<path:filename>")
def image(filename):
return send_from_directory(config.imgdir, filename)
if __name__ == "__main__": if __name__ == "__main__":
app.run() app.run()

View file

@ -2,5 +2,6 @@
<body> <body>
<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>
</body> </body>
</html> </html>