librelist archives

« back to archive

Flask module layout

Flask module layout

From:
bruce bushby
Date:
2010-11-06 @ 01:55
Hi

Thanks to Anton's skeleton (
https://github.com/akhodakivskiy/flask/tree/master/skeleton/) I thought I
would give a module application a go however I'm having some troubles
accessing css style from the static directory in relation to the current
module being called.

This is my take on Anton's skeleton (https://github.com/BruceUK/ModuleLayout)
 however it refuses to make use ".static/css/main.css" and will only load
css from the application's static directory
rather then the modules static directory. I'm using jinja2 and "url_for"  ..
however it will ONLY look in the application static. The "ModuleLayout" link
above has all the code...it looks something like:

[bruce@core main]$ pwd
/home/bruce/work/myapp/myapp/modules/mail

[bruce@core main]$ cat views.py
from main import module
from flask import render_template, url_for, redirect, request

@module.route('/')
def root():
        return redirect('/main')

@module.route('/main')
def main():
        return render_template('main/index.html', title='Main')
[bruce@core main]$





[bruce@core templates]$ pwd
/home/bruce/work/myapp/myapp/modules/main/templates

[bruce@core templates]$ cat base.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
        {% block head %}
                <title>MyApp - {% block title %}{% endblock %}</title>
                <meta http-equiv="Content-Type" content="text/html;
charset=utf-8">
                <link rel="stylesheet" type="text/css" href="{{
url_for('.static', filename='css/style.css') }}" />
        {% endblock %}
</head>

<body>
        {% block main_back %}
                <div id=main_back>main_back</div>
        {% endblock %}
</body>
[bruce@core templates]$


I'm running gevent with debug and all I get is a white browser page.....and
no errors from debug....unless I add an intentional spelling error etc.
Using "css" from the "application static" works 100%

Any ideas?  .. would be much appreciated!!!!!

Cheers
Bruce

Re: [flask] Flask module layout

From:
Anton Khodakivskiy
Date:
2010-11-06 @ 12:51
This problem has to do with url prefixes.
http://flask.pocoo.org/docs/api/#module-objects

from flask import Module
module = Module(__name__, 'main')

If you register the Module this way, then the static path on the web is 
/static, which clashes with that of the top level Application static 
folder. So you have to change it, there are at least two ways:

1) module = Module(__name__, 'main', static_path='/main/static')
2) module = Module(__name__, 'main', url_prefix='/main')

The second way will also prefix all the view urls that belong to the given module.

Also read up here:
http://flask.pocoo.org/docs/patterns/packages/

Thanks
Anton

On Nov 6, 2010, at 3:55 AM, bruce bushby wrote:

> Hi
> 
> Thanks to Anton's skeleton 
(https://github.com/akhodakivskiy/flask/tree/master/skeleton/) I thought I
would give a module application a go however I'm having some troubles
> accessing css style from the static directory in relation to the current
module being called.
> 
> 
> This is my take on Anton's skeleton 
(https://github.com/BruceUK/ModuleLayout)  however it refuses to make use 
".static/css/main.css" and will only load css from the application's 
static directory
> rather then the modules static directory. I'm using jinja2 and "url_for"
.. however it will ONLY look in the application static. The "ModuleLayout"
link above has all the code...it looks something like:
> 
> [bruce@core main]$ pwd
> /home/bruce/work/myapp/myapp/modules/mail
> 
> [bruce@core main]$ cat views.py
> from main import module
> from flask import render_template, url_for, redirect, request
> 
> @module.route('/')
> def root():
>         return redirect('/main')
> 
> @module.route('/main')
> def main():
>         return render_template('main/index.html', title='Main')
> [bruce@core main]$
> 
> 
> 
> 
> 
> [bruce@core templates]$ pwd
> /home/bruce/work/myapp/myapp/modules/main/templates
> 
> [bruce@core templates]$ cat base.html
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
> <html lang="en">
> <html xmlns="http://www.w3.org/1999/xhtml">
> 
> <head>
>         {% block head %}
>                 <title>MyApp - {% block title %}{% endblock %}</title>
>                 <meta http-equiv="Content-Type" content="text/html; 
charset=utf-8">
>                 <link rel="stylesheet" type="text/css" href="{{ 
url_for('.static', filename='css/style.css') }}" />
>         {% endblock %}
> </head>
> 
> <body>
>         {% block main_back %}
>                 <div id=main_back>main_back</div>
>         {% endblock %}
> </body>
> [bruce@core templates]$
> 
> 
> I'm running gevent with debug and all I get is a white browser 
page.....and no errors from debug....unless I add an intentional spelling 
error etc. Using "css" from the "application static" works 100%
> 
> Any ideas?  .. would be much appreciated!!!!!
> 
> Cheers
> Bruce
> 
> 

