Create Python web app using Flask Framework

We will be leaning how to create a web application using python flask framework in series of articles, Now in this post I will show you how to set up Flask frame work for web development using python and run a simple code to check the set up.

  1. Why do we choose python over other tech stacks?
    • Python is simple and lightweight. You can focus on your design and logic’s rather than the code complexity.
    • It is open-source and User friendly data structure and
    • Compatible with all the operating systems.
  2. Why do we choose the Flask Framework for web development?
    • Flask is a micro web development framework where it uses RESTful request dispatching.
    • It also has its own development server and debugging tool well-integrated for unit testing.

Let’s jump into development. Here the shell scripts are used for mac-os. It might differ for windows.

python –version

    • Choose your IDE. Here we are choosing VS Code https://code.visualstudio.com/download
    • Install pip with the below command. It is a utility to manage PyPI package installations from the command line

sudo easy_install pip

    • Install flask library using the below command

pip install flask

  • Let’s create a folder with the project name. Say flask_app

mkdir flask_app – Creates a folder

cd flask_app – Bash into the folder

  • Create a python file. Say app.py

cat > app.py – Creates new file in editable mode

  • Paste the below code snippet,
# app.py
# import flask
from flask import Flask

# create an app instance
app = Flask(__name__)

# at the end point /
@app.route("/")
def hello():
    # which returns "hello world"
    return "Hello World!"
    
# on running python app.py
if __name__ == "__main__":
    # run the flask app
    app.run()
  • Run the application with the below command. Flask runs on local server 5000 as default

python app.py

Now our flask web application is up and running! You will see the below command once the server is running

* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Once you click on http://127.0.0.1:5000/ you will see the return response from your flask app. Hello World!

local server showing Hello world text

 

Leave a Comment