Skip to content

Commit

Permalink
Example Flask integration
Browse files Browse the repository at this point in the history
I have appended an example of a Flask integration.
  • Loading branch information
mjmare committed Aug 29, 2014
1 parent c4820d0 commit e0f4a34
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion doc/integrating.rst
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,50 @@ is equivalent to this::

app = bower.publisher(bower.injector(my_wsgi_app))


Example Flask integration
-------------------------
Currently there is no Flask extension for BowerStatic integration, but
manual integration is not too hard. The following code is sufficient to get it working::

from flask import Flask, render_template, request
import bowerstatic
import os.path
app = Flask(__name__)
bower = bowerstatic.Bower()
components = bower.components('components', os.path.join(os.path.dirname(__file__), 'static/bower_components'))
@app.before_request
def include_js_css():
include = components.includer(request.environ)
include('jquery/dist/jquery.min.js')
include('bootstrap/dist/js/bootstrap.min.js')
include('bootstrap/dist/css/bootstrap.min.css')

# these do not work yet:
# include('dist/fonts/glyphicons-halflings-regular.eot')
# include('dist/fonts/glyphicons-halflings-regular.ttf')
# include('bootstrap/less/bootstrap.less')


@app.route('/')
def home():
return render_template('home.html')
@app.route('/welcome')
def welcome():
return render_template('welcome.html')
if __name__ == "__main__":
app.wsgi_app = bower.wrap(app.wsgi_app) # please note the wrapping of the wsgi app not the Flask app.
app.run(debug=True)


Due to the wsgi magic no changes to the Jinja templates are necessary.

Please note that BowerStatic does not support file extensions other than css and js. So, for example, font
files will generate errors. Therefore file inclusion by using the Bower main endpoint might not work for
some packages (like Bootstrap).

0 comments on commit e0f4a34

Please sign in to comment.