Hiero
Hiero

Reputation: 2205

Python Eve serving static files from a given folder

I build a simple REST api with Eve and Python.

from flask import redirect, send_from_directory, render_template
from eve import Eve
import os


PWD = os.environ.get('PWD')
public = os.path.join(PWD, 'public')


app = Eve(static_folder=public)

# serve index.html
@app.route('/')
def index():
    return send_from_directory(public, 'index.html')

# start the app
if __name__ == '__main__':
    app.run()

Where I just serve a static HTML files from /public folder when / is requested.

I'm using bower to install bootstrap:

<link rel="stylesheet" type="text/css" href="bower_components/bootstrap/dist/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="bower_components/bootstrap/dist/css/bootstrap-theme.css">  

But the problem is that the files are not being found although the path is correct.

Can someone explain me why is this happening?

Upvotes: 2

Views: 1126

Answers (3)

Georg Boegerl
Georg Boegerl

Reputation: 1

I found in another thread a question regarding templates and it works quite well for static files, too:

template_folder = os.path.join(os.path.dirname(__file__), 'templates')
static_folder = os.path.join(os.path.dirname(__file__), 'static')
app = Eve(settings=settings, static_url_path='/static', template_folder=template_folder, static_folder=static_folder)

Upvotes: 0

Patrick Collins
Patrick Collins

Reputation: 4334

It took me ages to figure this out. You have to import send_from_directory explicitly and also use __name__

from eve import Eve
from flask import send_from_directory

app = Eve(__name__, static_folder="static")

@app.route('/')
def index():
    return send_from_directory("static", 'index.html')

app.run()

Then in settings.py:

URL_PREFIX="api"

Static file delivery:

http://127.0.0.1:5000/ 
http://127.0.0.1:5000/static/images/image.jpg
http://127.0.0.1:5000/static/css/style.css

API calls:

http://127.0.0.1:5000/api/person/

Upvotes: 5

Hiero
Hiero

Reputation: 2205

Apparently the path for my bower_components should be /public/bower_components/../../

Upvotes: 0

Related Questions