Think of Flask as a lightweight “framework” for building web applications using Python. A framework provides you with a set of tools and guidelines that make it easier to create web applications without having to build everything from scratch. Flask is known for being “micro,” meaning it gives you the essentials while allowing you the flexibility to choose other components you might need.
Basic Concepts in Web Development
Before we write any Flask code, let’s understand a couple of fundamental web development concepts:
- Client-Server Architecture: When you visit a website, your web browser (like Chrome, Firefox, Safari) acts as the client. It sends a request to a server (a computer hosting the website’s files). The server then processes your request and sends back a response, which your browser then displays as the webpage you see.
- Routes: In web development, a route is a specific URL path that your web application responds to. For example, when you go to
www.example.com/about,/aboutis the route. Flask needs to know what Python code to run when a user visits a particular route.
Your First Flask Application (“Hello, World!”)
Let’s create a very simple Flask application. Don’t worry if some of the terms seem new right now; we’ll break them down.
First, you’ll need to make sure you have Flask installed. If you don’t, you can open your terminal or command prompt and type:
pip install Flask
Now, create a new Python file (let’s call it app.py) and paste the following code into it:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)
Let’s break down this code step by step:
from flask import Flask: This line imports theFlaskclass from theflasklibrary. We need this class to create our web application.app = Flask(__name__): This line creates an instance of theFlaskclass.__name__is a special Python variable that represents the name of the current module. This is a standard practice when initializing a Flask application.@app.route('/'): This is a decorator. Decorators in Python are a way to modify or enhance functions. In Flask, the@app.route('/')decorator tells Flask that the function immediately following it (hello_world()) should be executed when a user visits the root URL of our application (i.e., just/).def hello_world():: This defines a function namedhello_world. This function will be called when the user visits the/route.return 'Hello, World!': Inside thehello_worldfunction, we are returning the string'Hello, World!'. In a web application, this string will be sent back to the user’s web browser as the content of the webpage.if __name__ == '__main__':: This is a common Python construct. It ensures that the code inside this block only runs when the script is executed directly (not when it’s imported as a module into another script).app.run(debug=True): This line starts the Flask development server.app.run()is the method that runs the application.debug=Trueenables debugging mode. This is very helpful during development because it will automatically reload the server when you make changes to your code and will provide more detailed error messages if something goes wrong. Remember to turndebug=Falsewhen you deploy your application for production.
Your Next Task (15-20 minutes):
- Install Flask: If you haven’t already, open your terminal or command prompt and run
pip install Flask. - Create
app.py: Create a new text file namedapp.pyand paste the code above into it. - Run the application: Open your terminal or command prompt, navigate to the directory where you saved
app.py, and run the command:python app.py - Open your web browser: Once the server starts (you should see some output in your terminal saying something like “Running on http://127.0.0.1:5000/“), open your web browser and go to the address
http://127.0.0.1:5000/. You should see the text “Hello, World!” displayed on the page.

Leave a comment