我无法使用Flask从Blueprint提供静态文件。我知道这已被多次询问,但觉得说明不清楚。此外,我还没能使它工作。我有以下文件结构:
FlaskApp
├──__init__.py # Let's call it OuterInit.
└── FlaskApp
├──__init__.py # Let's call it InnerInit.
├── modulo
│ └── main.py #My blueprint
├── mongo
│ └── otherBlueprint... Still not developed.
├── __pycache__
├── resources
├── static
│ ├── public
│ │ └── SomePublicFiles...
│ └── R
| └── TheFileIWant.R #I want to serve this file
└── templates
└── index.html #I serve this template from my Blueprint. (it works)
OuterInit : 该文件只是启动服务。这就像我的主要。有人告诉我这是一个很好的做法。
from FlaskApp import app
app.run()
InnerInit :此文件注册蓝图并创建应用。
from flask import Flask, render_template
app = Flask(__name__)
from FlaskApp.modulo.main import map_module
app.register_blueprint(map_module)
Main.py :这里我公开了这个模块的API服务。
import os
otherImports...
static_folder = os.path.join(os.pardir, 'static')
map_module = Blueprint('map_module', __name__, static_folder=static_folder, static_url_path="/static")
@map_module.route("/map")
def map():
return render_template("index.html")
@map_module.route("/serviceData")
def serviceData():
r = robjects.r
r.source("R/TheFileIWant.R")
return "Ok."
我可以提供index.html文件,它实际上显示了许多动态元素......但是我无法使用rpy2访问R文件。我确信这与蓝图有关,因为在创建蓝图之前(为了使程序更加模块化)它起作用了。
这是我访问“http://localhost:5000/serviceData
”
(flaskEnv) python_user@Pipepc:~/Documents/FlaskApp$ python __init__.py
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
/home/python_user/Documents/flaskEnv/local/lib/python3.4/site-packages/rpy2/rinterface/__init__.py:186: RRuntimeWarning: Error in file(filename, "r", encoding = encoding) :
cannot open the connection
warnings.warn(x, RRuntimeWarning)
/home/python_user/Documents/flaskEnv/local/lib/python3.4/site-packages/rpy2/rinterface/__init__.py:186: RRuntimeWarning: In addition:
warnings.warn(x, RRuntimeWarning)
/home/python_user/Documents/flaskEnv/local/lib/python3.4/site-packages/rpy2/rinterface/__init__.py:186: RRuntimeWarning: Warning message:
warnings.warn(x, RRuntimeWarning)
/home/python_user/Documents/flaskEnv/local/lib/python3.4/site-packages/rpy2/rinterface/__init__.py:186: RRuntimeWarning: In file(filename, "r", encoding = encoding) :
warnings.warn(x, RRuntimeWarning)
/home/python_user/Documents/flaskEnv/local/lib/python3.4/site-packages/rpy2/rinterface/__init__.py:186: RRuntimeWarning:
warnings.warn(x, RRuntimeWarning)
/home/python_user/Documents/flaskEnv/local/lib/python3.4/site-packages/rpy2/rinterface/__init__.py:186: RRuntimeWarning: cannot open file 'R/rmethod.R': No such file or directory
warnings.warn(x, RRuntimeWarning)
[2017-03-24 01:36:28,320] ERROR in app: Exception on /serviceData [GET]
Traceback (most recent call last):
File "/home/python_user/Documents/flaskEnv/local/lib/python3.4/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/home/python_user/Documents/flaskEnv/local/lib/python3.4/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/python_user/Documents/flaskEnv/local/lib/python3.4/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/python_user/Documents/flaskEnv/local/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/python_user/Documents/flaskEnv/local/lib/python3.4/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/home/python_user/Documents/flaskEnv/local/lib/python3.4/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/python_user/Documents/FlaskApp/FlaskApp/modulo/main.py", line 19, in serviceData
r.source("R/rmethod.R")
File "/home/python_user/Documents/flaskEnv/local/lib/python3.4/site-packages/rpy2/robjects/functions.py", line 178, in __call__
return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
File "/home/python_user/Documents/flaskEnv/local/lib/python3.4/site-packages/rpy2/robjects/functions.py", line 106, in __call__
res = super(Function, self).__call__(*new_args, **new_kwargs)
rpy2.rinterface.RRuntimeError: Error in file(filename, "r", encoding = encoding) :
cannot open the connection
答案 0 :(得分:0)
问题出在你对R的调用中:
@map_module.route("/serviceData")
def serviceData():
r = robjects.r
r.source("R/TheFileIWant.R")
return "Ok."
更改为:
r.source(map_module.root_path + "/R/TheFileIWant.R)
另外,将R文件移动到蓝图下的文件夹中。它根本不是静态文件(定义“由Web服务器按原样提供”)。