JavaScript raises SyntaxError with data rendered in Jinja template

Flask’s Jinja environment automatically escapes data rendered in HTML templates. This is to avoid security issues if the dev tries to render untrusted user input. Since you are passing a Python object to be treated as JSON, Flask provides the tojson filter which automatically dumps the data to JSON and marks it safe. return render_template(‘tree.html’, … Read more

Deploying a minimal flask app in docker – server connection issues

The problem is you are only binding to the localhost interface, you should be binding to 0.0.0.0 if you want the container to be accessible from outside. If you change: if __name__ == ‘__main__’: app.run() to if __name__ == ‘__main__’: app.run(host=”0.0.0.0″) It should work. Note that this will bind to all interfaces on the host, … Read more

Link to Flask static files with url_for

You have by default the static endpoint for static files. Also Flask application has the following arguments: static_url_path: can be used to specify a different path for the static files on the web. Defaults to the name of the static_folder folder. static_folder: the folder with static files that should be served at static_url_path. Defaults to … Read more

How to get POSTed JSON in Flask?

First of all, the .json attribute is a property that delegates to the request.get_json() method, which documents why you see None here. You need to set the request content type to application/json for the .json property and .get_json() method (with no arguments) to work as either will produce None otherwise. See the Flask Request documentation: … Read more