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
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/
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
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
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