librelist archives

« back to archive

Does Flask have a pendant to django's collectstatic?

Does Flask have a pendant to django's collectstatic?

From:
Lars Holm Nielsen
Date:
2012-05-07 @ 11:10
Hi,

I'm coming from Django and have been playing a bit around with Flask and 
so far I like. There's one question however I haven't been able to find 
any answers to - perhaps its just not the way to do it in Flask, so just 
let me know if this is the case :-)

My setup includes ngnix proxying requests through to Gunicorn which is 
running my Flask app. Now I want to serve static content with ngnix (say 
everthing under /static/) and proxy everything else through to Gunicorn. 
However, I want to serve /static/ from /var/www which both will contain my
apps static files as well as non-version controlled files (say e.g. PDFs 
or images that you don't want under version control). In django you have a
command "collectstatic" which will take your apps static files and sync 
them over to your static root. Is there anything similar in Flask or is 
there another preferred method of serving these non-version controlled 
static files? 

I can naturally quickly develop a sync script myself, but I'd preferrer to
use the standard way of solving this problem with Flask. 

Thanks
Lars Holm Nielsen

Re: [flask] Does Flask have a pendant to django's collectstatic?

From:
Simon Sapin
Date:
2012-05-07 @ 12:07
Le 07/05/2012 13:10, Lars Holm Nielsen a écrit :
> Hi,
>
> I'm coming from Django and have been playing a bit around with Flask
> and so far I like. There's one question however I haven't been able
> to find any answers to - perhaps its just not the way to do it in
> Flask, so just let me know if this is the case:-)
>
> My setup includes ngnix proxying requests through to Gunicorn which
> is running my Flask app. Now I want to serve static content with
> ngnix (say everthing under/static/) and proxy everything else through
> to Gunicorn. However, I want to serve/static/  from /var/www which
> both will contain my apps static files as well as non-version
> controlled files (say e.g. PDFs or images that you don't want under
> version control). In django you have a command "collectstatic" which
> will take your apps static files and sync them over to your static
> root. Is there anything similar in Flask or is there another
> preferred method of serving these non-version controlled static
> files?
>
> I can naturally quickly develop a sync script myself, but I'd
> preferrer to use the standard way of solving this problem with
> Flask.


Hi,

I don’t really like how Django’s collectstatic works, but that’s a 
matter of taste.

What I usually do is have different URL prefixes for static files 
(version-controlled alongside the application) and media files (anything 
not version-controlled, usually uploaded by the application’s users. My 
habit of calling these "media" actually comes from Django.)

Then you can configure your web server to serve /static/ and /media/ 
from two different directories, and pass everything else to the application.

Now if you want the list of all static files (to copy or symlink them or 
something), you can use something like Frozen-Flask’s[1] 
static_files_urls method[2]. This particular method is internal and not 
documented, but it should be fairly stable. (I’m the author.)
walk_directory(app.static_folder) is a shorter version if you’re not 
using blueprints.

[1] http://packages.python.org/Frozen-Flask/
[2] 
https://github.com/SimonSapin/Frozen-Flask/blob/v0.9/flask_frozen/__init__.py#L355

Regards,
-- 
Simon Sapin