librelist archives

« back to archive

Static files & subdomains

Static files & subdomains

From:
Honza Javorek
Date:
2011-08-25 @ 08:43
Hello,

I am trying to create a simple application with support for dynamic
subdomains. Let's say, every user will have it's own subdomain, so if
his username is "tomcruise", I'd like to route all my application
after login to "tomcruise.example.com". However, there are also some
public static pages, perfectly suitable for classic "www" subdomain.
So homepage and so on should be accessible at "www.example.com".

I set SERVER_NAME to "example.com" and i use decorators like
@app.route('/', subdomain='www') for home or @app.route('/blabla/',
subdomain='<username>') for others, which seems to solve all this...
except of static files. I have no idea how to handle URLs to these.
I'd like to have them at one place. For now "www.example.com/static"
would be perfect, but in future we'll maybe move it somewhere else
(another subdomain, Amazon, whatever). How can I link to them?

{{ url_for('static', filename='css/base.css') }}

...routes to http://example.com/css/base.css. Setting static_url_path
to Flask() app doesn't seem to be helpful in any way, only adjusts the
part between example.com/ and /css/base.css. There is no argument to
pass to url_for in template to suggest a subdomain or something.

Any ideas? Thanks!
Honza

Re: [flask] Static files & subdomains

From:
Simon Sapin
Date:
2011-08-25 @ 09:08
 On Thu, 25 Aug 2011 10:43:54 +0200, Honza Javorek wrote:
> Hello,
>
> I am trying to create a simple application with support for dynamic
> subdomains. Let's say, every user will have it's own subdomain, so if
> his username is "tomcruise", I'd like to route all my application
> after login to "tomcruise.example.com". However, there are also some
> public static pages, perfectly suitable for classic "www" subdomain.
> So homepage and so on should be accessible at "www.example.com".
>
> I set SERVER_NAME to "example.com" and i use decorators like
> @app.route('/', subdomain='www') for home or @app.route('/blabla/',
> subdomain='<username>') for others, which seems to solve all this...
> except of static files. I have no idea how to handle URLs to these.
> I'd like to have them at one place. For now "www.example.com/static"
> would be perfect, but in future we'll maybe move it somewhere else
> (another subdomain, Amazon, whatever). How can I link to them?
>
> {{ url_for('static', filename='css/base.css') }}
>
> ...routes to http://example.com/css/base.css. Setting static_url_path
> to Flask() app doesn't seem to be helpful in any way, only adjusts 
> the
> part between example.com/ and /css/base.css. There is no argument to
> pass to url_for in template to suggest a subdomain or something.
>
> Any ideas? Thanks!
> Honza

 Hi,

 This is the relevant code in Flask:

 https://github.com/mitsuhiko/flask/blob/master/flask/app.py#L430

 The following code will get you the same thing:

     app = Flask(__name__, static_folder=None)  # Disable static
     app.static_folder = 'static'  # Enable is back, but the URL rule is 
 still not created
     app.add_url_rule('/static/<path:filename>',
                      endpoint='static',
                      view_func=app.send_static_file)

 ... but this time you get full control on the URL rule creation. See 
 the documentation:

 http://flask.pocoo.org/docs/api/#flask.Flask.add_url_rule
 http://werkzeug.pocoo.org/docs/routing/#werkzeug.routing.Rule

Re: [flask] Static files & subdomains

From:
Honza Javorek
Date:
2011-08-25 @ 11:48
Thank you! Works great :-)

Regards,
Honza


