I have a python script main.py that takes in two arguments (2 text files)
I'm using MAC OS X Python 2.7
This runs easily on terminal with:
python main.py train.txt sample.txt
I have now developed a small front-end using Flask with very minor HTML as follows:
#front.py FLASK
from flask import Flask, render_template, request, redirect
app = Flask(__name__)
@app.route('/')
def hello_world():
return render_template('index.html')
@app.route('/signup', methods = ['POST'])
def signup():
email = request.form['email']
email1 = request.form['email1']
# command below is just for testing, I wish to implement the same as this would if this would be typed in terminal.
print("main.py " + email + " " + email1)
return redirect('/')
if __name__ == "__main__":
app.run()
and the HTML
<!DOCTYPE html>
<html>
<head>
<title>T</title>
</head>
<body>
<form action="/signup" method="post">
<input type="text" name="email"></input>
<input type="text" name="email1"></input>
<input type="submit" value="Signup"></input>
</form>
</body>
</html>
This HTML code is simply using a form to take in the 2 arguments ( I find this easier than JS as I have no experience with that).
I have just written the
print("main.py " + email + " " + email1)
command above to test, it's not of any utility for now.
Usage of the parameters:
#main.py
from filter import Filter
import sys
# Get arguments from user
train = sys.argv[1]
messages = sys.argv[2]
# Open files for reading and writing
train_file = open(train, "rb")
messages_file = open(messages, "rb")
predictions_file = open("predictions.txt", "w")
# Create new filter and train it using the train-file
f = Filter()
f.train(train_file)
#filter the messages in messages_file, write results to predictions_file
f.filter(messages_file, predictions_file)
# Close all the files
train_file.close()
messages_file.close()
predictions_file.close()
I wish to now run my script which is main.py via this flask application itself, and want to know how this is possible.
I was using import main with another app decorator say /exec and manually changing the URL to go from 127.0.0.2000 to 127.0.0.2000/exec but this was giving errors as main requires the arguments to be passed.
Sorry if I'm unclear in explaining the problem, please let me know if I can explain anything in a better way to help understand the problem.
Thank you
You need to rework this script slightly. You should put all the code that deals with input inside a name == '__main__'
block as you do in the Flask app, and the rest inside a function that you call from that block:
def do_stuff(train, messages):
# Open files for reading and writing
train_file = open(train, "rb")
...
predictions_file.close()
if __name__ == '__main__':
# Get arguments from user
train = sys.argv[1]
messages = sys.argv[2]
do_stuff(train, messages)
Now your Flask app can call main.do_stuff(email, email1)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.