Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serve static files with Eve

Tags:

python

flask

eve

I am running Flask and Eve on localhost at a same time. The Flask app serves static files and makes requests to the Eve app to get some data. I want to run Eve only, without a separate Flask app. How can I serve static files with Eve?

like image 577
user3458284 Avatar asked Jan 06 '15 12:01

user3458284


3 Answers

A better approach will be to prefix the /api for all REST APIs. This can be done by adding URL_PREFIX="api" in settings.py.

By doing this whenever there is request to /, Eve(Flask) will not return the resource catalog instead returns the page as given in run.py.

To serve static content add route decorators accordingly in run.py,

@app.route('/')
def index():
    return app.send_static_file('index.html')

app.run(host="0.0.0.0", debug=True)
like image 66
Yogeswaran Avatar answered Nov 11 '22 13:11

Yogeswaran


Eve is a Flask application (a subclass) so as a general rule everything that works with Flask works with Eve too. You could register a blueprint, or add new routes.

Also see this answer for a link to a working example: Servicing html requests with Eve

like image 37
Nicola Iarocci Avatar answered Nov 11 '22 12:11

Nicola Iarocci


try set import_name arg for Eve:

app = Eve(import_name=__name__)

like image 1
jamlee Avatar answered Nov 11 '22 12:11

jamlee