November 6th, 2012, 11:13 PM
Flask: problem with complex forms and returning files
I'm trying to port a program I wrote in PHP to Python using Flask. I've run into a somewhat multi-faceted problem, though, so I hope you can bear with me as I explain.
The first problem I run into with Flask is that it doesn't parse the query string like PHP does. If I have inputs named "parameters['some_date']" and "parameters['some_term']", it doesn't organize those into a single hash like PHP. It's important to have the parameters grouped into an array, since (a) I don't know ahead of time how many there will be, (b) I need to loop through them, separately from the other data, and (c) the parameter names are important.
The workaround for the problem was to submit the form as JSON using jquery AJAX. This is not ideal since it doesn't degrade gracefully, but there's a bigger problem -- I can't get AJAX to prompt the user for downloading files.
I've tried the iframe thing, and data URI. The iframe didn't work because my file data is generated on the fly in memory, and there isn't actually a URL that will return the file without resubmitting the POST and processing it twice.
The data URI works, but doesn't allow me to give the user a filename, isn't very cross-browser, and I don't think it'll work with large datasets.
Every solution that comes to mind seems like it'd be an unbelievable hack for something that ought to be relatively simple. Can anyone point me in the right direction, or suggest a different approach to the design?