0

I have used this code to parse csv using python flask

from flask import Flask, make_response, request, render_template
import csv

app = Flask(__name__)

def transform(text_file_contents):
    #return text_file_contents.replace("=", ",")
    return text_file_contents

@app.route('/') 
def form():
    return """ Transform a file demo
      <form action="/transform" method="post" enctype="multipart/form-data">
        <input type="file" name="data_file" />
        <input type="submit" />
      <form>"""

@app.route('/transform', methods=["POST"])
def transform_view():
    file = request.files['data_file']
    if not file:
        return "No file"

    file_contents = file.stream.read().decode("utf-8")
    csv_input = csv.reader(file_contents) 
    result = transform(file_contents)
    print result
    return result
    return render_template('commission.sh', result=result)

if __name__ == "__main__":
    import webbrowser
    webbrowser.open('localhost:5000')
    app.run()

The above is to parse the csv and display the output. But I need to send the output to the shell script as input to proceed further in the shell script itself.. Thanks in advance...

Sergey Shubin
  • 3,040
  • 4
  • 24
  • 36
Suganthan Raj
  • 2,330
  • 6
  • 31
  • 42
  • 1
    I have *very hesitantly* approved the code formatting edit here, since at least it could not have made the question any worse than it was. OP please review and follow up if something in the ostensibly corrected code is different from your actual code. In the future, please take care to format Python code with correct indentation (tip: copy/paste your code, select the pasted block, type ctrl-K to apply Markdown code formatting). – tripleee Apr 11 '17 at 07:52

1 Answers1

0

At first, I'm curious about the content of commission.sh. You need pass template file name as the first parameter of render_template. Jinja can't execute shell script for you.

And as the first answer point out, The content after the first return statement will not be executed. No matter what programming language you use.

You can follow this question to execute shell script in python(flask). And then return the result in view function.

If parse the csv is a time consuming task, I recommend you to try Celery with flask.

Community
  • 1
  • 1
stamaimer
  • 6,227
  • 5
  • 34
  • 55