Python(Flask)服务于角度项目索引.html金融机构

2024-06-16 15:01:33 发布

您现在位置:Python中文网/ 问答频道 /正文

有人知道如何使用Flask来提供一个有角度的单页应用程序吗?在

我无法提供默认路由“/”,它应该加载索引.html以及相关组件。这是我的烧瓶功能:

@app.route('/')
def hello_world():
    return send_file('templates/dist/templates/index.html')

当我访问本地主机:5000,我看到一个空的Chrome浏览器窗口:

Empty Chrome window with Angular / Flask

Chrome的开发控制台中存在以下错误:

enter image description here

以下是Chrome中应该显示的内容:

enter image description here

我认为错误是因为Flask不知道从哪里找到支持文件来渲染角度组件。根据Angular的快速入门教程,我的索引.html大部分为空,其中app root作为HTML body元素的占位符:

^{pr2}$

有人知道如何告诉烧瓶让棱角渲染页面吗?在


Tags: 功能app应用程序flask路由烧瓶defhtml
2条回答

为了简化设置,考虑在构建过程中使用Angular CLI将所有文件放在分发目录中,即通过在角度.json. 你可以使用角度.jsonassets部分在构建期间移动Python文件。在

在角度.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/assets/",
          "output": "assets"
        },
        {
          "glob": "**/*",
          "input": "your-project-directory/src/python/",
          "output": "."
        }



把你的cd4{cd4{3>和你的基本目录放在一起。注意静态代理以确保支持文件得到服务。在

在主.py在

^{pr2}$

如烧瓶docs中所述:

Just create a folder called static in your package or next to your module and it will be available at /static on the application.

因此,如果在以下情况下使用模板:

templates/dist/templates/index.html
static/ # set static folder there

或者,根据应用程序的排序方式:

^{pr2}$

看看他们如何在文档中对应用程序进行排序:

/application.py
/templates
    /hello.html

或者如果您使用模块文件夹:

/application
    /__init__.py
    /templates
        /hello.html

相关问题 更多 >