简体   繁体   English

如何在python flask static内部使用有角构建资源?

[英]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. 每次部署时,我都无法更改。

  1. Do not move the files manually, it will only complicate the deployment process and be the place of many issues in the future. 不要手动移动文件,这只会使部署过程复杂化,并且将来会成为很多问题的地方。
  2. Try an set your AngularJS output build directory to be the static folder of the Flask application. 尝试将AngularJS输出构建目录设置为Flask应用程序的静态文件夹。 Usually it is in the webpack configuration. 通常它在webpack配置中。 This will ensure that you are not managing the frontend build files manually and also ensure the relative paths in the JS and CSS files would all be preserved. 这将确保您不会手动管理前端构建文件,也将确保保留JS和CSS文件中的相对路径。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM