为了简化设置,请考虑在构建过程中使用Angular
CLI将所有文件放置在分发目录中,即通过
outputPath在angular.json中指定。您可以
assets在构建期间使用angular.json部分移动您的Python文件。
angular.json
"your-project": { "root": "your-project-directory", "sourceRoot": "your-project-directory/src", "projectType": "application", "architect": { "build": { "builder": "@angular-devkit/build-angular:browser", "options": { "outputPath": "dist", "index": "your-project-directory/src/index.html", "main": "your-project-directory/src/main.ts", ... "assets": [ { "glob": "***", "input": "your-project-directory/src/python/", "output": "." }在
dist目录的顶层,将您
main.py的基本Flask设置与一起放置
index.html。注意 static_proxy
以确保提供支持文件。
main.py
from flask import Flask, send_from_directoryapp = Flask(__name__)@app.route('/<path:path>', methods=['GET'])def static_proxy(path): return send_from_directory('./', path)@app.route('/')def root(): return send_from_directory('./', 'index.html')if __name__ == '__main__': # This is used when running locally only. When deploying use a webserver process # such as Gunicorn to serve the app. app.run(host='127.0.0.1', port=8080, debug=True)@app.errorhandler(500)def server_error(e): return 'An internal error occurred [main.py] %s' % e, 500


