Re: [flask] Serving static html directories via flask ?
- From:
- Armin Ronacher
- Date:
- 2011-09-26 @ 09:54
Hi,
On 2011-09-26 8:56 AM, Nicolas Steinmetz wrote:
> I set up a flask app at the root of my site and as a side note, I can't
> change apache conf (exept via htaccess) and since then, all directories
> in which I have static html pages are no longer available.
mod_rewrite can still help:
RewriteEngine ON
RewriteRule ^static/(.*)$ /path/to/static/files
If I remember correctly.
> Is there a way through Flask to server these pages ?
send_from_directory() does exactly that:
@app.route('/foo/<path:filename>')
def send_foo(filename):
return send_from_directory('/path/to/static/files', filename)
Keep in mind however that Apache is a few gazillion times faster sending
that than Flask :)
Regards,
Armin
Re: [flask] Serving static html directories via flask ?
- From:
- Nicolas Steinmetz
- Date:
- 2011-09-26 @ 12:45
Hi,
2011/9/26 Armin Ronacher <armin.ronacher@active-4.com>
> mod_rewrite can still help:
>
> RewriteEngine ON
> RewriteRule ^static/(.*)$ /path/to/static/files
>
> If I remember correctly.
>
Following code did the job :
RewriteEngine On
RewriteRule ^/journal/(.*) - [L]
RewriteRule ^/tutoriels/(.*) - [L]
> Is there a way through Flask to server these pages ?
> send_from_directory() does exactly that:
>
> @app.route('/foo/<path:filename>')
> def send_foo(filename):
> return send_from_directory('/path/to/static/files', filename)
>
my bad, I tried yesterday with a relative path... it works also :-)
> Keep in mind however that Apache is a few gazillion times faster sending
> that than Flask :)
>
Yep, I know even if it's not an issue for the current project :)
Thanks all for your answers,
Nicolas
--
Nicolas Steinmetz
http://www.steinmetz.fr - http://nicolas.steinmetz.fr/
Re: [flask] Serving static html directories via flask ?
- From:
- Armin Ronacher
- Date:
- 2011-09-26 @ 12:57
Hi,
On 9/26/11 2:45 PM, Nicolas Steinmetz wrote:
> my bad, I tried yesterday with a relative path... it works also :-)
Relative paths to send_from_directory are relative to the application
root folder.
> Yep, I know even if it's not an issue for the current project :)
And we acknowledge this usecase exists which is why this is a public API
and not hidden away from you. Our stance was always that not everything
that appears stupid is stupid in every situation :)
That said, I will still always bring that disclaimer :-)
Regards,
Armin
Re: [flask] Serving static html directories via flask ?
- From:
- Simon Sapin
- Date:
- 2011-09-26 @ 07:45
Le 26/09/2011 08:56, Nicolas Steinmetz a écrit :
>
> Is there a way through Flask to server these pages ? I tried to use the
> send_file() or send_from_directory() but without a lof of success. As
> depth of the static html pages directory is quite long, I would like to
> have a generic solution so that flask just serve the files, wherever
> they are (I would like to declare only the 1st level directories).
Hi,
Depending on how Flask is setup for you, it might be possible to have
some static files served by Apache even with only .htacces.
Otherwise, can you elaborate? What code do you use? What exactly is the
problem?
From your talking of directory depth, I can only guess that you may
need to use the "path" converter for your URL variable:
http://werkzeug.pocoo.org/docs/routing/#werkzeug.routing.PathConverter
Regards,
--
Simon Sapin
Re: [flask] Serving static html directories via flask ?
- From:
- Alex Morega
- Date:
- 2011-09-26 @ 07:02
On Sep 26, 2011, at 9:56 AM, Nicolas Steinmetz wrote:
> I set up a flask app at the root of my site and as a side note, I can't
change apache conf (exept via htaccess) and since then, all directories in
which I have static html pages are no longer available.
>
> Is there a way through Flask to server these pages ? I tried to use the
send_file() or send_from_directory() but without a lof of success. As
depth of the static html pages directory is quite long, I would like to
have a generic solution so that flask just serve the files, wherever they
are (I would like to declare only the 1st level directories).
>
> Is it possible (even if not it's not state of the art or performance killing) ?
Yes, there is a middleware in Werkzeug that serves static files as you
describe. It will look for a file on disk and fall back to the wrapped
application. There is a nice example in the documentation:
http://werkzeug.pocoo.org/docs/middlewares/#werkzeug.wsgi.SharedDataMiddleware
Cheers,
-- Alex