librelist archives

« back to archive

How to send entire css folder to Jinja2?

How to send entire css folder to Jinja2?

From:
Juan Christian
Date:
2014-10-06 @ 23:12
I have a folder assets and there I have tons of css and js files, I want to
send them like:

{{ url_for('static', filename='dashboard/assets/css/*.css') }}

Is that possible?

Re: [flask] How to send entire css folder to Jinja2?

From:
Eric B
Date:
2014-10-06 @ 23:19
Maybe use something like Flask-Assets to combine them into 1 file and then
include that one file.

http://flask-assets.readthedocs.org/en/latest/
On Oct 6, 2014 7:16 PM, "Juan Christian" <juan0christian@gmail.com> wrote:

> I have a folder assets and there I have tons of css and js files, I want
> to send them like:
>
> {{ url_for('static', filename='dashboard/assets/css/*.css') }}
>
> Is that possible?
>

Re: [flask] How to send entire css folder to Jinja2?

From:
Juan Christian
Date:
2014-10-06 @ 23:33
But still I'd need to write them all, right? That's what I don't want. I
have more than 20 js files.

On Mon, Oct 6, 2014 at 8:19 PM, Eric B <neurosnap@gmail.com> wrote:

> Maybe use something like Flask-Assets to combine them into 1 file and then
> include that one file.
>
> http://flask-assets.readthedocs.org/en/latest/
> On Oct 6, 2014 7:16 PM, "Juan Christian" <juan0christian@gmail.com> wrote:
>
>> I have a folder assets and there I have tons of css and js files, I want
>> to send them like:
>>
>> {{ url_for('static', filename='dashboard/assets/css/*.css') }}
>>
>> Is that possible?
>>
>

Re: [flask] How to send entire css folder to Jinja2?

From:
Scott Lipsig
Date:
2014-10-07 @ 02:12
Yes, you would still need to write them all - and should, even if that 
once means creating a series of templates with inheritance - if only to 
support reuse. Nudge nudge, wink wink.

In the interest of being helpful, here is a nasty hack that will do the 
sort of work you are talking about without writing them all out. Use with 
discretion.

test.py:
*****
from glob    import glob
from os.path import join

from flask import Flask, render_template, url_for
app = Flask(__name__)

@app.route('/test')
def test():
    # Get every file under static
    static_files = glob(join('static', '*'))
    # Return template
    return render_template('basic.html', static_files=static_files)

if __name__ == '__main__':
    app.run(debug=True, port=8000)
*****
templates/basic.html:
*****
<!doctype html>
{% for static_file in static_files %}
<link href="{{ static_file }}" rel="stylesheet">
{% endfor %}

<title>Hello from Flask</title>
{% if name %}
  <h1>Hello {{ name }}!</h1>
{% else %}
  <h1>Hello World!</h1>
{% endif %}
*****

For your specific directory structure, something like the following 
modification should work:

static_files = glob(join('static', 'dashboard', 'assets', 'css', '*'))
static_files = [f for f in static_files for f in static_files if '.css' in f]

On Oct 6, 2014, at 4:33 PM, Juan Christian <juan0christian@gmail.com> wrote:

> But still I'd need to write them all, right? That's what I don't want. I
have more than 20 js files.
> 
> On Mon, Oct 6, 2014 at 8:19 PM, Eric B <neurosnap@gmail.com> wrote:
> Maybe use something like Flask-Assets to combine them into 1 file and 
then include that one file.
> 
> http://flask-assets.readthedocs.org/en/latest/
> 
> On Oct 6, 2014 7:16 PM, "Juan Christian" <juan0christian@gmail.com> wrote:
> I have a folder assets and there I have tons of css and js files, I want
to send them like:
> 
> {{ url_for('static', filename='dashboard/assets/css/*.css') }}
> 
> Is that possible?
> 

Re: [flask] How to send entire css folder to Jinja2?

From:
Matthias Urlichs
Date:
2014-10-07 @ 06:49
Hi,

Scott Lipsig:
> static_files = glob(join('static', 'dashboard', 'assets', 'css', '*'))
> static_files = [f for f in static_files for f in static_files if '.css' in f]
> 
That's one too many "for f in static_files", methinks.
Also "f.endswith('.css')" is a bit faster, and also safer than
"'.css' in f", as I'd expect a file named 'foo.css.old' to NOT be included.

-- 
-- Matthias Urlichs

Re: [flask] How to send entire css folder to Jinja2?

From:
Scott Lipsig
Date:
2014-10-07 @ 18:12
Matthias,

Quite right, and thanks for the catch.

S

On Oct 6, 2014, at 11:49 PM, Matthias Urlichs <matthias@urlichs.de> wrote:

> Hi,
> 
> Scott Lipsig:
>> static_files = glob(join('static', 'dashboard', 'assets', 'css', '*'))
>> static_files = [f for f in static_files for f in static_files if '.css' in f]
>> 
> That's one too many "for f in static_files", methinks.
> Also "f.endswith('.css')" is a bit faster, and also safer than
> "'.css' in f", as I'd expect a file named 'foo.css.old' to NOT be included.
> 
> -- 
> -- Matthias Urlichs