You need to create a route that connects to a send from directory invocation from Flask. So you basically create an image with a src
attribute to your Flask route that sends the image using send_from_directory
. That's to send from any arbitrary folder beyond the default static folder. When I send Matplotlib images I add a query string that is the file modification time for the image file. There's a Python library to get that from a path and you can see some usage below. There's a special meta mode for html to force a refresh each time an image is used but it didn't work for me for reasons I couldn't explain and I wound up using the mod time to bypass the browser cache.
First off I render a template with the following parameters...
# Acquire image modification times to use as query strings to bypass image caching.
modificationTime = os.path.getmtime( "%s/%s" % ( model.getImagesSubfolder( ) , imageFile ) )
return render_template( 'plotsPage.html' , fileName=imageFile , modTimes=modificationTime )
Then my plotsPage.html template file has a the following tag, which creates the necessary query string with the mod time just looked up on the server side...
<img src="{{ url_for( 'sendPlotImage' , filename=fileName , t=modTime ) }}">
Then the url I'm connecting to serves the actual image using the send_from_directory. I have a view model that gives me the subfolder beneath my app where my images can be retrieved on a user by user basis with session state and all that.
@flaskApp.route( '/Images/<filename>' , methods=['GET'] )
def sendPlotImages( filename ) :
# Image file not generated, send placeholder instead to denote file does not exist.
if not os.path.isfile( "%s/%s" % ( model.getImagesSubfolder( ) , filename ) ) :
return send_from_directory( "static" , placeHolderImage )
return send_from_directory( model.getImagesSubfolder( ) , filename )