how to process an upload file in Flask?
Question: I have a simple Flask app and I would like for it to process an uploaded excel
file and display it's data in the webpage. So far I got a page to upload the
excel file.

main.py

    
    
    from flask import Flask, render_template, send_file, request
    from flask_uploads import UploadSet, configure_uploads, DOCUMENTS, IMAGES
    
    app = Flask(__name__)
    
    #the name 'datafiles' must match in app.config to DATAFILES
    docs = UploadSet('datafiles', DOCUMENTS)
    app.config['UPLOADED_DATAFILES_DEST'] = 'static/uploads'
    configure_uploads(app, docs)
    
    @app.route("/", methods=['GET'])
    def index():
      # return send_file("templates/index.html")
      return render_template('index.html')
    
    @app.route("/upload", methods = ['GET', 'POST'])
    def upload():
      #user_file is the name value in input element
      if request.method == 'POST' and 'user_file' in request.files:
        filename = docs.save(request.files['user_file'])
        return filename
      return render_template('upload.html')
    
    @app.route("/mergedata", methods=['GET'])
    def merge_data():
       return send_file("templates/mergeDataPage.html")
    
    if __name__ == "__main__":
      app.run(host='0.0.0.0', debug=True)
    

upload.html

    
    
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>upload</title>
      </head>
      <body>
        <form method="POST" enctype=multipart/form-data action ="{{url_for('upload')}}">
          <input type="file" name="user_file">
          <input type="submit">
        </form>
      </body>
    </html>
    

Once the file is uploaded, I would like to process this excel data by using
another python script - This script will take the excel file, remove special
characters and converted to a csv file/output. I would like to display the csv
output in the webpage.

The idea is for users to upload the file, press a button to clean the data and
have the results printed in an output box. How do I go about in building
something like this scenario?

I'm pretty new to Flask, but so far has been pretty exciting to learn this
cool web framework. Your help will be appreciated, thanks!


Answer: You can use [pandas](http://pandas.pydata.org/) module and first convert the
excel file.

    
    
    import pandas as pd
    data_xls = pd.read_excel('your_workbook.xls', 'Sheet1', index_col=None)
    data_xls.to_csv('your_csv.csv', encoding='utf-8')
    

Pandas is spectacular for dealing with csv files, and the following code would
be all you need to read a csv and save an entire column into a variable:

    
    
    import pandas as pd
    df = pd.read_csv(csv_file)
    saved_column = df.column_name #you can also use df['column_name']
    

For instance if you wanted to save all of the info in your column Names into a
variable, this is all you need to do:

    
    
    names = df.Names
    

