Note that there are some explanatory texts on larger screens.

plurals
  1. POBlueprint-specific assets in Flask using Flask-Assets?
    primarykey
    data
    text
    <p>I'm pretty new to Flask, for what it's worth. I'm using blueprints to break up my code and am trying to use Flask-Assets for serving up asset links. For some reason, though, I can't get Flask-Assets to work within a blueprint.</p> <p>Code structure:</p> <pre><code>/modules /base __init__.py __init__.py /static # ... stuff /templates /layout.html </code></pre> <p>In /modules/base/__init__.py:</p> <pre><code>from flask import Blueprint, render_template, request from flask.ext.sqlalchemy import SQLAlchemy from flask.ext.assets import Environment, Bundle from flask import current_app as app default = Blueprint('base', __name__) assets = Environment(app) css = Bundle('css/bootstrap.min.css') assets.register('css_all', css) js = Bundle('js/jquery-1.7.2.min.js', 'js/bootstrap.min.js') assets.register('js_all', js) @default.route('/') def index(): return render_template('index/index.html') </code></pre> <p>And the base layout has this in it:</p> <pre><code>{% assets "css_all" %} &lt;link rel="stylesheet" href="{{ ASSET_URL }}" /&gt; {% endassets %} {% assets "js_all" %} &lt;script type="text/javascript" src="{{ ASSET_URL }}"&gt;&lt;/script&gt; {% endassets %} </code></pre> <p>Actually what I have in /modules/base/__init__.py right now doesn't even work, as it throws "RuntimeError: working outside of application context". If I move the assets code into the index function it does work, but it seems weird to have to repeat that code every time I want to add assets. Is there a better way to do this? Am I missing something?</p> <p><em><strong>Edit:</em></strong> Updated /modules/base/__init__.py per codecool's comment, but now it throws an "TemplateSyntaxError: Encountered unknown tag 'assets'" error. I think this one is because the assets object isn't getting passed through to the app.</p> <p><em><strong>Edit 2:</em></strong> So taking some advice from how SQLAlchemy does things, I got this to sort of work with the following:</p> <p>In my main __init__.py:</p> <pre><code>from flask import Flask from modules.base import default, assets as base_assets from flask.ext.assets import Environment, Bundle app = Flask(__name__) app.register_blueprint(default) base_assets.init_app(app) </code></pre> <p>In base/__init__.py: from flask import Blueprint, render_template, request from flask.ext.assets import Environment, Bundle from flask import current_app as app</p> <pre><code>default = Blueprint('base', __name__) assets = Environment(app) css = Bundle('css/bootstrap2.min.css') assets.register('css_all', css) js = Bundle('js/jquery-1.7.2.min.js', 'js/bootstrap.min.js') assets.register('js_all', js) </code></pre> <p>The trick here is creating assets in the blueprint, importing it into the base init, and then calling .init_app there. Where this breaks down, however, is in having two blueprints that do this. The last one called always wins out. Maybe it'd be possible to pull this out into some common module or something..</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload