Flask Accept data from user
Pre - Req :
1. Make sure u have installed Flask module , Requests module
2. Currently using Liclipse as python IDE
Example 1 :
To accept data from user ( post request ) and display result in same page
----------------------Paste below code inside Your_Project/templates/vote.html------------------------------<html>
<body>
<form action="" method = "POST">
<p>Enter Name : <input name="name" type="text" /></p>
<p>Enter Age :<input name="age" type="text" /> </p>
<! note : button should always be type ="submit">
<p><input name="OK" type="submit" value="submit" /></p>
</form>
{% if name %}
<p><strong>{{name}} is {{eligible}} to vote</span></strong></p>
{% endif %}
</body>
</html>
--------------------------------------------------------------------------------------------------------
from flask import request,Flask,json,render_template
from flask.json import jsonify
app = Flask(__name__)
@app.route('/')
def vote():
return render_template('vote.html')
@app.route('/',methods=['POST'])
def vote1():
result1 = request.form
#input fields values are stored as dictionary and key = input name given in html
if (int(result1['age'])>=18):
eligible="is eligible"
else:
eligible="is not eligible"
return render_template("vote.html",name=result1['name'],eligible=eligible)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=1111 , debug=True)#http://localhost:1111/"
Steps :
1. Run the above code
1. Run the above code
2. Open browser
3. Open http://localhost:1111/ or http://your_sytem_name:1111
Example 2 :
To accept data from user ( post request ) and display result in different page
----------------------Paste below code inside Your_Project/templates/vote2.html-----------------------------<html>
<body>
<form action="http://den01bro.us.oracle.com:1111/result" method = "POST">
<p>Enter Name : <input name="name" type="text" /></p>
<p>Enter Age :<input name="age" type="text" /> </p>
<! note : button should always be type ="submit">
<p><input name="OK" type="submit" value="submit" /></p>
</form>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------
from flask import request,Flask,json,render_template
from flask.json import jsonify
app = Flask(__name__)
@app.route('/<string:name>')
def temp(name):
return render_template('vote2.html')
@app.route('/result' ,methods=['POST'])
def temp2():
result1 = request.form
if (int(result1['age'])>=18):
eligible="is eligible"
else:
eligible="is not eligible"
return (result1['name']+" "+eligible)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=1111 , debug=True)#http://localhost:8000/"
1. Run the above code
2. Open browser
3. Open http://localhost:1111/any_string or http://your_sytem_name:1111/any_string_here
Example 3:
Adding example 1 + example 2 , ie.,
scenario 1 : If user opens http://localhost:1111/ , User will get result in the same page.
scenario 2 : If user opens http://localehost:1111/any_string , User will get result in different page .
----------------------Paste below code inside Your_Project/templates/vote.html------------------------------
<html>
<body>
<form action="" method = "POST">
<p>Enter Name : <input name="name" type="text" /></p>
<p>Enter Age :<input name="age" type="text" /> </p>
<! note : button should always be type ="submit">
<p><input name="OK" type="submit" value="submit" /></p>
</form>
{% if name %}
<p><strong>{{name}} is {{eligible}} to vote</span></strong></p>
{% endif %}
</body>
</html>
----------------------Paste below code inside Your_Project/templates/vote2.html-----------------------------
<html>
<body>
<form action="http://den01bro.us.oracle.com:1111/result" method = "POST">
<p>Enter Name : <input name="name" type="text" /></p>
<p>Enter Age :<input name="age" type="text" /> </p>
<! note : button should always be type ="submit">
<p><input name="OK" type="submit" value="submit" /></p>
</form>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------
<html>
<body>
<form action="" method = "POST">
<p>Enter Name : <input name="name" type="text" /></p>
<p>Enter Age :<input name="age" type="text" /> </p>
<! note : button should always be type ="submit">
<p><input name="OK" type="submit" value="submit" /></p>
</form>
{% if name %}
<p><strong>{{name}} is {{eligible}} to vote</span></strong></p>
{% endif %}
</body>
</html>
----------------------Paste below code inside Your_Project/templates/vote2.html-----------------------------
<html>
<body>
<form action="http://den01bro.us.oracle.com:1111/result" method = "POST">
<p>Enter Name : <input name="name" type="text" /></p>
<p>Enter Age :<input name="age" type="text" /> </p>
<! note : button should always be type ="submit">
<p><input name="OK" type="submit" value="submit" /></p>
</form>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------
from flask import request,Flask,json,render_template
from flask.json import jsonify
app = Flask(__name__)
@app.route('/')
def vote():
return render_template('vote.html')
@app.route('/',methods=['POST'])
def vote1():
result1 = request.form
if (int(result1['age'])>=18):
eligible="is eligible"
else:
eligible="is not eligible"
return render_template("vote.html",name=result1['name'],eligible=eligible)
@app.route('/<string:name>')
def temp(name):
return render_template('vote2.html')
@app.route('/result' ,methods=['POST'])
def temp2():
result1 = request.form
if (int(result1['age'])>=18):
eligible="is eligible"
else:
eligible="is not eligible"
return (result1['name']+" "+eligible)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=1111 , debug=True)
Example 4:
Example 4:
Accept 2 input from a html file and display same value into another html .
----------------------Paste below code inside Your_Project/templates/input_data.html--------------------
<form action="{{ url_for('submit') }}" method="post">
<textarea name="text"></textarea>
<textarea name="text2"></textarea>
<input type="submit">
</form>
----------------------Paste code in Your_Project/templates/output_2data.html--------------------
<p>text1 <input name="text1" type="text" value={{text1}}/></p>
<p>text2 <input name="text2" type="text" value={{text2}}/></p>
----------------------input_data2.py----------------------
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('input_data.html')
@app.route('/submit', methods=['POST'])
def submit():
data=request.form
data=request.form
data1=data['text']
data2=data['text2']
return render_template("output_2data.html",text1=data1,text2=data2)
#return 'You entered: {}'.format(request.form['text'])
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8003)
-------------------------------------------------------------------------------------------------------------------------
No comments:
Post a Comment