Setting up the Python Application

This entry is part 4 of 12 in the series Kubernetes and everything else

To start the Python application we need to have Python3 and Pip installed (their environment variables need to be set up as well).

Installing Dependencies

Navigate in the CMD to the directory sa-logic/sa (repo) and type the following command:

$ python -m pip install -r requirements.txt
$ python -m textblob.download_corpora

Starting the app

Pip installs the dependencies and we are ready to start the application by executing the following command:

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

Which means that our application is running and listening for HTTP Requests at the port 5000 on localhost.

Inspecting the Code

Let’s investigate the code to understand what is happening in the SA Logic python application.

  1. Instantiate a Flask object.
  2. Defines the path at which a POST request can be made.
  3. Extract the “sentence” property from the request body.
  4. Instantiate an anonymous TextBlob object and get the polarity from the first sentence. (We have only one).
  5. Return the response with the body containing the sentence and the polarity to the caller.
  6. Run the flask object app to listen for requests on localhost:5000.

The services are set up to communicate with each other, give them a try before continuing on!

Fig. 1. Microservice architecture set up

Let’s recapitulate what we did; We started all the microservices running in different ports, we configured each to communicate with each other, and this was a tedious process, but it worked, so why Kubernetes you ask? Because our application is not scalable, it’s not resilient and not maintainable. Stay tuned as in the next article, we will go over how to start the services in Docker Containers, as it is a prerequisite to being able to run them in Kubernetes Cluster.

Container-ization of the Services >>
If you enjoyed the article, please share and comment below!
  • Олег Тихонов

    Hello, Rinor! Fist of all thanks you for this graceful articles. It’s perceived easy. Could you help me with one obstacle – on the step when I am trying to check service to get polarity value. Namely, typed text and then clicked “send” i have faced with error in the web console such as

    Source map error: Error: NetworkError when attempting to fetch resource.Resource URL: http://localhost/static/js/main.c2de1388.js

    (screen is attached)

    And got error from python console that - - [06/May/2020 21:45:40] "[35m[1mPOST /analyse/sentiment HTTP/1.1[0m" 500 -
    [2020-05-06 21:45:48,321] ERROR in app: Exception on /analyse/sentiment [POST]