Re: [flask] Application routes
- From:
- Alex
- Date:
- 2010-09-21 @ 07:46
On Tue, Sep 21, 2010 at 1:11 AM, Dan Jacob <danjac354@gmail.com> wrote:
>> My main motivation is not exactly getting rid of the circular import,
>> which works out when used properly, but to find a pattern in general
>> that does not require importing all the application upfront.
>
> OTOH this is an advantage of Flask; it's conceptually simple, in that
> you have an app instance you attach routes etc to. It's kind of
> "inside out" compared to e.g. Django where the setup is hidden.
>
>>
>> Also my plan is to find a nicer way to handle static files which are
>> currently automagically exported based on the presence of a static
>> folder which was a huge mistake in the hindsight :(
>>
>
> What would be useful are "named" static handlers that you can attach
> to your app. For example, I have this:
>
> app = Flask(__name__)
> app.add_static_handler('images', '/images/', path_to_images)
>
> In your template:
>
> <img src="{{ url_for('images', filename="icon.jpg") }}">
>
> This is fine for development, but in production you are serving your
> images in nginx or a completely different domain such as S3. You can
> change the URL and/or path in configuration, but still use the
> 'images' handler name.
+1 for static handler. It looks really clean and straightforward.
Alex
Re: [flask] Application routes
- From:
- Armin Ronacher
- Date:
- 2010-09-20 @ 23:26
Hi,
On 9/21/10 1:11 AM, Dan Jacob wrote:
> What would be useful are "named" static handlers that you can attach
> to your app. For example, I have this:
>
> app = Flask(__name__)
> app.add_static_handler('images', '/images/', path_to_images)
That kinda already works:
from flask import send_from_directory
app.add_url_rule('/images/<path:filename>', 'images',
lambda request, filename:
send_from_directory(path_to_images, filename))
Regards,
Armin
Re: [flask] Application routes
- From:
- Dan Jacob
- Date:
- 2010-09-21 @ 07:14
Can I use a completely different domain with add_url_rule ? If so,
then yes, that fixes the problem. For example, I might want to switch
all my static URLs to an Amazon S3 account later on; I don't want to
go through all my templates and change the URLs.
On 21 September 2010 00:26, Armin Ronacher <armin.ronacher@active-4.com> wrote:
> Hi,
>
> On 9/21/10 1:11 AM, Dan Jacob wrote:
>> What would be useful are "named" static handlers that you can attach
>> to your app. For example, I have this:
>>
>> app = Flask(__name__)
>> app.add_static_handler('images', '/images/', path_to_images)
>
> That kinda already works:
>
> from flask import send_from_directory
> app.add_url_rule('/images/<path:filename>', 'images',
> lambda request, filename:
> send_from_directory(path_to_images, filename))
>
>
> Regards,
> Armin
>
Re: [flask] Application routes
- From:
- Thadeus Burgess
- Date:
- 2010-09-21 @ 16:04
Another +1 for by domain for this and for @route.
--
Thadeus
On Tue, Sep 21, 2010 at 2:14 AM, Dan Jacob <danjac354@gmail.com> wrote:
> Can I use a completely different domain with add_url_rule ? If so,
> then yes, that fixes the problem. For example, I might want to switch
> all my static URLs to an Amazon S3 account later on; I don't want to
> go through all my templates and change the URLs.
>
> On 21 September 2010 00:26, Armin Ronacher <armin.ronacher@active-4.com> wrote:
>> Hi,
>>
>> On 9/21/10 1:11 AM, Dan Jacob wrote:
>>> What would be useful are "named" static handlers that you can attach
>>> to your app. For example, I have this:
>>>
>>> app = Flask(__name__)
>>> app.add_static_handler('images', '/images/', path_to_images)
>>
>> That kinda already works:
>>
>> from flask import send_from_directory
>> app.add_url_rule('/images/<path:filename>', 'images',
>> lambda request, filename:
>> send_from_directory(path_to_images, filename))
>>
>>
>> Regards,
>> Armin
>>
>
Re: [flask] Application routes
- From:
- Dejan Noveski
- Date:
- 2010-09-20 @ 23:20
I like the static handler idea. It's simple, you can either add 1 static
handler for e.g. /static folder or add several handlers for /static/js
/static/css etc.
On Tue, Sep 21, 2010 at 1:11 AM, Dan Jacob <danjac354@gmail.com> wrote:
> > My main motivation is not exactly getting rid of the circular import,
> > which works out when used properly, but to find a pattern in general
> > that does not require importing all the application upfront.
>
> OTOH this is an advantage of Flask; it's conceptually simple, in that
> you have an app instance you attach routes etc to. It's kind of
> "inside out" compared to e.g. Django where the setup is hidden.
>
> >
> > Also my plan is to find a nicer way to handle static files which are
> > currently automagically exported based on the presence of a static
> > folder which was a huge mistake in the hindsight :(
> >
>
> What would be useful are "named" static handlers that you can attach
> to your app. For example, I have this:
>
> app = Flask(__name__)
> app.add_static_handler('images', '/images/', path_to_images)
>
> In your template:
>
> <img src="{{ url_for('images', filename="icon.jpg") }}">
>
> This is fine for development, but in production you are serving your
> images in nginx or a completely different domain such as S3. You can
> change the URL and/or path in configuration, but still use the
> 'images' handler name.
>
--
--
Dejan Noveski
Web Developer
dr.mote@gmail.com
Twitter: http://twitter.com/dekomote | LinkedIn:
http://mk.linkedin.com/in/dejannoveski
Atomidata http://www.atomidata.com