Re: [flask] Flask module layout

From:
bruce bushby
Date:
2010-11-06 @ 14:43
ahh thanks Anton, much appreciated!!! Will give it a go.

On Sat, Nov 6, 2010 at 12:51 PM, Anton Khodakivskiy <akhodakivskiy@gmail.com
> wrote:

> This problem has to do with url prefixes.
> http://flask.pocoo.org/docs/api/#module-objects
>
> from flask import Module
> module = Module(__name__, 'main')
>
> If you register the Module this way, then the static path on the web is
> /static, which clashes with that of the top level Application static folder.
> So you have to change it, there are at least two ways:
>
> 1) module = Module(__name__, 'main', static_path='/main/static')
> 2) module = Module(__name__, 'main', url_prefix='/main')
>
> The second way will also prefix all the view urls that belong to the given
> module.
>
> Also read up here:
> 
http://flask.pocoo.org/docs/patterns/packages/<http://flask.pocoo.org/docs/p+atterns/packages/>
>
> Thanks
> Anton
>
> On Nov 6, 2010, at 3:55 AM, bruce bushby wrote:
>
> Hi
>
> Thanks to Anton's skeleton (
> https://github.com/akhodakivskiy/flask/tree/master/skeleton/) I thought I
> would give a module application a go however I'm having some troubles
> accessing css style from the static directory in relation to the current
> module being called.
>
> This is my take on Anton's skeleton (
> https://github.com/BruceUK/ModuleLayout)  however it refuses to make use
> ".static/css/main.css" and will only load css from the application's static
> directory
> rather then the modules static directory. I'm using jinja2 and "url_for"
>  .. however it will ONLY look in the application static. The "ModuleLayout"
> link above has all the code...it looks something like:
>
> [bruce@core main]$ pwd
> /home/bruce/work/myapp/myapp/modules/mail
>
> [bruce@core main]$ cat views.py
> from main import module
> from flask import render_template, url_for, redirect, request
>
> @module.route('/')
> def root():
>         return redirect('/main')
>
> @module.route('/main')
> def main():
>         return render_template('main/index.html', title='Main')
> [bruce@core main]$
>
>
>
>
>
> [bruce@core templates]$ pwd
> /home/bruce/work/myapp/myapp/modules/main/templates
>
> [bruce@core templates]$ cat base.html
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
> <html lang="en">
> <html xmlns="http://www.w3.org/1999/xhtml">
>
> <head>
>         {% block head %}
>                 <title>MyApp - {% block title %}{% endblock %}</title>
>                 <meta http-equiv="Content-Type" content="text/html;
> charset=utf-8">
>                 <link rel="stylesheet" type="text/css" href="{{
> url_for('.static', filename='css/style.css') }}" />
>         {% endblock %}
> </head>
>
> <body>
>         {% block main_back %}
>                 <div id=main_back>main_back</div>
>         {% endblock %}
> </body>
> [bruce@core templates]$
>
>
> I'm running gevent with debug and all I get is a white browser page.....and
> no errors from debug....unless I add an intentional spelling error etc.
> Using "css" from the "application static" works 100%
>
> Any ideas?  .. would be much appreciated!!!!!
>
> Cheers
> Bruce
>
>
>
>