Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Flask Not Updating Images [duplicate]

There were a few Flask questions about images on here, but none that addressed my issue. I have an app that creates an image, saves it, then displays it. Once. It SHOULD do this multiple times (each time the image is changed it SHOULD load a new image). It does not; it only displays the first image associated with the filename it displayed. These files are no longer in the static folder, yet show up in the app.

Image is created, saved to a subfolder of the static folder via:

img.save('../source/static/images/characters/{}.png'
              .format(self.char.name), "PNG")

The app loads it thru:

'<img src="/static/images/characters/{}.png" '
            .format(self.name) +
            ' alt="{}" title="{}" '.format(self.name, self.name) +
            'width=100% />'

(which is added to the flask app thru code manipulations).

Issue is I have two test cases: One where text appears on the image (text being added was removed a few iterations of the code ago) and one where the alpha channel is in the wrong place (RGBA -> ARGB issues, resolved, except I can't see the new files on the site). As of now, only running/testing locally. The proper pictures appear in ~\source\static\images\characters. All *.py files are in source. What do I need to do in order to fix this?

I've tried:

@app.after_request
def add_header(response):
"""
Add headers to both force latest IE rendering engine or Chrome Frame,
and also to cache the rendered page for 10 minutes.
"""
response.headers['X-UA-Compatible'] = 'IE=Edge,chrome=1'
response.headers['Cache-Control'] = 'public, max-age=0'
return response

as a separate piece of code at the end of my app.py file, also tried adding:

app.config["CACHE_TYPE"] = "null"

To the beginning of the app.route() that calls the page that creates the image.

On a likely separate note, the image should be in a table, but appears to not be... What I mean is, there is a table with headers, then a table in one col, the image in the other... but the first column's text is below the image (though each in their proper places otherwise). Bizarre, but likely unrelated.

So, why does flask not update properly, and how do I force it to do so? (Problem persists when shutting the program down and reopening it).

UPDATE: This seemed to work...

Using Flask, how do I modify the Cache-Control header for ALL output?

Did it by adding:

app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 1

To the top (included the app = Flask part to help other newbs) and this at the end:

# No caching at all for API endpoints.
@app.after_request
def add_header(response):
    # response.cache_control.no_store = True
    if 'Cache-Control' not in response.headers:
        response.headers['Cache-Control'] = 'no-store'
    return response

But only worked for a single instance, others still had weird caching. Could be cached from before and the other one had just timed out, but that shouldn't happen since I keep closing the server (localhost), right?

like image 722
Darin LaSota Avatar asked Aug 09 '17 07:08

Darin LaSota


2 Answers

This seemed to work

Using Flask, how do I modify the Cache-Control header for ALL output?

Did it by adding:

app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0

To the top (included the app = Flask part to help other newbs) and this at the end:

# No caching at all for API endpoints.
@app.after_request
def add_header(response):
    # response.cache_control.no_store = True
    response.headers['Cache-Control'] = 'no-store, no-cache, must-
    revalidate, post-check=0, pre-check=0, max-age=0'
    response.headers['Pragma'] = 'no-cache'
    response.headers['Expires'] = '-1'
    return response

This works, but the log in my command prompt window is constantly sending information.

like image 52
Darin LaSota Avatar answered Sep 19 '22 11:09

Darin LaSota


try to change the image name so it seems it pretends to be a new image every time you regenerate it. You can use some time-based suffix of the file name. Or you can just change the url of the image adding some fake get parameters to it.

Reference: flask cache busting

flask cache busting library

like image 27
Radek Hofman Avatar answered Sep 22 '22 11:09

Radek Hofman