Flask

Creating a Basic Flask Web Page


Introduction

Flask is a Python micro web framework that allows you to build web applications quickly and with minimal boilerplate code. In this guide, we'll walk you through the process of creating a basic Flask web page. You'll learn how to set up a Flask application, create routes, and render HTML templates to display content.

Step 1: Setting Up Your Flask Application

Before you can create a Flask web page, you need to set up your Flask application. Here's a minimal example:

from flask import Flask
app = Flask(__name)
        

Step 2: Creating a Route

Create a route that defines the content of your web page. Routes in Flask are functions that are executed when a specific URL is accessed. Here's an example of a route that displays a simple `Hello, World!` message:

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

Step 3: Running Your Flask Application

To run your Flask application, add the following code at the end of your script:

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

Now, you can run your application with the command python your_app.py and access your basic Flask web page in a web browser.

Step 4: Accessing Your Flask Web Page

Open a web browser and navigate to http://localhost:5000/ to view your Flask web page. You'll see the `Hello, World!` message displayed in the browser.

Conclusion

Creating a basic Flask web page is a simple but essential step in web development. You've learned how to set up a Flask application, create a route, and display content on a web page. You can further enhance your web page by adding templates, forms, and more complex routes as your project grows.

Written by Surfside Media

Senior Full Stack Developer specializing in Web Technologies.