librelist archives

« back to archive

The correct way to generate paths for files

The correct way to generate paths for files

From:
Nicky Chorley
Date:
2015-07-26 @ 16:36
Hi all,

I'm building a small application with Flask, in which a user will upload an
audio file for some processing. The back end will generate a new file
resulting from that processing and then display a new page for the user to,
say, download that file.

The processing part works (well, there's only a small amount there right
now) and the files are generated.

What I'm struggling with is how to generate the path to the files in my
templates. All works fine if I put them in my 'static' directory and then
do, e.g.

<a href="{{ url_for('static', filename='foo-modified.wav') }}">Modified
file</a>

However, putting these files in 'static' doesn't seem like the correct
thing to do, as that's really meant for CSS/JS/static HTML.

What I would like is to create my own directory for these, called, say,
'output' (or something). What, then, is the correct incantation of
url_for()? I also looked at using instance folders for this, but again
couldn't work out how to get the path correctly.

Any ideas?

Thanks,

Nicky

Re: [flask] The correct way to generate paths for files

From:
David Nieder
Date:
2015-07-27 @ 11:34
On 26.07.2015 18:36, Nicky Chorley wrote:
> Hi all,
>
> I'm building a small application with Flask, in which a user will upload an
> audio file for some processing. The back end will generate a new file
> resulting from that processing and then display a new page for the user to,
> say, download that file.
>
> The processing part works (well, there's only a small amount there right
> now) and the files are generated.
>
> What I'm struggling with is how to generate the path to the files in my
> templates. All works fine if I put them in my 'static' directory and then
> do, e.g.
>
> <a href="{{ url_for('static', filename='foo-modified.wav') }}">Modified
> file</a>
>
> However, putting these files in 'static' doesn't seem like the correct
> thing to do, as that's really meant for CSS/JS/static HTML.
>
> What I would like is to create my own directory for these, called, say,
> 'output' (or something). What, then, is the correct incantation of
> url_for()? I also looked at using instance folders for this, but again
> couldn't work out how to get the path correctly.
>
> Any ideas?
>
> Thanks,
>
> Nicky
>

Hey Nicky.

There is a chapter on file uploads in the docs:
http://flask.pocoo.org/docs/0.10/patterns/fileuploads/
It's worth the read.

So if you want to serve the files with your flask application you need 
something like this:

@app.route('/uploads/<filename>')
def serve_file(filename):
     return send_from_directory('/filesystem/path/output', filename)

Now you can use url_for():
url_for('serve_file', filename='foo-modified.wav')

If this project is more than something you run locally for yourself, you 
probably don't want to server static files through python but use your 
web server. In order to use url_for() in this case you still need to 
register the url in your application but you don't attach any code to it:

app.add_url_rule('/uploads/<filename>', 'uploaded_file', build_only=True)

I hope this helps you with your problem.
Cheers
David

P.S. There is a new flask mailing list:
https://mail.python.org/mailman/listinfo/flask