On Thu, Aug 25, 2011 at 11:08, Simon Sapin <simon.sapin@exyr.org> wrote:
>  On Thu, 25 Aug 2011 10:43:54 +0200, Honza Javorek wrote:
>> Hello,
>>
>> I am trying to create a simple application with support for dynamic
>> subdomains. Let's say, every user will have it's own subdomain, so if
>> his username is "tomcruise", I'd like to route all my application
>> after login to "tomcruise.example.com". However, there are also some
>> public static pages, perfectly suitable for classic "www" subdomain.
>> So homepage and so on should be accessible at "www.example.com".
>>
>> I set SERVER_NAME to "example.com" and i use decorators like
>> @app.route('/', subdomain='www') for home or @app.route('/blabla/',
>> subdomain='<username>') for others, which seems to solve all this...
>> except of static files. I have no idea how to handle URLs to these.
>> I'd like to have them at one place. For now "www.example.com/static"
>> would be perfect, but in future we'll maybe move it somewhere else
>> (another subdomain, Amazon, whatever). How can I link to them?
>>
>> {{ url_for('static', filename='css/base.css') }}
>>
>> ...routes to http://example.com/css/base.css. Setting static_url_path
>> to Flask() app doesn't seem to be helpful in any way, only adjusts
>> the
>> part between example.com/ and /css/base.css. There is no argument to
>> pass to url_for in template to suggest a subdomain or something.
>>
>> Any ideas? Thanks!
>> Honza
>
>  Hi,
>
>  This is the relevant code in Flask:
>
>  https://github.com/mitsuhiko/flask/blob/master/flask/app.py#L430
>
>  The following code will get you the same thing:
>
>     app = Flask(__name__, static_folder=None)  # Disable static
>     app.static_folder = 'static'  # Enable is back, but the URL rule is
>  still not created
>     app.add_url_rule('/static/<path:filename>',
>                      endpoint='static',
>                      view_func=app.send_static_file)
>
>  ... but this time you get full control on the URL rule creation. See
>  the documentation:
>
>  http://flask.pocoo.org/docs/api/#flask.Flask.add_url_rule
>  http://werkzeug.pocoo.org/docs/routing/#werkzeug.routing.Rule
>
>

Re: [flask] Static files & subdomains

From:
Michael Fogleman
Date:
2011-08-25 @ 13:10
This is how I handle static content on a subdomain. It also makes it
less verbose in the templates.

import urlparse

app.config['STATIC_ROOT'] = 'http://static.example.com/'

def static(path):
    root = app.config.get('STATIC_ROOT', None)
    if root is None: # fallback on the normal way
        return url_for('static', filename=path)
    return urlparse.urljoin(root, path)

@app.context_processor
def inject_static():
    return dict(static=static)

Then in the templates you can just do this, which is nice:

{{ static('example.png') }}

Michael



On Thu, Aug 25, 2011 at 7:48 AM, Honza Javorek <honza@javorek.net> wrote:
> Thank you! Works great :-)
>
> Regards,
> Honza
>
>
> On Thu, Aug 25, 2011 at 11:08, Simon Sapin <simon.sapin@exyr.org> wrote:
>>  On Thu, 25 Aug 2011 10:43:54 +0200, Honza Javorek wrote:
>>> Hello,
>>>
>>> I am trying to create a simple application with support for dynamic
>>> subdomains. Let's say, every user will have it's own subdomain, so if
>>> his username is "tomcruise", I'd like to route all my application
>>> after login to "tomcruise.example.com". However, there are also some
>>> public static pages, perfectly suitable for classic "www" subdomain.
>>> So homepage and so on should be accessible at "www.example.com".
>>>
>>> I set SERVER_NAME to "example.com" and i use decorators like
>>> @app.route('/', subdomain='www') for home or @app.route('/blabla/',
>>> subdomain='<username>') for others, which seems to solve all this...
>>> except of static files. I have no idea how to handle URLs to these.
>>> I'd like to have them at one place. For now "www.example.com/static"
>>> would be perfect, but in future we'll maybe move it somewhere else
>>> (another subdomain, Amazon, whatever). How can I link to them?
>>>
>>> {{ url_for('static', filename='css/base.css') }}
>>>
>>> ...routes to http://example.com/css/base.css. Setting static_url_path
>>> to Flask() app doesn't seem to be helpful in any way, only adjusts
>>> the
>>> part between example.com/ and /css/base.css. There is no argument to
>>> pass to url_for in template to suggest a subdomain or something.
>>>
>>> Any ideas? Thanks!
>>> Honza
>>
>>  Hi,
>>
>>  This is the relevant code in Flask:
>>
>>  https://github.com/mitsuhiko/flask/blob/master/flask/app.py#L430
>>
>>  The following code will get you the same thing:
>>
>>     app = Flask(__name__, static_folder=None)  # Disable static
>>     app.static_folder = 'static'  # Enable is back, but the URL rule is
>>  still not created
>>     app.add_url_rule('/static/<path:filename>',
>>                      endpoint='static',
>>                      view_func=app.send_static_file)
>>
>>  ... but this time you get full control on the URL rule creation. See
>>  the documentation:
>>
>>  http://flask.pocoo.org/docs/api/#flask.Flask.add_url_rule
>>  http://werkzeug.pocoo.org/docs/routing/#werkzeug.routing.Rule
>>
>>
>

