PF 9
PF 9
Course_id:22DC101A
Section:S-33
Id:2200090067
Name: p aakash
Pre-Lab:
a) Hello World: This is a simple program that displays "Hello, World!" in the browser.
app = Flask(__name__)
@app.route('/')
def hello_world():
if __name__ == '__main__':
app.run(debug=True)
output:
* Running on http://127.0.0.1:5000
In-Lab:
a)Design and develop a Flask application to demonstrate Student results. Using Home. Courses, Rankers
Gallery.
app = Flask(__name__)
students = [
]
@app.route('/')
def home():
@app.route('/courses')
def courses():
@app.route('/rankers-gallery')
def rankers_gallery():
if __name__ == '__main__':
app.run(debug=True)
html templates:
home.html
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Welcome to the Home Page</h1>
<h2>Student Results</h2>
<table>
<thead>
<tr>
<th>Name</th>
<th>Course</th>
<th>Marks</th>
</tr>
</thead>
<tbody>
{% for student in students %}
<tr>
<td>{{ student.name }}</td>
<td>{{ student.course }}</td>
<td>{{ student.marks }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
Courses.html
<!DOCTYPE html>
<html>
<head>
<title>Courses</title>
</head>
<body>
<h1>Courses Offered</h1>
<ul>
<li>Math</li>
<li>Science</li>
<li>History</li>
<!-- Add more courses as needed -->
</ul>
</body>
</html>
Gallery.html
<!DOCTYPE html>
<html>
<head>
<title>Rankers Gallery</title>
<style>
.rankers-table {
border-collapse: collapse;
width: 100%;
}
.rankers-table th {
background-color: #f2f2f2;
}
.rankers-table tr:nth-child(even) {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<h1>Rankers Gallery</h1>
<table class="rankers-table">
<thead>
<tr>
<th>Name</th>
<th>Course</th>
<th>Marks</th>
</tr>
</thead>
<tbody>
{% for student in students %}
<tr>
<td>{{ student.name }}</td>
<td>{{ student.course }}</td>
<td>{{ student.marks }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
Output:
* Running on http://127.0.0.1:5000
* Running on http://127.0.0.1:5000/courses
* Running on http://127.0.0.1:5000/rankers_gallery
Post-Lab:
a) How can I handle different HTTP methods (GET, POST, etc.) in Flask? In Flask, you can handle different
HTTP methods by specifying them in the route decorator using the methods parameters.
app = Flask(__name__)
if request.method == 'POST':
# Handle POST requests here
data = request.form.get('data') # Access form data
return f'This is a POST request with data: {data}'
if __name__ == '__main__':
app.run(debug=True)
output:
* Running on http://127.0.0.1:5000/example