The initial code doesn't work because you're returning a document, which PyMongo represents as a Python dictionary. Flask doesn't know what to do with it. (Note that find_one() returns a single document, and find() returns a Cursor.)
Your final code returns the "thing" it gets from GridFS.get_last_version(), which is a GridOut object for reading from a GridFS file.
GridOut is iterable: iterating a GridOut obtains chunks of byes. Flask does know how to turn an iterable into an HTTP response, so the second version of your code works.
The lesson is: when you want to interact with GridFS, use the GridFS class rather than find() or find_one().
Yes, the image consists of merged chunks
data from the fs.chunks
collection.
To include the image in an <img>
tag, something like this should work:
@route('/images/<filename>')
def image(filename):
fs = gridfs.GridFS(db)
gridout = fs.get_last_version(filename=filename)
response.content_type = 'image/jpeg'
return gridout
Then in your HTML, <img src="/images/filename.jpg">
.