Reputation: 5
Hello I am currently a noob in Python and need to implement this for a homework, I would appreciate a lot your help please!
Currently I have this python code with flask implemented that sends input values from several text boxes in my HTML to my code and I can view the result in cmd prompt, but this "values" need to be input into another large python script with no flask so it can work. This variables are like the input variables for that code to work. How can I do that? I've tried "import from" and things like that but it doesn´t work, I don´t know if it is because how the function is constructed or my syntax, sorry if this is way to easy I would appreciate a lot your help! (some things are wrote in spanish but its only strings and variable names)
Leave my python/flask code below 'gettingtextvalue.py':
from flask import Flask, request, render_template
app = Flask(__name__)
#class TestDatos():
@app.route('/')
def my_form():
return render_template('from_ex.html')
@app.route('/', methods=['POST'])
def my_form_post():
num = request.form['a']
dist = request.form['b']
fallaPer = request.form['c']
fallaCen = request.form['d']
gen = request.form['e']
reb = request.form['f']
numInt = int(num)
distInt = int(dist)
fallaPerInt = int(fallaPer)
fallaCenInt = int(fallaCen)
genInt = int(gen)
rebInt = int(reb)
print "Numero de computadoras: ", numInt
print "Distribucion de trabajos: ", distInt
print "Falla en comp perifericas: ", fallaPerInt
print "Falla en comp central: ", fallaCenInt
print "Generacion de trabajos: ", genInt
print "Recuperacion despues de falla: ", rebInt
return (numInt, distInt, fallaPerInt, fallaCenInt, genInt, rebInt)
def operacion():
print("Prueba", numInt)
if __name__ == "__main__":
app.run()
And my HTML here:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Title</title>
</head>
<body>
<form method="post">
<p>Login</p><br>
<input name = "a">
<input name = "b">
<input name = "c">
<input name = "d">
<input name = "e">
<input name = "f">
<input type = "submit">
</form>
</body>
</html>
This is the other test script 'ejemplo.py' (not my final one):
from gettingtextvalue import my_form_post
print my_form_post()
This is what cmd displays from running 'gettingtextvalue.py':
Numero de computadoras: 1
Distribucion de trabajos: 2
Falla en comp perifericas: 3
Falla en comp central: 4
Generacion de trabajos: 5
Recuperacion despues de falla: 6
[2020-05-30 22:13:24,328] ERROR in app: Exception on / [POST]
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1953, in full_dispatch_request
return self.finalize_request(rv)
File "C:\Python27\lib\site-packages\flask\app.py", line 1968, in finalize_request
response = self.make_response(rv)
File "C:\Python27\lib\site-packages\flask\app.py", line 2090, in make_response
"The view function did not return a valid response tuple."
TypeError: The view function did not return a valid response tuple. The tuple must have the form (body, status, headers), (body, status), or (body, headers).
This is the error while running 'ejemplo.py':
RuntimeError: Working outside of request context.
This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.
Upvotes: 0
Views: 3973
Reputation: 12762
You can't call the view function my_form_post
directly (like you do in your ejemplo.py
). Flask's view function is meant to handle HTTP request sent by a client (browser), so it will be called by the web (WSGI) server.
If you want to get the form data sent from the browser and do something with it, you should wrap your process code in a function, then call it in the view function my_form_post
.
The return value of the view function will be sent to the browser (client), make sure it's either a string or dict or some function call that will generate a valid response (not tuple).
For example, write a calculate_it()
in your ejemplo.py
:
def calculate_it(numInt, distInt, fallaPerInt, fallaCenInt, genInt, rebInt):
# do some calculate with the input value
return the_result
Then import this function and call it in my_form_post
:
from ejemplo import calculate_it
@app.route('/', methods=['POST'])
def my_form_post():
# ...
numInt = int(num)
distInt = int(dist)
fallaPerInt = int(fallaPer)
fallaCenInt = int(fallaCen)
genInt = int(gen)
rebInt = int(reb)
result = calculate_it(numInt, distInt, fallaPerInt, fallaCenInt, genInt, rebInt)
return 'the result is %s' % result
Upvotes: 1