Single-Page Applications

Flask can be used to serve Single-Page Applications (SPA) by placing staticfiles produced by your frontend framework in a subfolder inside of yourproject. You will also need to create a catch-all endpoint that routes allrequests to your SPA.

The following example demonstrates how to serve an SPA along with an API:

  1. from flask import Flask, jsonify
  2.  
  3. app = Flask(__name__, static_folder='app')
  4.  
  5.  
  6. @app.route("/heartbeat")
  7. def heartbeat():
  8. return jsonify({"status": "healthy"})
  9.  
  10.  
  11. @app.route('/', defaults={'path': ''})
  12. @app.route('/<path:path>')
  13. def catch_all(path):
  14. return app.send_static_file("index.html")