Python Flask: A Powerful Micro Web Framework
Flask is a micro web framework written in Python. It is designed to be simple, lightweight, and easy to use, making it a popular choice for building web applications and APIs. In this article, we will explore some of the key features of Flask and demonstrate how to use them with practical examples.
1.Installation and Setup:
To get started with Flask, you first need to install it using pip. Open your terminal or command prompt and run the following command:
pip install Flask
Once Flask is installed, you can create a new Python file for your web application.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, World!'
2. Routing and URL Handling:
Flask uses decorators to define routes for different URLs. Here’s an example of how to define routes for different pages:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Welcome to the Home Page!'
@app.route('/about')
def about():
return 'This is the About Page.'
@app.route('/contact')
def contact():
return 'Contact us at contact@example.com'
3. Rendering HTML Templates:
Flask allows you to render HTML templates easily. First, create a templates folder in your project directory and add an HTML file:
<!-- templates/index.html -->
<!DOCTYPE html>
<html>
<head>
<title>Flask App</title>
</head>
<body>
<h1>Hello, Flask!</h1>
<p>This is a simple Flask web application.</p>
</body>
</html>
Next, update your app.py to render this template:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
4. Handling Forms:
Flask makes it easy to handle HTML forms. Let’s create a simple contact form:
<!-- templates/contact.html -->
<!DOCTYPE html>
<html>
<head>
<title>Contact Us</title>
</head>
<body>
<h1>Contact Us</h1>
<form action="/submit" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="message">Message:</label>
<textarea id="message" name="message" required></textarea>
<input type="submit" value="Submit">
</form>
</body>
</html>
Then, handle the form submission in app.py:
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/contact', methods=['GET', 'POST'])
def contact():
if request.method == 'POST':
name = request.form['name']
email = request.form['email']
message = request.form['message']
# Process the form data (e.g., send an email)
return f'Thank you, {name}! We have received your message.'
return render_template('contact.html')
5. Handling Static Files:
Flask also allows you to serve static files like CSS, images, and JavaScript. Create a static folder in your project directory and add your static files there.
To link a CSS file, for example:
<!-- templates/index.html -->
<!DOCTYPE html>
<html>
<head>
<title>Flask App</title>
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
</head>
<body>
<h1>Hello, Flask!</h1>
<p>This is a simple Flask web application.</p>
</body>
</html>
In this article, we covered the basics of Python Flask and demonstrated some essential features with practical examples.
Flask’s simplicity and flexibility make it an excellent choice for developing web applications and APIs.
Happy coding!