Re: [flask] Static files & subdomains

From:
Peter Cai
Date:
2011-08-25 @ 13:36
Why not add a standard method like this into flask?
A lot of url_for('static', filename='....') in template is a bit annoying.

On Thu, Aug 25, 2011 at 9:10 AM, Michael Fogleman <fogleman@gmail.com>wrote:

> This is how I handle static content on a subdomain. It also makes it
> less verbose in the templates.
>
> import urlparse
>
> app.config['STATIC_ROOT'] = 'http://static.example.com/'
>
> def static(path):
>    root = app.config.get('STATIC_ROOT', None)
>    if root is None: # fallback on the normal way
>        return url_for('static', filename=path)
>    return urlparse.urljoin(root, path)
>
> @app.context_processor
> def inject_static():
>    return dict(static=static)
>
> Then in the templates you can just do this, which is nice:
>
> {{ static('example.png') }}
>
> Michael
>
>
>
> On Thu, Aug 25, 2011 at 7:48 AM, Honza Javorek <honza@javorek.net> wrote:
> > Thank you! Works great :-)
> >
> > Regards,
> > Honza
> >
> >
> > On Thu, Aug 25, 2011 at 11:08, Simon Sapin <simon.sapin@exyr.org> wrote:
> >>  On Thu, 25 Aug 2011 10:43:54 +0200, Honza Javorek wrote:
> >>> Hello,
> >>>
> >>> I am trying to create a simple application with support for dynamic
> >>> subdomains. Let's say, every user will have it's own subdomain, so if
> >>> his username is "tomcruise", I'd like to route all my application
> >>> after login to "tomcruise.example.com". However, there are also some
> >>> public static pages, perfectly suitable for classic "www" subdomain.
> >>> So homepage and so on should be accessible at "www.example.com".
> >>>
> >>> I set SERVER_NAME to "example.com" and i use decorators like
> >>> @app.route('/', subdomain='www') for home or @app.route('/blabla/',
> >>> subdomain='<username>') for others, which seems to solve all this...
> >>> except of static files. I have no idea how to handle URLs to these.
> >>> I'd like to have them at one place. For now "www.example.com/static"
> >>> would be perfect, but in future we'll maybe move it somewhere else
> >>> (another subdomain, Amazon, whatever). How can I link to them?
> >>>
> >>> {{ url_for('static', filename='css/base.css') }}
> >>>
> >>> ...routes to http://example.com/css/base.css. Setting static_url_path
> >>> to Flask() app doesn't seem to be helpful in any way, only adjusts
> >>> the
> >>> part between example.com/ and /css/base.css. There is no argument to
> >>> pass to url_for in template to suggest a subdomain or something.
> >>>
> >>> Any ideas? Thanks!
> >>> Honza
> >>
> >>  Hi,
> >>
> >>  This is the relevant code in Flask:
> >>
> >>  https://github.com/mitsuhiko/flask/blob/master/flask/app.py#L430
> >>
> >>  The following code will get you the same thing:
> >>
> >>     app = Flask(__name__, static_folder=None)  # Disable static
> >>     app.static_folder = 'static'  # Enable is back, but the URL rule is
> >>  still not created
> >>     app.add_url_rule('/static/<path:filename>',
> >>                      endpoint='static',
> >>                      view_func=app.send_static_file)
> >>
> >>  ... but this time you get full control on the URL rule creation. See
> >>  the documentation:
> >>
> >>  http://flask.pocoo.org/docs/api/#flask.Flask.add_url_rule
> >>  http://werkzeug.pocoo.org/docs/routing/#werkzeug.routing.Rule
> >>
> >>
> >
>