Python is a versatile and widely-used programming language, loved by developers for its simplicity and readability. Flask, a micro web framework for Python, is a popular choice for building web applications due to its flexibility, minimalism, and ease of use. Let's explore Flask in Python with some practical examples to help you understand its capabilities and how to apply it in real-world scenarios.

Before diving into the examples, ensure you have Python and Flask installed on your system. You can install Flask using pip: `pip install Flask`. Now, let's create our first Flask application and explore some key features.

Setting Up a Basic Flask Application
Begin by creating a new folder for your project and navigate into it. Inside this folder, create a new Python file (e.g., `app.py`) and a folder named `templates`. This folder will store your HTML templates. Flask looks for these templates in the `templates` directory by default.

Now, let's create our first application. Open `app.py` and add the following code:
```python from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello, World!" ```
Save the file, and run the application using `python app.py`. Visit

Defining Routes
In the code above, `@app.route("/")` is a decorator that binds a function (in this case, `hello`) to a URL. This is how Flask maps URLs to functions, making your application interactive.
Add another route to `app.py` to display a different message:

```python @app.route("/about") def about(): return "This is the about page!" ```
Now, visit
Using HTML Templates
Although Flask allows you to return plain text or strings from your routes, it's more common to return HTML templates. Create a new file named `index.html` in the `templates` folder, and add the following code:

```html
{{ message }}
```
Update the `hello` function in `app.py` to render this template:






![Learn Flask [2026] Most Recommended Tutorials](https://i.pinimg.com/originals/70/c3/0b/70c30b834f681afe86155783ec72f112.png)













```python from flask import Flask, render_template app = Flask(__name__) @app.route("/") def hello(): message = "Hello, World!" return render_template("index.html", message=message) ```
Passing Data to Templates
In the example above, `render_template()` takes the template name and any variables you want to pass to it. In the template, `{{ message }}` is replaced with the value of the `message` variable.
Let's pass a list of users to a template and loop through it:
Using for Loop in Templates
Add a new route and template to `app.py` and `templates` folder:
```python @app.route("/users") def users(): users = ["Alice", "Bob", "Charlie"] return render_template("users.html", users=users) ``` ```html
-
{% for user in users %}
- {{ user }} {% endfor %}
```
Visit
Using Flask Forms for User Input
Flask provides tools for handling forms and user input. Let's create a simple contact form:
Creating a Contact Form
Install Flask-WTF, a forms handling extension for Flask: `pip install Flask-WTF`. Add the following code to `app.py` and create a new template `contact.html`:
```python from flask import Flask, render_template, request, flash from flask_wtf import FlaskForm from wtforms import StringField, TextAreaField, SubmitField from wtforms.validators import DataRequired, Email app = Flask(__name__) app.config['SECRET_KEY'] = 'your_secret_key' class ContactForm(FlaskForm): name = StringField('Name', validators=[DataRequired()]) email = StringField('Email', validators=[DataRequired(), Email()]) message = TextAreaField('Message', validators=[DataRequired()]) submit = SubmitField('Submit') @app.route("/contact", methods=['GET', 'POST']) def contact(): form = ContactForm() if form.validate_on_submit(): flash('Message sent successfully!') return render_template("contact.html", form=form) ``` ```html
{{ message }}
{% endif %}
```Visit
Flask provides a robust foundation for building web applications in Python. By understanding Flask routing, templates, and form handling, you can create engaging and interactive web experiences. Explore the Flask documentation and experiment with other features like sessions, file uploads, and databases to expand your Flask knowledge. Happy coding!