Flask, a lightweight and flexible Python web framework, empowers developers to build web applications with ease and simplicity. In this beginner-friendly blog post, we’ll walk through the process of setting up a basic web application using Flask, from installation to creating routes and rendering templates.
Step 1: Installation
Before we can get started with Flask, we need to install it. You can install Flask using pip, Python’s package manager:
pip install Flask
Step 2: Creating a Flask Application
Let’s create a simple Flask application. Create a new Python file, such as app.py
, and import the Flask module:
from flask import Flask
app = Flask(__name__)
Step 3: Defining Routes
In Flask, routes define the URLs that our application will respond to. Let’s create a simple route that displays a “Hello, World!” message:
@app.route('/')
def index():
return 'Hello, World!'
Step 4: Running the Application
To run the Flask application, we need to add a conditional block to check if the script is being run directly:
if __name__ == '__main__':
app.run(debug=True)
Now, you can run your Flask application by executing the Python script:
python app.py
Step 5: Accessing the Application
Once your Flask application is running, open a web browser and navigate to http://localhost:5000
to see the “Hello, World!” message displayed.
Step 6: Rendering Templates
Flask allows us to render HTML templates to create dynamic web pages. Let’s create a simple HTML template and render it using Flask:
- Create a new directory named
templates
. - Inside the
templates
directory, create a file namedindex.html
with the following content:
<!DOCTYPE html>
<html>
<head>
<title>Flask Example</title>
</head>
<body>
<h1>Hello, Flask!</h1>
</body>
</html>
- Update the Flask route to render the HTML template:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
Now, when you access the application in your web browser, you should see the “Hello, Flask!” message rendered from the HTML template.
Conclusion
Congratulations! You’ve successfully set up a basic web application using Flask. From installation to defining routes and rendering templates, you’ve learned the fundamental steps to get started with Flask development. As you continue your journey with Flask, explore its rich ecosystem of extensions, dive into more advanced features, and unleash the full potential of web development with Python. Happy coding!