Wednesday, September 5, 2018

Teach My Kids Python - How to create web service using Flask

Python is the language that helps you put together your idea faster than most other computer languages.  Micro-controller like Raspberry Pi also let you write in Python to control electronic
device such as robots, yes, robots...



Before getting to build a robot, this blog is to teach you how to build a simple web service using Python.  To begin, let starts with a well known Python language editor (the computer language editor is referred as "Integrated Development Environment, IDE in short).    The Python IDE can be downloaded from the following link.

Getting PyCharm

Once the IDE is installed, you can open the IDE.  The program you are going to write is to serve a  text in JSON format when an user open a link using web browser to your web service.

A JSON message have the following format:

{
    "glossary": {
        "title": "example glossary",
  "GlossDiv": {
            "title": "S",
   "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
     "SortAs": "SGML",
     "GlossTerm": "Standard Generalized Markup Language",
     "Acronym": "SGML",
     "Abbrev": "ISO 8879:1986",
     "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
      "GlossSeeAlso": ["GML", "XML"]
                    },
     "GlossSee": "markup"
                }
            }
        }
    }
}

JSON messages are widely exchanged between computer systems.  In this blog, you will also 
learn how to read JSON message from a text file stored in your computer.



Section 1


Create a Python project, name it "mocked_analytic_server", click the Create button 
when you are done.
 






Section 2


Create a requirements.txt file to specify the Flask library for your web service program.  
The flask library information can be found at this link: PyPI libraries. PyPI is the 
place where you find all kinds of libraries included controlling robots.






















Once you created requirements.txt, enter the following flask library you needed:
flask==1.0.2
At the time of this writing 1.0.2 is the latest version for the Flask library.
















The IDE should offer you to install the library.  So, please click on "Install requirement".
When it is done, you will see the following pop-up:











Section 3


Now create a new file again, this time named it as "sample.json", and enter any JSON 
you like.  For example:

{
 "age":1000,
 "name":"Dark Vader",
 "messages":["Luke", "I am your father"]
}

The correct format of the JSON message is important, so if you cannot type well, just copy
above example JSON text to sample.json.















Below screenshot is just an example, the JSON you entered can be different
from the screenshot:

































Section 4


On the the main program, create a Python file, name it "main.py":





















Then you have to type the following Python code.  Sorry, no copying this time.. You have 
to learn from mistakes:






















Section 5


It is the test to try out your program by running it. Right click on main.py on the
left panel, it will have a pop up:































Click on Run 'main', and you will see your web service is running on the lower panel:


















Then, click on the link http://127.0.0.1:5000.  It will launch your web browser, and
show the JSON message:










Your JSON message can be different from mine.

Section 6


To show it off to your friend, either 

1. Tell your friend to come to your home to look.
2. Ask your dad to configure your router, set port forward to your running
   web service running on port 5000

Number 2 is not recommended because there are tons of hackers out there looking
for opportunity to take over your computer.