Creating a Simple Web Application with Python and Flask

December 12, 2023

Flask is a popular web framework for building web applications with Python. It provides a simple and efficient way to create web applications, making it a favorite among developers. In this lesson, we will learn how to create a simple web application using Flask, including setting up routes, handling requests, and rendering templates.

What is Flask?

Flask is a micro web framework written in Python. It was developed by Armin Ronacher and released in 2010. It is designed to be lightweight, simple, and easy to use, allowing developers to focus on building their application rather than dealing with complex frameworks.

Setting Up Routes

The first step in creating a web application with Flask is setting up routes. Routes are URLs that will be used to access different pages or functions in our application. To set up a route, we will use the @app.route() decorator in our Python code.

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

In the above code, we have defined a route for the root URL /. This means that when a user visits our website, they will see the message Hello World!. We can also define routes for different URLs, such as /about or /contact.

Handling Requests

Now that we have set up our routes, we need to handle requests from users. This means that when a user visits a specific URL, our application will perform a certain action. For example, if a user visits /about, we may want to display information about our website or company.

To handle requests, we will use the @app.route() decorator again, but this time we will specify the HTTP method that we want to handle, such as GET or POST.

@app.route('/about', methods=['GET'])
def about():
    return 'This is our website!'

In the above code, we have defined a route for /about and specified that it should only handle GET requests. This means that if a user visits /about using any other HTTP method, they will receive an error.

Rendering Templates

In order to create a user-friendly web application, we will need to use templates to display information. Templates are HTML files that contain dynamic content, such as data from a database or user input. To render a template in Flask, we will use the render_template() function.

@app.route('/hello')
def hello():
    return render_template('hello.html', name='John')

In the above code, we have defined a route for /hello and used the render_template() function to render the hello.html template. We have also passed in a variable name with the value 'John', which can be accessed in the template.

Putting it All Together

Now that we have covered the basics of Flask, let’s put it all together and create a simple web application. First, we will need to install Flask using pip:

pip install Flask

Next, we will create a new Python file and import the necessary modules:

from flask import Flask, render_template

app = Flask(__name__)

Then, we can define our routes and functions:

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

@app.route('/about', methods=['GET'])
def about():
    return 'This is our website!'

@app.route('/hello')
def hello():
    return render_template('hello.html', name='John')

Finally, we can create our hello.html template:

<h1>Hello, {{ name }}!</h1>

Now, when a user visits our website, they will see the message Hello World!. If they visit /about, they will see This is our website!. And if they visit /hello, they will see Hello, John!.

Conclusion

In this lesson, we learned how to create a simple web application using Flask. We covered setting up routes, handling requests, and rendering templates. Flask is a powerful and flexible framework that allows us to quickly and easily build web applications with Python. With further practice and exploration, you can create even more complex and dynamic web applications with Flask.