Python code structure with flask integrated
Question: [Newbie] I have written a python program that does some data manipulations to
imported xlsx files and save them as csv. It looks kinda like this:

    
    
    #!/usr/bin/env python2.7    
    
    def main():
    
        imported_files = import_files_from_input_folder('/input/*.xlsx')
    
        data_handling_functions(imported_files)
    
        save_processed_files_to_output_folder('/output/')
    
    if __name__ = '__main__':
    
        main()
    

I want to create a web app (using flask) for the users to use the program, by
uploading their files to 'input' folder, and then download the results from
'output' folder. Thanks to [this
topic](http://stackoverflow.com/questions/27628053/uploading-and-downloading-
files-with-flask) and some others, I know how to upload multiple files into
'input' folder.

Now, my first question is: how to list all the files in 'output' folder and
let users download them?

My second question is: how to wrap/integrate the flask part into the existed
program?


Answer: This is how a basic Flask web server looks like:

    
    
    from flask import Flask
    app = Flask(__name__)
    
    @app.route("/")
    def hello():
        return "Hello World!"
    
    if __name__ == "__main__":
        app.run()
    

so it is quite straightforward to integrate your functionality inside the app.
Just add a routing function (like the hello) and call the functions you would
normally call inside the main in your code, and after that render the results
on a web page, along with some Download buttons. For this you could use the
function `render_template` and give the output (altered in such a way that it
can be iterated over to get each file) as a parameter, like so:

    
    
    @app.route("/")
    def hello():
        return render_template('name_of_template', output = your_output)
    

You can find Flask documentation [here](http://flask.pocoo.org) and it will
show you how to do all that. I recommend that you use the jinja2 API to add
Python code to your HTML templates e.g. you could do something like that:

    
    
    {% for file in output %}
        <!-- Output file inside html tag -->
        <!-- Add a download button -->
    {% endfor %}
    

You can find more about Jinja [here](http://jinja.pocoo.org/docs/dev/)

And of course, you would store the CSVs on your server.

