从Flask运行NPM Build [英] Running NPM Build from Flask

查看:452
本文介绍了从Flask运行NPM Build的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个React前端,我想在我的python后端API相同的起源。我正在尝试使用Flask,但是我遇到了Flask没有找到我的静态文件的问题。



我的前端构建是使用 npm run build in saas_frontend



这里是我的文件结构看起来像:

 ├──main.py 
├──requirements.txt
├── run.sh
├──saas_backend
├──saas_frontend

烧瓶是从> main.py 运行,并在 saas_frontend / build 中为前端服务:

 ├──asset-manifest.json 
├──favicon.ico
├──index.html
├── manifest.json
├──service-worker.js
└──static
├──css
│├──main.096c9e23.css
│└ ──main.096c9e23.css.map
├──js
│├──main.8949f17a.js
│└── main.8949f17a.js.map
└──media
├──delta.56f5d855.csv
└──logo.e233ff84.png

我在 main.py 中声明了新模板路径和静态路径,如下所示:

  7 import os $ b $ 8从flask导入Flask,render_template 
9
10 template_dir = os.path .abspath('saas_frontend / build /')
11 static_dir = os.path.abspath('saas_frontend / build / static')
12 app = Flask(__ name__,static_path = static_dir,template_folder = template_dir)
13
14
15 @ app.route('/')
16 def index():
17 print template_dir
18 print static_dir
19 return render_template('index.html')

但是Flask仍然没有我服务我的静态文件,我得到这个作为输出....

  127.0.0.1  -   -  [21 / Jul / 2017 12:13:14]GET / HTTP / 1.1200  -  
127.0.0.1 - - [21 / Jul / 2017 12:13:14]GET /static/css/main.096c9e23.css HTTP / 1.1404 -
127.0.0.1 - - [21 / Jul / 2017 12:13:14]GET /static/js/main.8949f17a.js HTTP / 1.1404 -
127.0 .0.1 - - [21 / Jul / 2017 12:13:14]GET /static/css/main.096c9e23.css HTTP / 1.1404 -
127.0.0.1 - - [21 / Jul / 2017 12 :13:14]GET /static/js/main.8949f17a.js HTTP / 1.1404 -



<有什么我应该做的不同?如何解决错误信息中的路径正确的文件,但完全无法将其提供给浏览器?



谢谢!!


解决方案我解决了我的问题,通过使用符号链接到适当的文件和目录。

在我的构建脚本我将包括:
$ b $ pre $ lt; code> ln -s saas_frontend / build / static static
ln -s saas_frontend / build templates

现在一切正常!


I have a React front end that I would like to serve on the same origin as my python backend API. I'm trying to use Flask for this, but I'm running into an issue with Flask not finding my static files.

My front end build is generated with npm run build in saas_frontend

Here is what my file structure looks like:

    ├── main.py
    ├── requirements.txt
    ├── run.sh
    ├── saas_backend
    ├── saas_frontend

Flask is running from main.py and serving my front end in saas_frontend/build:

├── asset-manifest.json
├── favicon.ico
├── index.html
├── manifest.json
├── service-worker.js
└── static
    ├── css
    │   ├── main.096c9e23.css
    │   └── main.096c9e23.css.map
    ├── js
    │   ├── main.8949f17a.js
    │   └── main.8949f17a.js.map
    └── media
        ├── delta.56f5d855.csv
        └── logo.e233ff84.png

I declared the new template path and static path in main.py as follows:

  7 import os
  8 from flask import Flask, render_template
  9 
 10 template_dir = os.path.abspath('saas_frontend/build/')
 11 static_dir   = os.path.abspath('saas_frontend/build/static')
 12 app = Flask(__name__, static_path=static_dir, template_folder=template_dir)
 13 
 14 
 15 @app.route('/')
 16 def index():
 17     print template_dir
 18     print static_dir                                                                                                                                                                                    
 19     return render_template('index.html')

But Flask is still unable to serve my static files and I get this as the output....

127.0.0.1 - - [21/Jul/2017 12:13:14] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [21/Jul/2017 12:13:14] "GET /static/css/main.096c9e23.css HTTP/1.1" 404 -
127.0.0.1 - - [21/Jul/2017 12:13:14] "GET /static/js/main.8949f17a.js HTTP/1.1" 404 -
127.0.0.1 - - [21/Jul/2017 12:13:14] "GET /static/css/main.096c9e23.css HTTP/1.1" 404 -
127.0.0.1 - - [21/Jul/2017 12:13:14] "GET /static/js/main.8949f17a.js HTTP/1.1" 404 -

Is there anything I should be doing differently? How can Flask resolve the right file with path in the error message but be completely unable to serve it to the browser?

Thanks!!

解决方案

I fixed my issue by using symlinks to the appropriate files and directories.

In my build script I will include:

ln -s saas_frontend/build/static static
ln -s saas_frontend/build templates

Everything works as expected now!

这篇关于从Flask运行NPM Build的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