Skip to content

Instantly share code, notes, and snippets.

@ripstick
Forked from rminderhoud/upload.py
Created March 15, 2017 02:41
Show Gist options
  • Select an option

  • Save ripstick/94a63a7562bc3c31bd236b2ec8c34dc1 to your computer and use it in GitHub Desktop.

Select an option

Save ripstick/94a63a7562bc3c31bd236b2ec8c34dc1 to your computer and use it in GitHub Desktop.

Revisions

  1. @rminderhoud rminderhoud revised this gist Mar 14, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion upload.py
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    Example provided for spitfiredd on thread
    https://www.reddit.com/r/flask/comments/5zen73/help_looking_to_create_a_simple_web_app_where_i/?ref=share&ref_source=link
    """
    from flask import Flask, request, render_template
    from flask import Flask, request, render_template, send_file

    app = Flask(__name__)

  2. @rminderhoud rminderhoud created this gist Mar 14, 2017.
    69 changes: 69 additions & 0 deletions upload.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    """ Basic file upload example for Flask
    Example provided for spitfiredd on thread
    https://www.reddit.com/r/flask/comments/5zen73/help_looking_to_create_a_simple_web_app_where_i/?ref=share&ref_source=link
    """
    from flask import Flask, request, render_template

    app = Flask(__name__)

    # We need a secret key to work with form data
    # http://flask.pocoo.org/docs/0.12/config/#builtin-configuration-values
    app.config['SECRET_KEY'] = 'super secret'

    @app.route('/', methods=['GET', 'POST'])
    def index():
    # The browser sends an HTTP GET when opening a page
    # so we respond with some HTML to display our page
    if request.method == 'GET':
    return '''<!DOCTYPE html>
    <html>
    <head><title>Upload</title></head>
    <body>
    <form method="POST" enctype="multipart/form-data">
    <input type="file" name="file1" />
    <input type="file" name="file2" />
    </form>
    </body>
    </html>'''
    elif request.method == 'POST':
    # Get our files from the request object (or None if
    # there is no file
    file1 = request.files.get('file1', None)
    file2 = request.files.get('file2', None)

    # Check if files exist
    files_exist = file1 != None and file2 != None
    files_name = file1.filename != '' and file2.filename != ''

    # Return error if they don't
    if not (files_exist and files_named):
    flash('Please select two files')
    return redirect(request.url)

    # Do processing here (e.g. load into pandas)
    #
    # NOTE: If you are saving the files with the original name
    # make sure to sanitize it first
    #
    # Example (http://xlsxwriter.readthedocs.io/working_with_pandas.html#saving-the-dataframe-output-to-a-string):
    import io
    import pandas as pd

    d = pd.DataFrame()
    output = io.BytesIO()
    writer = pd.ExcelWriter(output, engine='xslxwriter')
    d.to_excel(writer, sheet_name='Sheet1')
    writer.save()

    return send_file(output, attachment_filename='output.xslx', as_attachment=True)