Flask template based javascript file

This is actually striaght forward, but appearently you have to deliver the .js file correctly towards your browser.

The simple way you can deliver your template rendered js file via:

    @app.route('/application.js', methods=['GET'])
        def s_application():
        flask.render_template('application.js_tpl')

Where application.js is located inside your template folder.

But this way it's not delivered as js type, as you can examine with your browsers developer tools. It states html.

So extend your function a little bit:

import io

@app.route('/application.js', methods=['GET'])
def s_application():
    content = flask.render_template('application.js_tpl').encode('utf-8')
    scriptfile = io.BytesIO(content)
    return flask.send_file(scriptfile, attachment_filename="application.js")

And your browser is happy ;)