Lab 6 - (2448001)
Lab 6 - (2448001)
Using appropriate routing, write client-side (html) and server-side (python) code for the
following:
1. Accept a number from a user through a form using 'get' method. For example say 8 (or say
11). Name this page as "input.html".
If the number is even then display on another html page "result.html":
You entered an even number 8.
Else if the number is odd then display:
You entered an odd number 11.
On result.html create a hyperlink back to "input.html" via server routing.
Ans:
CODE: (input.html)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title> Input Page</title>
</head>
<body>
<h1> Enter a number: </h1>
<form action ="/result" method ="get">
<label for = "number"><b>Number: </b></label>
<input type="number" name="number" required><br>
<br><button type="submit">Submit</button>
</form>
</body>
</html>
OUTPUT:
CODE: (result.html)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Result Page</title>
</head>
<body>
<h1>Result</h1>
{% if number % 2 == 0 %}
<p> You entered an even number {{number}}. </p>
{% else %}
<p> You entered an odd number {{number}}. </p>
{% endif %}
<a href="/input">Back to Input Page</a>
</body>
</html>
OUTPUT :
CODE : (myserver.py)
app = Flask(__name__)
@app.route('/input')
def input():
return render_template("input.html")
@app.route('/result', methods=['GET'])
def result():
number = int(request.args.get("number"))
return render_template("result.html",number=number)
if __name__ == '__main__':
app.run(debug=True)
Q2. On "readvalues.html" page read two numbers via text boxes and a mathematical
operation (addition, subtraction, multiplication, division) via radio button using form 'post'
method. Calculate the result based on the user's choice of operation and show the result in
"caculator.html" as below:
For example if given numbers are 2, 3 and the operation is multiplication then show:
2*3=6
For example if given numbers are 2, 0 and operation is division then show:
Division by zero is not possible !
On calculator.html create a hyperlink back to "readvalues.html" via server routing.
NOTE: use proper type conversions for float values.
Ans:
CODE (readvalues.html)
<label>Operation:</label><br>
<input type="radio" id="add" name="operation" value="add" required>
<label for="add">Addition</label><br>
<button type="submit">Calculate</button>
</form>
</body>
</html>
OUTPUT
CODE (calculator.html)
OUTPUT :
CODE (myserver.py)
app = Flask(__name__)
@app.route('/readvalues')
def readvalues():
return render_template('readvalues.html')
@app.route('/calculate', methods=['POST'])
def calculate():
# Retrieve form data
num1 = float(request.form.get('num1'))
num2 = float(request.form.get('num2'))
operation = request.form.get('operation')
if __name__ == '__main__':
app.run(debug=True)