Getting Started with Flask: A Python Web Framework Example
Flask is a popular, lightweight Python web framework that's perfect for small applications and APIs. It's easy to get started with Flask, and it's a great choice for prototyping and learning. In this article, we'll create a simple Flask application to demonstrate its core features.
Setting Up the Environment
Before we dive into the code, ensure you have Python and pip installed. Then, install Flask using pip:
pip install flask
Once installed, you're ready to create your first Flask application.

Creating a Basic Flask Application
Create a new file named app.py and add the following code:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Hello, World!"
if __name__ == '__main__':
app.run(debug=True)
Here's what's happening:
from flask import Flask: Imports the Flask class.app = Flask(__name__): Creates a new Flask web server.@app.route('/'): Decorator that maps the specified route ('/') to the following function.def home():: The function that handles the specified route. In this case, it returns a simple string.if __name__ == '__main__':: Ensures the server only runs if this script is executed directly (not imported).app.run(debug=True): Starts the Flask development server with debug mode enabled.
Running the Application
To run the application, simply execute the script:

python app.py
Then, open your browser and navigate to http://127.0.0.1:5000/ to see your Flask application in action.
Adding More Routes
Let's add a new route to our application. Update the app.py file as follows:
from flask import Flask, request
app = Flask(__name__)
@app.route('/')
def home():
return "Hello, World!"
@app.route('/greet/')
def greet(name):
return f"Hello, {name}!"
if __name__ == '__main__':
app.run(debug=True)
Here, we've added a new route /greet/{name} that accepts a variable name. The greet function uses this variable to return a personalized greeting.

Using HTML Templates
While Flask is great for simple applications, it's often more convenient to use HTML templates for rendering web pages. Let's create a simple template and update our application to use it.
Create a new folder named templates in the same directory as app.py. Inside this folder, create a new file named base.html with the following content:
<!DOCTYPE html>
<html>
<head>
<title>My Flask App</title>
</head>
<body>
<h1>Welcome to my Flask app!</h1>
<p>{{ message }}</p>
</body>
</html>
Now, update the app.py file to use this template:
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('base.html', message='Hello, World!')
@app.route('/greet/')
def greet(name):
return render_template('base.html', message=f"Hello, {name}!")
if __name__ == '__main__':
app.run(debug=True)
The render_template function renders the specified template with the provided context data.
Conclusion
In this article, we've created a simple Flask application that demonstrates the core features of this popular Python web framework. By following these examples, you should now have a solid understanding of how to get started with Flask and create basic web applications.






















