-1

I'm trying to call a complicated python script in a flask app, but not entirely sure on how to do it right now. If it was simple I would just copy and paste the script with a router in flask, but that would get too messy. So far I have this for my main.py. I want to call a script temp.py.

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

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


@app.route('/temp', methods=['GET', 'POST'])
def temp():


if __name__ == "__main__":
    app.run(debug=True)
  • In addition to this, you can maintain different queuing and scheduling techniques as well. – Nabin Nov 27 '17 at 05:18
  • in `temp.py` ensure there is a `__name__ == '__main__':` block, so it doesn't execute immediately when its imported. Then if it doesnt already exist, define a function to call in `temp.py` that will execute the code you want to run and then import temp and call the function. – Paul Rooney Nov 27 '17 at 05:20
  • Thanks, the stackoverflow thread you linked me to actually helped me a lot! – Richard Chen Nov 29 '17 at 07:32

1 Answers1

1

You can use subprocess for calling script in command line

import subprocess
process = subprocess.Popen(['python' , 'temp.py' ], stdout=subprocess.PIPE)
out, err = process.communicate()
print(out)

it will provide the output or error(if its there)

for more info on this look for:

python 2: https://docs.python.org/2/library/subprocess.html

python 3: https://docs.python.org/3/library/subprocess.html

  • or `os.system()` or `subprocess.call()` or `subprocess.check_output()`... there are many ways to create/call a process from current process – Artsiom Praneuski Nov 27 '17 at 08:35