How To Create A Simple Flask API In Python

Hello Learners…

Welcome to my blog…

Table Of Contents

  • Introduction
  • What is Flask?
  • How To Create A Simple Flask API In Python
  • Summary
  • References

Introduction

In this post, we discuss the Flask Framework of Python Programming. Flask is used to create an API. Here we simplify the process of How To Create A Simple Flask API In Python.

What is Flask?

Flask is a lightweight Python web framework that provides useful tools and features for creating web applications.

Flask is easy to get started with and it can be used to build a wide range of web applications, from simple ones to complex, production-ready ones.

Official Flask Site: https://flask.palletsprojects.com/en/2.3.x/

How To Create A Simple Flask API In Python

Requirements

  • Basic knowledge of Python Programming Language
  • Python Installed on your system

I am creating this tutorial using Linux OS. Based on your OS there are a few changes but I hope you understand the concepts.

My Current Python Version Is:

python version check

If you are working with Python virtual environment then first activate it.also, we can do this directly without using a virtual environment.

Here we are using Python virtual environment, It is always good practice to use a virtual environment.

Here we use VSCode but you can use any Code editor which is comfortable for you.

To use Flask, we have to install it first. We can do this by using pip, the Python package manager:

Open the terminal and run the below command.

pip install flask

Then, We create a simple “Hello, World!” application with Flask by creating a file called app.py and adding the following code to it:

from flask import Flask
app = Flask(__name__)

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

if __name__ == '__main__':
    app.run()
install flask using pip

To run this application, we can use the following command:

python3 app.py
run flask api with python3 app.py

We get an URL http://localhost:5000 in our terminal.

Now opens this URL in any web browser as we can see below.

flask api output in browser

Summary

This is the simple way to create a Flask API. We will create many complicated Flask APIs in the upcoming posts. stay updated with us.

Also, you can refer to other posts related to Flask,

Happy Learning And Keep Learning…

Thank You…

References

Leave a Comment