DEV Community

Romulo Gatto
Romulo Gatto

Posted on

Building a Simple Web Application with Flask

Building a Simple Web Application with Flask

Flask is a lightweight web framework written in Python that allows you to build web applications quickly and easily. In this guide, we will walk through the steps of building a simple web application using Flask.

Prerequisites

Before starting, ensure that you have the following installed on your machine:

  • Python (version 3 or above)
  • Flask (install via pip install flask)

Step 1: Setting up the Project Structure

To get started, create a new project directory on your machine. Open a terminal or command prompt and navigate to the desired location for your project.

Once in the project directory, create an additional directory called "app". This will serve as our main application folder.

my-web-app/
└── app/
Enter fullscreen mode Exit fullscreen mode

Step 2: Creating and Configuring the Flask Application

Now let's set up our Flask application file. Inside the "app" directory, create a new Python file named app.py.

Open app.py in your code editor and import the necessary modules:

from flask import Flask
Enter fullscreen mode Exit fullscreen mode

Next, create an instance of the Flask class:

app = Flask(__name__)
Enter fullscreen mode Exit fullscreen mode

With our application instance created, we can start defining routes. Routes are URLs that users can visit within our web application.

For example, let's create a simple route that displays "Hello World!" when visiting the homepage:

@app.route('/')
def hello():
    return 'Hello World!'
Enter fullscreen mode Exit fullscreen mode

Save your changes to app.py.

Step 3: Running Your Web Application

To run your web application locally for testing purposes, open a terminal or command prompt in your project root directory (my-web-app) and execute:

$ export FLASK_APP=app.py       # Linux/MacOS
$ set FLASK_APP=app.py          # Windows PowerShell
$ flask run
Enter fullscreen mode Exit fullscreen mode

Visit http://localhost:5000 in your web browser, and you should see "Hello World!" displayed.

Step 4: Building Additional Routes

Now that we have a basic route working, let's add more functionality to our web application. You can define as many routes as needed for your specific project.

Here's an example of a route that returns the current date:

from datetime import datetime

@app.route('/date')
def display_date():
    now = datetime.now()
    return f"The current date is {now.strftime('%Y-%m-%d')}"
Enter fullscreen mode Exit fullscreen mode

By visiting http://localhost:5000/date, you will see the current date displayed.

Feel free to continue adding more routes based on your project requirements.

Conclusion

Congratulations! You have successfully built a simple web application using Flask. Now you can take this foundation and expand it with additional features or functionalities according to your needs.

Remember, Flask offers endless possibilities for building dynamic web applications. Explore its documentation and experiment with different functionalities to unleash the full potential of Flask!

Top comments (0)