7

I have a small flask app where it takes user input and returns some text. Here the user input is fed to another python script say temp.py and this temp.py will return a value which should be returned to user. For eg:

flask.py

from flask import Flask, render_template, request
app = Flask(__name__)

@app.route('/')
def result():
    return render_template("index.html")

@app.route('/getconfig', methods=['GET', 'POST'])
def config():
    value = request.form['config']
    print ("This is the user value :  ", value);
    // This value is written to a file say x.pol and my temp.py reads from x.pol file and writes the output to y.pol. I'm not sure how to trigger the script like "python temp.py" on the server.
    // The reason I'm doing this because I don't want to tweak the third party tool where, the third party tool reads from input files and generates output files. 

    return content_generated_by_temp.py // by reading y.pol.

if __name__ == '__main__':
   app.run(debug = True)

By the way, temp.py is itself a nightmare. I understood what the tool does. For more info about the tool: Google Caprica

3
  • Use os module to call this other python script in temp.py or better way would be to make a function in temp.py, import that function and call from flask app. Commented Aug 7, 2017 at 18:40
  • @SachinKukreja I cannot make it as function. I added the tool I'm using. It has so many dependencies and I don't want to touch any of them as it may ruin the generated pols. Commented Aug 7, 2017 at 18:42
  • then, os module can do your job. After that, read the output file generated by temp. Commented Aug 7, 2017 at 18:48

1 Answer 1

9

Wrap whatever temp.py is doing in a function. Place it in the same directory as flask.py. Call import temp in flask.py, then use temp.myfunction()

Sign up to request clarification or add additional context in comments.

1 Comment

I'd suggest this over os for sure. Not that os is wrong, but this feels a lot better. And you should be make able to make it return an object rather than writing to disk and having to read it back in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.