简体   繁体   中英

Flask - How to serve static files with Blueprint?

I can't serve a static file from a Blueprint using Flask. I know this has been asked many times but feel the instructions aren't clear. Also, I haven't been able to make it work. I have the following file structure:

  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 : This file just starts the service. It's like my main. Somebody told me it's a good practice.

from FlaskApp import app
app.run()

InnerInit : This file registers the blueprint and creates the app.

from flask import Flask, render_template
app = Flask(__name__)
from FlaskApp.modulo.main import map_module
app.register_blueprint(map_module)

Main.py : Here I expose the API services of this module.

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."

I can serve the index.html file and it actually displays many dynamic elements... but I can't access the R file with rpy2. I'm sure this has to do with the blueprints because before creating the blueprint (to make the program more modular) it worked.

This is what I get when I access ' 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

The problem is inside your call to R:

@map_module.route("/serviceData")
def serviceData():
r = robjects.r
r.source("R/TheFileIWant.R")
return "Ok."

Change to this:

r.source(map_module.root_path + "/R/TheFileIWant.R)

Also, move the R file into the folder under the blueprints. It's not a static file at all (the definition is "served as is by the web server").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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