DEV Community

Siva
Siva

Posted on • Originally published at Medium on

Building a Basic Web Service with Flask

Flask is a popular Python web framework that is used to develop web applications. It is simple, lightweight, and flexible, making it an excellent choice for building small to medium-sized web applications. In this blog post, we’ll walk through the steps to create a basic web service using Flask.

Step 1: Install Flask

To use Flask, you’ll first need to install it. You can do this using pip, the Python package installer, by running the following command in your terminal or command prompt:

pip install flask
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a Flask Application

Once Flask is installed, create a new Python file for your application. In this example, we’ll call the file app.py. At the top of the file, you'll need to import Flask and create a new instance of the Flask class:

from flask import Flask

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

The __name__ parameter tells Flask where to find resources such as templates and static files. In this case, we're telling Flask to use the current module as the source of these resources.

Step 3: Define a Route

Routes are used to map URLs to functions in your application. To create a new route, use the route() decorator and specify the URL that you want to map to the function. For example:

@app.route('/')
def home():
    return 'Hello, world!'
Enter fullscreen mode Exit fullscreen mode

In this example, we’ve created a route for the root URL (/) and defined a function home() that returns the string 'Hello, world!'. This function will be called whenever a user navigates to the root URL.

Step 4: Run the Application

To run your Flask application, add the following code to the bottom of your app.py file:

if __name__ == ' __main__':
    app.run()
Enter fullscreen mode Exit fullscreen mode

This code will start the Flask development server and allow you to access your application by visiting http://localhost:5000 in your web browser.

Step 5: Add Functionality

With your Flask application up and running, you can start adding functionality to it. For example, you might want to create a form that allows users to submit data to your application. Here’s an example of how you might create a simple form in Flask:

├── main.py
└── templates
    └── home.html

from flask import Flask, request, render_template

app = Flask( __name__ )

@app.route('/', methods=['GET', 'POST'])
def home():
    if request.method == 'POST':
        name = request.form['name']
        return f'Hello, {name}!'
    else:
        return render_template('home.html')

curl -X POST -d "name=John King" http://localhost:5000/
Enter fullscreen mode Exit fullscreen mode

In this example, we’ve added a new route accepting GET and POST requests. If the request method is POST, we retrieve the value of the name field from the submitted form and return a personalized greeting. If the request method is GET, we render a template called home.html that contains the form.

Step 6: Deploy Your Application

Once you’ve built your Flask application, you’ll need to deploy it to a web server so that others can access it. There are many ways to deploy a Flask application, including using a cloud hosting provider or a dedicated server. Some popular options for deploying Flask applications include Heroku, Google Cloud Platform, and Amazon Web Services.

In conclusion, Flask is a powerful and flexible web framework that makes it easy to build and deploy web services in Python. By following the steps outlined in this blog post, you can create a simple Flask application that accepts user input and returns the personalized output. With a little bit of creativity and ingenuity, you can use Flask to build powerful and scalable web services that can be used by

Demo — Code Walkthrough

https://medium.com/media/cb60d778c7a01553339430f6fbc3c1f1/href

Top comments (0)