librelist archives

« back to archive

Trouble reading a static file in a function

Trouble reading a static file in a function

From:
Alex
Date:
2010-12-29 @ 22:12
Hi all.

I wrote a simple function to read a file in the static folder and
render its contents in a template. The function is:

@app.route("/faq/", methods=['GET'])
def show_faq():
    content = codecs.open('static/faq.markdown','r','utf-8').read()
    content = Markup(markdown.markdown(content))
    return render_template('faq.html', content=content)

App structure is:

- myApp
  - static
  - templates
  - app.py


When I run the app with:

python app.py

All works ok.

When I run the application from outside the root folder with:

python myApp/app.py

I got an error accessing the URL /faq.html because the app cannot find
the file static/faq.markdown
This behaviour do not affect another similar function that use send_file

@app.route("/some_file.xml", methods=['GET'])
def sitemap():
    return send_file('static/some_file.xml')

My question is:  what is the best practice to access a file in the
static folder within a function?

Thanks in advance.

Alex

Re: [flask] Trouble reading a static file in a function

From:
Simon Sapin
Date:
2010-12-29 @ 22:50
Le 30/12/2010 07:12, Alex a écrit :
> My question is:  what is the best practice to access a file in the
> static folder within a function?

Be aware that everything in the static directory will be served to HTTP 
clients, eg. http://localhost:5000/static/faq.markdown
This may or may not be what you want. If it’s not, put these resources 
in another directory next to 'static'. I use 'pages' for Flask-FlatPages.

Regards,
-- 
Simon Sapin
http://exyr.org/

Re: [flask] Trouble reading a static file in a function

From:
Alex
Date:
2010-12-30 @ 09:49
On Wed, Dec 29, 2010 at 11:50 PM, Simon Sapin <simon.sapin@exyr.org> wrote:
> Le 30/12/2010 07:12, Alex a écrit :
>> My question is:  what is the best practice to access a file in the
>> static folder within a function?
>
> Be aware that everything in the static directory will be served to HTTP
> clients, eg. http://localhost:5000/static/faq.markdown
> This may or may not be what you want. If it’s not, put these resources
> in another directory next to 'static'. I use 'pages' for Flask-FlatPages.
>

I did not know it, Thanks for the heads up.

Alex

Re: [flask] Trouble reading a static file in a function

From:
Steven Kryskalla
Date:
2010-12-29 @ 22:21
On Wed, Dec 29, 2010 at 5:12 PM, Alex <thinkpragmatic@gmail.com> wrote:
>
> My question is:  what is the best practice to access a file in the
> static folder within a function?
>

I've always done it this way, by building an absolute path to the file:

ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
...
path = os.path.join(ROOT_DIR, 'static/faq.markdown')
content = codecs.open(path,'r','utf-8').read()

-Steve

Re: [flask] Trouble reading a static file in a function

From:
Alex
Date:
2010-12-30 @ 09:48
On Wed, Dec 29, 2010 at 11:21 PM, Steven Kryskalla <skryskalla@gmail.com> wrote:
> On Wed, Dec 29, 2010 at 5:12 PM, Alex <thinkpragmatic@gmail.com> wrote:
>>
>> My question is:  what is the best practice to access a file in the
>> static folder within a function?
>>
>
> I've always done it this way, by building an absolute path to the file:
>
> ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
> ...
> path = os.path.join(ROOT_DIR, 'static/faq.markdown')
> content = codecs.open(path,'r','utf-8').read()

Thanks Steve I'll go this way.

Alex