[Flask] A routing question

Adam Steer Adam.Steer at anu.edu.au
Fri Mar 18 01:16:39 EDT 2016


Hi Brian

> Looks like a cool project that you're working on.

Thanks!

> Your problem probably lies in the call to send_from_directory but we're going to need some more information to help you any further. Specifically, what is the value of app.config['ROOT']? It doesn't seem to be a predefined entry in the config dictionary. 

app.config[‘ROOT’] = ‘/home/(user)/data/‘ - but the eventual location is not fixed.

This is where files and directories being exposed by the app live, so http://some_server/directory/ ==> /home/user/data/directory and http://some_server/file.txt ==> /home/user/data/file.txt

For a bit more context, the app will be deployed on virtual machines in a cloud environment, for users who request a public ftp style service. I figure we will guide users about where they can put files, but this basically the location needs to be pretty flexible.

> Also, are you hosting the app through a WSGI-supported server or are you just hosting it via app.run()?

Right now, it’s going using app.run().

…but it will need to deploy using WSGI. I’ve been working on making it run on WSGI, and I *think* I’m going to need to do something like declare the directory which is currently in app.config[‘ROOT’] in my apache virtual host, and then tell Flask to serve static files from there.

I’ll see if there are any barriers to publishing the code. If not, I’ll put the app in a git repo over the weekend. I’m pretty certain a lot of it could be written/done a lot better! Flask has a lot it can do, but right now a lot of it leads to mushy brain at this end...

Thanks

Adam











> Best,
> BK
> 
> On Thu, Mar 17, 2016 at 8:38 PM, Adam Steer <Adam.Steer at anu.edu.au> wrote:
> Hi all
> 
> I am working on a small application that is essentially flask-autoindex with some bells and whistles - it is meant to easily expose files for download, and allow a user to select multiple files to zip and download using checkboxes. It essentially looks like Autoindex (https://pythonhosted.org/Flask-AutoIndex/), and the reason I didn’t use autoindex is that I could not figure out how to expose the lists of files and directories to other functions in my flask app. Which I need to do in order to allow users to select a bunch of files to download at once.
> 
> Things are mostly working - but I’m stuck on routing. I want my flask app to navigate a directory tree which it builds on the fly, and then download files which are clicked on.
> 
> This routing works for navigating a directory tree:
> ---
> @app.route('/', methods=['GET', 'POST'])
> @app.route('/<path:path>/', methods=['GET', 'POST'])
> def render_page(path=None):
>      …..stuff
>      return render_template(page.html, files=files,dirs=dirs)
> ---
> …where files are listed in page.html using:
> 
>  {% for f in files %}
>         <tr>
>               <td>
>                 {{ f.0.name }}
>               </td>
>               <td>
>                 [<a href="{{ request.base_url + '/' + f.0.name }}">http download</a>]
>               </td>
>               <td>{{ f.0.size|filesizeformat }}</td>
>             </tr>
>  {% endfor %}
> 
> …but if I click on the file link in my browser, flask sees the file name as a directory name. Links to directories have the same structure, loop through a list and show links with the format:
> 
>   <a href="{{ request.base_url[:-1] + '/' + d.name }}">{{ d.name }}</a>
> 
> 
> Adding another endpoint:
> 
> @app.route('/<path:filename>')
> def getthefile(filename):
>     return send_from_directory(app.config['ROOT'], filename,as_attachment=True)
> 
> …lets me download files at the root level of my directory tree, changing the file link to  <a href=“{{ url_for(‘getthefile’, f.0.name) }}” >http download</a> in my template.
> 
> …but gives a 404 if I try to navigate to a directory.
> 
> SO, I am clearly confusing how Flask routes to things. I want to treat directories one way, and files another, but using the same route.
> 
> The endpoint of the app has the app living in /var/www/…, but the file tree that flask is displaying starting at some arbitrary point (right now /home/user/data/). So I guess I’m trying to serve static files from a dynamically built tree.
> 
> …and I’m pretty much blundering my way through flask and trying things out. This is the first real hangup so far!
> 
> Any advice you can offer will be greatly appreciated. I’m sure it’s something very simple that I’m just not getting.
> 
> Thanks
> 
> Adam
> _______________________________________________
> Flask mailing list
> Flask at python.org
> https://mail.python.org/mailman/listinfo/flask
> 



More information about the Flask mailing list