Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is flask in Python

Getting to Know Flask: A Beginner's Guide

When you're just starting out in the world of programming, diving into web development can be both exciting and overwhelming. One of the tools you'll likely come across is Flask, a lightweight web application framework written in Python. But what does that mean? Let's break it down.

What is a Web Application Framework?

Imagine you're building a house. Instead of creating every single piece from scratch, you start with a pre-made foundation and framework that shapes the structure of your house. Similarly, a web application framework provides a foundation and components to help you build web applications more efficiently.

Enter Flask

Flask is like a minimalist's toolkit for web development. It gives you the essentials to create a web application but stays out of your way when you want to design it your way. Think of it as giving you the walls and roof of your house, but letting you choose the color of the paint, the style of the doors, and the type of flooring.

Installing Flask

Before you can start using Flask, you need to install it. If you have Python installed on your computer, you can add Flask using a tool called pip, which is a package installer for Python. Open your command line or terminal and type the following:

pip install Flask

This command downloads Flask and prepares it for use in your projects.

Your First Flask Application

Let's jump straight into creating a simple Flask application. Here's a basic example:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return 'Hello, World!'

if __name__ == '__main__':
    app.run(debug=True)

Copy this code into a file named app.py. This small program is a complete Flask web application. Let's go through it piece by piece.

Importing Flask

from flask import Flask

This line imports the Flask class from the Flask package. You can think of a class as a blueprint for creating objects, and in this case, we're using it to create our application.

Creating the Application Instance

app = Flask(__name__)

Here, we create an instance of the Flask class. The __name__ variable in Python represents the name of the module being used, and it helps Flask know where to look for resources like templates and static files.

Defining a Route

@app.route('/')
def home():
    return 'Hello, World!'

In web development, a route is a path that leads to a specific function in your application. This function is called when the route is accessed. In our example, the @app.route('/') decorator tells Flask that the function home() should be called when the root URL ('/') is accessed. When you visit the root of your website, it will simply display "Hello, World!".

Starting the Application

if __name__ == '__main__':
    app.run(debug=True)

This block checks if you're running the script directly (not importing it from another one) and then calls app.run() to start the application. The debug=True argument allows you to see detailed error messages in your browser if something goes wrong. It also automatically reloads your application when you make changes to the code, which is very handy during development.

Running Your Flask Application

To see your application in action, save the app.py file and run it by typing the following command in your terminal:

python app.py

Your terminal will show you an address (usually http://127.0.0.1:5000/). Open that address in your web browser, and you should see the "Hello, World!" message.

Expanding Your Application

Flask is simple, but it's also powerful. You can create multiple routes to handle different parts of your website. For example:

@app.route('/about')
def about():
    return 'This is the about page.'

Adding this to your app.py file creates a new route that will display a message when you navigate to http://127.0.0.1:5000/about.

Templates and Static Files

As your application grows, you'll want to serve more than just text. Flask allows you to use templates to render HTML pages. Let's create a basic HTML template:

  1. Make a folder named templates in the same directory as your app.py.
  2. Inside the templates folder, create a file named index.html with the following content:
<!doctype html>
<html>
<head>
  <title>Home Page</title>
</head>
<body>
  <h1>Welcome to My Flask App</h1>
</body>
</html>

Now, update the home function in your app.py to render this template:

from flask import render_template

@app.route('/')
def home():
    return render_template('index.html')

When you visit the root URL, Flask will serve the index.html file, displaying the welcome message in an HTML page.

Conclusion: The Flask Journey

Starting with Flask can be likened to learning how to cook with a basic set of utensils. It might not have all the fancy gadgets of a professional kitchen, but it has everything you need to create something delicious. As you grow more comfortable with Flask, you'll discover that its simplicity is its greatest strength. You can add ingredients (extensions) as needed, and you're always in control of the final dish (your web application).

So, take the first step and start experimenting. Build a personal blog, a portfolio, or a small business website. With Flask, the web is your playground, and you're the architect of your digital space. Enjoy the process of learning, building, and creating. Happy coding!