Flask helloworld!


REST API is commonly use in web-services, thus here I'm going to use Flask, an python light-weight web development framework to implement it.
Before we can start doing something fancy we first need to install all the required software and make sure everything is running ok 💪

Instruction

here are the step to do so;

1.Install pip
well you need pip to install many packages. so if you don't have just install it.
shell command line in Linux:

sudo apt install python3-pip

2. Install Flask
Well it's obvious that we need to install flask to be able to use it ;)


pip3 install flask

3. Let's write the code!
here is the code and I name it : helloFlask.py


from flask import Flask

# we call the contructor and pass the name of this python file into it
app = Flask(__name__)

#here we define our app(or method) and the url that it will be running 
@app.route('/') # '/' means this method will be run on the home page
def hello_world():
    return "hello world"
#if this file is called the we run the app!
if __name__ =="__main__":
    app.run()

4. Run It
This part is a bit tricky (in my opinion) because it's not like a normal python script that we can just run it, here's how you do it. Actually, you can just run it the same way as normal python script. 

4.a Run as Python script


python helloFlask.py


4.b Run using Flask command
Well I found it in tutorial. Just for the record, you can do it this way but why bother, calling it using python is a lot more easier.

4.b.1 Export FLASK_APP Environment variable
It's a environment variable that will be use later by flask and we need to tell it which python file we going to use:

export FLASK_APP=helloFlask.py

4.b.2 run using flask
just as the name said :3


flask run

Done!!

here is the result I get:

 * Serving Flask-SocketIO app "helloFlask.py"
 * Forcing debug mode off
 * Serving Flask app "helloFlask.py"
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [16/Feb/2019 16:46:41] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [16/Feb/2019 16:46:41] "GET /favicon.ico HTTP/1.1" 404 -

Explanation

so, let's explain a bit;

when we execute flask run, flask setting up a web for us and it can be access by IP address highlighted above.

Now open the Browser, this we act as our client that want to access the web we created which can be called a server. Since we running our website on the localhost we can input the IP address directly.

When we enter the IP address into the browser, it will send and HTTP request to the settled up website. you can see that our website got a "Get" HTTP request on the following line in the command line;

127.0.0.1 - - [16/Feb/2019 16:46:41] "GET / HTTP/1.1" 200 -

there are many type of HTTP request but what commonly use in Rest API is this following request:

  • GET - the client want to receive some data from the server.
  • POST- the client want to send some data for the server to do some service.

And then the server will execute specific part of our code. which is determined by the following part of our code;


@app.route('/')

meaning the following method will be execute if the client request HTTP request by a "homepage" address or an IP address without any more directory behind.
In this example the client input "http://127.0.0.1:5000/" 

then the method "def hello_world():" is execute and the return value of this method will be sent back to the client.

Deemarc Burakitbumrung

A software develper who graduated from Electronic and Communication Engineer. Have an interest in the field of programming and embedded system.

No comments:

Post a Comment