Is template rendering and forms in Flask used with SPA frontend?

Question:

let’s assume I have created SPA style frontend (React). How should I host it with a Flask app? Should i return json data from flask app? Are Flask templates and forms still used (e.g. FLask-WTF) with SPA? Or is Flask backend currently created only based on jwt tokens (login system…)?
What is the common approach nowadays?

I would like some examples of how to do this.

Asked By: MartinAg

||

Answers:

Flask isn’t really relevant if you just have it "return JSON". This just makes it a backend API, which you need browser AJAX/fetch calls to get data back and render it from the browser. That could be hosted in any framework/language.

Otherwise, you can do server-side rendering from Jinja2 tempaltes that include div DOM elements, which then are populated dynamically on the client-side by React. However, you’ll find you get better performance by serving the React bundle from a static web server such as Nginx, then put Flask instance(s) behind a load-balancer.

JWT is not really relevant either, but sure, if you have some login form, or authentication mechanism, then you can use them.

Answered By: OneCricketeer
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.