librelist archives

« back to archive

troubleshooting issue serving static folder

troubleshooting issue serving static folder

From:
Josh Klein
Date:
2013-02-08 @ 18:07
I'm struggling to troubleshoot an issue with my static folder, so I'm
hoping you can help.

I've just reorganized my project tree. Now, everything is working as
expected, except the application-wide static folder is not accessible, or
not set correctly, or something is wrong with similar symptoms. Any
pointers on what I'm doing wrong, or what could be useful to debug?
Flask==0.9. My troubleshooting below:

The templates are looking for the files in the place I think they're
supposed to be, e.g.:
127.0.0.1 - - [08/Feb/2013 12:16:52] "GET /static/css/bootstrap.min.css
HTTP/1.1" 404 -

And the app seems to be set to serve those files in the place I think
they're supposed to be, too:
>>> print app
<flask.app.Flask object at 0x10aee69d0>
>>> print app.static_folder
/static
>>> print app.static_url_path
/static
>>> print app.static_path
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'Flask' object has no attribute 'static_path'
>>> dir(app)

I've truncated the return of dir(app), but there's no static_path (which I
saw in the API docs). Is something happening there? My application factory
looks like this:

def create_app(config='development.cfg'):
sys.path.append(os.path.dirname(__file__))
 app = Flask('my_project_name',
instance_path=os.path.abspath('configs'),
 instance_relative_config=True,
static_path=os.path.join(os.path.abspath(os.path.dirname(__file__)),
'/static'),
 static_folder='/static',
static_url_path='/static')
 configure_app(app, config)
setup_database(app)
 setup_encryption(app)
setup_authentication(app)
 setup_blueprints(app)
configure_filters(app)
 return app

I only added the 3 static_* arguments recently so-as to be explicit. Could
it be something with the instance folder? This is what the project tree
looks like (roughly):

my_project_name/
├── Procfile <-- runs gunicorn server
with app:create_app(config="production.cfg")
├── app
│   ├── __init__.py <-- has create_app application factory
│   ├── core <-- main blueprint that also has utilities
│   │   ├── __init__.py
│   │   ├── authentication.py
│   │   ├── database.py
│   │   ├── encryption.py
│   │   └── models
│   │       ├── etc
│   │   └── templates
│   │       ├── etc
│   ├── blueprint <-- basic layout for all blueprints
│   │   ├── __init__.py
│   │   ├── constants.py
│   │   ├── forms.py
│   │   ├── templates
│   │   │   ├── etc...
│   │   ├── views.py
│   └── static
│       ├── css
│       ├── font
│       ├── img
│       └── js
│           ├── vendor
├── configs
│   ├── development.cfg
│   └── production.cfg
├── docs
│   └── etc...
├── manage.py <-- runs werkzeug dev server with 'python manage.py runserver
-c development.cfg'
├── tests
│   └── etc...

Thanks for the awesome project.

- Josh

Re: [flask] troubleshooting issue serving static folder

From:
Wouter Bolsterlee
Date:
2013-02-08 @ 20:49
Josh Klein schreef op vr 08-02-2013 om 13:07 [-0500]:
> I'm struggling to troubleshoot an issue with my static folder, so I'm
> hoping you can help.  

> >>> print app.static_folder
> /static

Could it be that you set an explicit path instead of a relative path?
Try "static" (without a leading slash) for the folder parameter.

    — Wouter

Re: [flask] troubleshooting issue serving static folder

From:
Owein Reese
Date:
2013-02-08 @ 22:39
I have a very similar structure. What i did was put the app in the__init__
folder but pull and instantiated it at the highest level. I'm at a
conference right now but i can give a gist in a few hours.
On Feb 8, 2013 3:59 PM, "Wouter Bolsterlee" <uws@xs4all.nl> wrote:

> Josh Klein schreef op vr 08-02-2013 om 13:07 [-0500]:
> > I'm struggling to troubleshoot an issue with my static folder, so I'm
> > hoping you can help.
>
> > >>> print app.static_folder
> > /static
>
> Could it be that you set an explicit path instead of a relative path?
> Try "static" (without a leading slash) for the folder parameter.
>
>     — Wouter
>
>
>

Re: [flask] troubleshooting issue serving static folder

From:
Josh Klein
Date:
2013-02-08 @ 22:44
On Fri, Feb 8, 2013 at 3:49 PM, Wouter Bolsterlee <uws@xs4all.nl> wrote:

>
> Could it be that you set an explicit path instead of a relative path?
> Try "static" (without a leading slash) for the folder parameter.
>
>     — Wouter
>
>
Great thinking. Not only does it require a relative path, but my project's
directory tree required that the relative path be set to 'app/static'. Is
this maybe due to me having instance_relative_config set to True? So
everything is working correctly with these params inside my application
factory:

app = Flask('myappname',
    instance_path=os.path.abspath('configs'),
    instance_relative_config=True,
    template_folder='templates',
    static_folder='app/static',
    static_url_path='/static')