1967 - 991 - Undefined - Student Activity - Hands On Python Project
1967 - 991 - Undefined - Student Activity - Hands On Python Project
Activity
Student Activity: Hands-on Guided Project Using Python -
Flask Framework, Routes, Templates
Welcome to this hands-on guided project using Python! In this activity, you will explore the
Flask Framework, learn about Routes, work with Templates, and develop a simple web
project. This guide assumes no prior knowledge of web development or Python frameworks.
By the end of this session, you will have a solid understanding of how to build a basic web
application using Flask.
Examples:
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)
2. Flask with Debug Mode:
app = Flask(__name__)
@app.route('/')
def index():
return 'Debug Mode is On!'
if __name__ == '__main__':
app.run(debug=True)
app = Flask(__name__)
@app.route('/')
def custom_port():
return 'Running on a custom port!'
if __name__ == '__main__':
app.run(port=8080)
Examples:
1. Basic Route:
app = Flask(__name__)
@app.route('/')
def home():
return "Welcome to the Home Page!"
2. Multiple Routes:
app = Flask(__name__)
@app.route('/')
def home():
return "Home Page"
@app.route('/about')
def about():
return "About Page"
3. Dynamic Route:
app = Flask(__name__)
@app.route('/user/<username>')
def show_user_profile(username):
return f'User {username}'
Examples:
1. Basic Template:
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html', name="Alice")
app = Flask(__name__)
@app.route('/tasks')
def tasks():
task_list = ["Task 1", "Task 2", "Task 3"]
return render_template('tasks.html', tasks=task_list)
app = Flask(__name__)
@app.route('/status/<status>')
def status(status):
return render_template('status.html', status=status)
Step-by-Step Approach:
1. Set Up Flask:
Install Flask using pip: pip install flask
Create a new Python file (e.g., app.py ) and import Flask.
2. Define Routes:
Create routes for the home page (to display tasks) and a route to add new tasks.
3. Create Templates:
Use HTML templates to display the list of tasks and a form to add new tasks.
4. Handle User Input:
Use Flask’s request object to handle form submissions and add new tasks to the
list.
5. Display Tasks:
Use a template to loop through the list of tasks and display them on the home
page.
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html', tasks=tasks)
@app.route('/add', methods=['POST'])
def add_task():
task = request.form['task']
tasks.append(task)
return redirect(url_for('home'))
if __name__ == '__main__':
app.run(debug=True)
Template (home.html):
<!DOCTYPE html>
<html>
<head>
<title>To-Do List</title>
</head>
<body>
<h1>To-Do List</h1>
<ul>
{% for task in tasks %}
<li>{{ task }}</li>
{% endfor %}
</ul>
Explanation:
The home route renders the home.html template and passes the list of tasks to it.
The add_task route handles form submissions. When the user submits a new task, it
is added to the tasks list, and the user is redirected back to the home page.
It might be confusing at first to understand how routes work and how they map to
functions in your code. Practice by creating multiple routes and associating them with
different functions.
If you are new to HTML, working with templates might feel overwhelming. Start by
creating simple static HTML pages and gradually introduce dynamic content using
Jinja2.
Handling form submissions and user input can be tricky. Make sure you understand
how Flask’s request object works and how to use it to capture data from forms.
7. Conclusion
In this session, we covered the basics of the Flask framework, routes, templates, and how to
build a simple web project. By now, you should have a good understanding of how to:
I encourage you to experiment with the code and try building your own projects, such as a
simple calculator or a file organizer. The more you practice, the more comfortable you will
become with Flask and web development in general.
8. Next Steps
Try adding more features to the To-Do List application, such as the ability to delete
tasks or mark them as completed.
Explore Flask’s documentation to learn about more advanced features like database
integration and user authentication.
Thank you for participating in this activity! If you have any questions or need further
clarification, feel free to ask. Happy coding!