0% found this document useful (0 votes)
5 views6 pages

PF 9

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

PF 9

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Course:pfsd

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.

from flask import Flask

app = Flask(__name__)

@app.route('/')

def hello_world():

return '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.

from flask import Flask, render_template

app = Flask(__name__)

students = [

{"name": "Alice", "course": "Math", "marks": 90},

{"name": "Bob", "course": "Science", "marks": 85},

{"name": "Charlie", "course": "History", "marks": 78},

]
@app.route('/')

def home():

return render_template('home.html', students=students)

@app.route('/courses')

def courses():

return render_template('courses.html', students=students)

@app.route('/rankers-gallery')

def rankers_gallery():

return render_template('rankers_gallery.html', students=students)

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, .rankers-table td {


border: 1px solid #ddd;
padding: 8px;
text-align: left;
}

.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.

from flask import Flask, request

app = Flask(__name__)

@app.route('/example', methods=['GET', 'POST'])


def example():
if request.method == 'GET':
# Handle GET requests here
return 'This is a GET request.'

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

You might also like