[英]How to use angular build assets inside python flask static?
I am working on application which uses angular7 in the front end and python flask framework in the back end. 我正在研究在前端使用angular7并在后端使用python flask框架的应用程序。 I m trying to take the angular build ie, dist folder and configuring in flask as per the flask structure. 我正在尝试采用角度构建,即dist文件夹,并根据烧瓶结构在烧瓶中进行配置。
I am not able to load the images in this server. 我无法在该服务器上加载图像。 where images path in the dist is like dist/assets/images
. dist中的图像路径类似于dist/assets/images
。
flask
---app.py
---templates
----index.html
---static
---- main6gaugsx.js
---- runtime7gyg.js
--conf files
I tried to move the these images inside the static folder of flask. 我试图将这些图像移动到flask的静态文件夹中。 But I cannot set the path of images in main6gaugsx.js
, since it is bundled code. 但是我无法在main6gaugsx.js
设置图像的路径,因为它是捆绑的代码。 Also images may increase later point of time. 此外,图像可能会在以后增加时间点。 I cant do this changes every time of deployment. 每次部署时,我都无法更改。
But the issue would be index.html
will also be placed in the static
folder and not in the templates folder. 但是问题是index.html
也将放置在static
文件夹中,而不是模板文件夹中。 To deal with that, set your template_folder
during Flask app
creation to static/build
or wherever the index.html
is being placed in the static
folder. 为了解决这个问题,在Flask app
创建过程中将template_folder
设置为static/build
或将index.html
放置在static
文件夹中的任何位置。
# assuming, the angular build is in the statis/build directory
app = Flask(__name__, static_folder='static', template_folder='static/build`)
I changed angular build outputPath to point to static folder. 我将角度构建outputPath更改为指向静态文件夹。
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "my-project/dist/static",// changes
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/assets",
"src/favicon.ico"
],
},
.
.
},
moved build static folder inside service file and updated the static_folder and template_folder path in main.py as below. 将构建静态文件夹移动到服务文件中,并如下更新main.py中的static_folder和template_folder路径。
cp -r angular/my-project/dist/static service/app
in main.py. 在main.py中。
app = Flask(__name__, static_folder='static', template_folder='static')
@app.route('/')
def home():
return render_template('index.html')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.