librelist archives

« back to archive

Getting absolute paths for loading resources?

Getting absolute paths for loading resources?

From:
Michael Fogleman
Date:
2011-08-13 @ 01:42
I wrote myself a simple helper function:

def path_for(app, path):
    return os.path.join(app.root_path, path)

Example usage:

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///%s' % path_for(app,
'database.db')

I use it for loading resources from disk, relative to the app
directory. My question is - have I reinvented the wheel? Is there a
conventional/better way of doing this in Flask?

Thanks,
Michael

Re: [flask] Getting absolute paths for loading resources?

From:
Armin Ronacher
Date:
2011-08-13 @ 08:07
Hi,

On 8/13/11 3:42 AM, Michael Fogleman wrote:
> I wrote myself a simple helper function:
> 
> def path_for(app, path):
>     return os.path.join(app.root_path, path)
> 
> Example usage:
> 
> app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///%s' % path_for(app,
> 'database.db')
> 
> I use it for loading resources from disk, relative to the app
> directory. My question is - have I reinvented the wheel? Is there a
> conventional/better way of doing this in Flask?
Flask-SQLAlchemy is automatically relative to the app root by default.
Just leave the 4th slash out.  For opening files for reading there is
app.open_resource.

However with the introduction of instance folders I am considering
changing Flask-SQLAlchemy to be relative to the instance folder.


Regards,
Armin