Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong output in conjunction with stream_template #511

Open
svalo opened this issue Jul 25, 2023 · 0 comments
Open

Wrong output in conjunction with stream_template #511

svalo opened this issue Jul 25, 2023 · 0 comments

Comments

@svalo
Copy link

svalo commented Jul 25, 2023

When using flask-caching together with Flask's stream_template, JSON is produced rather than HTML.

Is this expected and am I missing something? I searched the docs but did not find mention of stream_template.

EDIT: It seems that render_template() returns an iterator which gets then retrieved as an array but upon retrieval it's not converted back

The issue is reproducible with:

  • SimpleCache
  • RedisCache
  • NullCache

To reproduce the issue:

Create a file app.py containing the following code

#! /usr/bin/env python3
from flask import Flask, render_template, stream_template
from flask_caching import Cache

config = {
    #"CACHE_TYPE": "RedisCache",  # Flask-Caching related configs
    #"CACHE_TYPE": "SimpleCache",  # Flask-Caching related configs
    "CACHE_TYPE": "NullCache",  # Flask-Caching related configs
    "CACHE_DEFAULT_TIMEOUT": 300,
    "CACHE_REDIS_URL" : "unix:///var/run/redis/redis.sock"
    }
app = Flask(__name__)
# tell Flask to use the above defined config
app.config.from_mapping(config)
cache = Cache(app)
                                                                                                                                                                    
@app.route("/")
@cache.cached()
def index():
    return render_template('index.html')

@app.route("/stream")
def stream():
    return stream_template('index.html')

@app.route("/stream-cached")
@cache.cached()
def stream_cached():
    return stream_template('index.html')

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

Create a file templates/index.html with the following content

<html>
    <body>
        <p>
           Hello World!
        </p>
    </body>
</html>

Run the app with python3 app.py

Expected behaviour is to obtain from all routes the following:

<html>
    <body>
        <p>
            Hello World!
        </p>
    </body>
</html>

What happens insetead is that both / and /stream produce expected output while /stram-cached produces the following:

[
  "<html>\n    <body>\n        <p>\n            Hello World!\n        </p>\n    </body>\n</html>"
]

Environment:

$ python --version
Python 3.11.3
$ pip freeze
blinker==1.6.2
cachelib==0.9.0
click==8.1.6
Flask==2.3.2
Flask-Caching==2.0.2
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.3
redis==4.6.0
Werkzeug==2.3.6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant