2

I have referred the link here: More than one static path in local Flask instance and tried getting to display the image files in the browser tab, but couldn't do so. Unable to make out the mistake. stored_name is the physical filename of the file stored in the path specified by the app config constant variable: 'UPLOAD_FOLDER'. This folder is NOT in the static folder and its sub-folder path. I would like to increase storage capacity of files (maybe image files also), later by adding harddisk space, which is difficult if I use the static folder for storage. I don't want to override the static folder during app initialisation(which works)

Code snippets:

from flask import send_from_directory

@app.route('/api/v1.0/docs/<path:filename>', methods=["GET"])
def img_render(filename):
    print 'Called here'
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)


@app.route('/api/v1.0/docs/<int:doc_id>',methods=["GET"])
def image_rendering(doc_id):
    s = select([docs]).where(docs.c.id==doc_id)
    rs = conn.execute(s).fetchone()
    filename = str(rs[docs.c.stored_name])

    return url_for('img_render', filename=filename)

Html template:

<!DOCTYPE html>
<html>
  <body>
    <img src="{{url_for('img_render', filename=filename) }}" />
  </body>
</html>

The template is in the templates folder itself. If I override the original static folder during init of the application, I am able to get the images displayed in the browser, but I have to change my reference to the static folder for the application OR I will have to upload images into the static folder from the application, which I don't want to do. What is the mistake that it just displays the path of the image in the browser example:

/api/v1.0/docs/1_20160707_121214.jpg

and not the image itself? Where is the mistake in the code? Using flask v .0.11, linux 2.7.

Community
  • 1
  • 1
user956424
  • 1,611
  • 2
  • 37
  • 67

1 Answers1

1

Your image_rendering method is returning a string (which is what you see in the browser) when it should be returning the result of a template rendering.

Replace 'mytemplate.html' with the name of your html template in the following sample.

from flask import render_template

@app.route('/api/v1.0/docs/<int:doc_id>',methods=["GET"])
def image_rendering(doc_id):
    s = select([docs]).where(docs.c.id==doc_id)
    rs = conn.execute(s).fetchone()
    filename = str(rs[docs.c.stored_name])

    return render_template('mytemplate.html', filename=filename)

Here is a simple Flask App that works in Windows. In my d:\temp folder there is a file test.jpg. Make sure your Flask App has the correct permissions to read the files in the upload folder. To use browse to http://127.0.0.1:5000/api/v1.0/docs/99:

from flask import Flask, send_from_directory, render_template

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'd:/temp'


@app.route('/')
def hello_world():
    return 'Hello World!'


@app.route('/api/v1.0/docs/<path:filename>', methods=["GET"])
def img_render(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename=filename, mimetype='image/jpeg')


@app.route('/api/v1.0/docs/<int:doc_id>',methods=["GET"])
def image_rendering(doc_id):
    #  ignore doc_id, we'll use a constant filename
    filename = 'test.jpg'
    return render_template('test.html', filename=filename)


if __name__ == '__main__':
    app.run()

test.html in the templates folder:

<!DOCTYPE html>
<html>
  <body>
    <img src="{{url_for('img_render', filename=filename) }}" />
  </body>
</html>
pjcunningham
  • 7,676
  • 1
  • 36
  • 49
  • I have tried the above also. It just displays a thumbnail, but not the actual image. – user956424 Apr 27 '17 at 02:52
  • In your `img_render` function instead of printing 'Called here' print the filename passed into the function i.e. `print filename` . Is this the expected name of the image ? – pjcunningham Apr 27 '17 at 09:20
  • In the `img_render` function also print out the upload folder path i.e. `print app.config['UPLOAD_FOLDER']` - is that correct ? – pjcunningham Apr 27 '17 at 09:53
  • Yes,app.config['UPLOAD_FOLDER'] is correct; but in console what I get is "GET /1_20160707_174342.jpg HTTP/1.1, 404 – user956424 Apr 27 '17 at 09:56
  • What is the actual value of `app.config['UPLOAD_FOLDER']` – pjcunningham Apr 27 '17 at 09:59
  • The physical full path where the actual image file resides; as mentioned this is not part of the static folder path. app.config['UPLOAD_FOLDER'] value is : '/opt/abc/images' – user956424 Apr 27 '17 at 10:05
  • I think I can see what's happening - get back to your shortly. – pjcunningham Apr 27 '17 at 10:15
  • Thx a lot. Actually the path was created dynamically, hence there was problem in rendering the image. Works great now. – user956424 Apr 29 '17 at 07:33