-2

I am simply trying to display an image in html using flask. But I am unable to do so even though I've given the complete correct path of image.

I've followed this link

python flask display image on a html page

My samply.py is

import os
from flask import Flask, flash, request, redirect, url_for, render_template

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

@app.route('/show_image')
def show_index():
    full_filename = os.path.join(app.config['UPLOAD_FOLDER'], '2.jpg')
    return render_template("layout.html", user_image = full_filename)

if __name__ == "__main__":
    app.run(debug=True)

And my HTML code:

<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
</head>
<body>
    <img src="{{ user_image }}" alt="User Image">
</body>
</html>

I am getting output of

127.0.0.1 - - [09/Jun/2019 11:50:41] "GET /home/user/Documents/images/2.jpg HTTP/1.1" 404 -

When I follow this link /home/user/Documents/images/2.jpg in my vscode, it's displaying the correct image; which means there's noting wrong with the path.

user_3pij
  • 1,334
  • 11
  • 22

1 Answers1

0

Did you configure a static directory in your flask app? If so then that is taken as the root of your application, and the app does not provide access to other sections of your file system. In other words you don't want the "full" (or more commonly, absolute) path to the file, you want to make sure the file is in your static directory or a sub-directory and that the path is relative to there.

This question might help you.

Maus
  • 1,791
  • 1
  • 17
  • 28