Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example Flask integration #17

Merged
merged 1 commit into from
Nov 19, 2014
Merged

Example Flask integration #17

merged 1 commit into from
Nov 19, 2014

Conversation

mjmare
Copy link
Contributor

@mjmare mjmare commented Aug 29, 2014

I have appended an example of a Flask integration as per your request.

I have appended an example of a Flask integration.
@webmaven
Copy link
Contributor

👍

1 similar comment
@deni64k
Copy link

deni64k commented Sep 3, 2014

👍

@delijati
Copy link

Actually this example it not working at all ive tried it with render_template and without. The script is not being included.

from flask import Flask, render_template
import bowerstatic
import os.path

app = Flask(__name__)
bower = bowerstatic.Bower()

components = bower.components(
    "components",
    os.path.join(os.path.dirname(__file__), "static", "bower"),
)

@app.route('/')
def home():
    from flask import request
    include = components.includer(request.environ)
    include('jquery/dist/jquery.js')
    return "Hello World!"  # render_template("foo.html")


if __name__ == "__main__":
    app.wsgi_app = bower.wrap(app.wsgi_app)
    app.run(debug=True)

Resulting html:

% curl http://127.0.0.1:5000/
Hello World!%

@faassen
Copy link
Owner

faassen commented Nov 18, 2014

Hm, I wonder what the cause of this is, do I understand you that the <script> tags are not included?

Could you check whether static/bower/jquery/dist/jquery.js exists?

Or perhaps it's the reason for the before_request story -- maybe it's otherwise somehow overwritten? Does it work with before_request?

@delijati
Copy link

Yes the <script> tag is not included.
The file exists:

% ls static/bower/jquery/dist/jquery.js   
static/bower/jquery/dist/jquery.js

Also the with before_request it is not working:

from flask import Flask
from flask import 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"),
)

@app.before_request
def include_js_css():
    include = components.includer(request.environ)
    include('jquery/dist/jquery.min.js')


@app.route('/')
def home():
    include = components.includer(request.environ)
    include('jquery/dist/jquery.js')
    return "Hello World!"


if __name__ == "__main__":
    app.wsgi_app = bower.wrap(app.wsgi_app)
    app.run(debug=True)

The .bowerrc:

% cat .bowerrc 
{
    "directory": "static/bower"
}

@delijati
Copy link

Ok figured it out if i add <head> tag to the output it works.

How is bowerstatic now served? From flask? Is there a dispatcher and how to activate it!?

from flask import Flask
from flask import 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"),
)


@app.route('/')
def home():
    include = components.includer(request.environ)
    include('jquery/dist/jquery.js')
    return "<head></head>Hello World!"


if __name__ == "__main__":
    app.wsgi_app = bower.wrap(app.wsgi_app)
    app.run(debug=True)

Output:

% curl http://127.0.0.1:5000/
<head><script type="text/javascript" src="/bowerstatic/components/jquery/2.1.1/dist/jquery.js"></script></head>Hello World!%

@faassen
Copy link
Owner

faassen commented Nov 18, 2014

Oh, glad you figured it out! Yes, it needs a <head> tag to insert things. I just created an issue to complain when there's no head: #37 as this is rather mysterious without a clear error message!

Bower publishes the /bowerstatic URLs using its own file-based publisher through a WSGI middleware. The bower.wrap() call takes care of this. Doesn't it complete loading the jquery?

Once I get positive feedback that this works I'll finally merge the pull request. I now have enough examples of Flask integration to know how to create a small example in the docs!

@delijati
Copy link

It works , after a bit of debugging, the path os.path.dirname(__file__) gave the relative path i've changed it to this [1]. Yes i think there should be printed warning if a file is not located or the path is relative.

[1]

from flask import Flask
from flask import request
import bowerstatic
import os.path

app = Flask(__name__)
bower = bowerstatic.Bower()

components = bower.components(
    "components",
    os.path.join(
        os.path.abspath(os.path.dirname(__file__)),
        "static", "bower"),
)

@app.route('/')
def home():
    include = components.includer(request.environ)
    include('jquery/dist/jquery.js')
    return "<head></head>Hello World!"

if __name__ == "__main__":
    app.wsgi_app = bower.wrap(app.wsgi_app)
    app.run(debug=True)

Output:

 % curl http://127.0.0.1:5000/bowerstatic/components/jquery/2.1.1/dist/jquery.js | head        
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0/*!
 * jQuery JavaScript Library v2.1.1
 * http://jquery.com/
 *
 * Includes Sizzle.js
 * http://sizzlejs.com/
 *
 * Copyright 2005, 2014 jQuery Foundation, Inc. and other contributors
 * Released under the MIT license
 * http://jquery.org/license
curl: (23) Failed writing body (305 != 16384)

@faassen faassen merged commit e0f4a34 into faassen:master Nov 19, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants