HOW TO: develop DataAnalytic in Python and manage them via IOT App

×

Warning message

  • You can't delete this newsletter because it has not been sent to all its subscribers.
  • You can't delete this newsletter because it has not been sent to all its subscribers.

VIDEO training in Italian: https://www.snap4city.org/download/video/Collini2.mp4

Please note that only users of Role AreaManager are entitled to ask for the Python Access on cloud. All the AreaManagers can develop Python tasks and use from IOT App, while only authorized can debug Python processes on cloud exploiting Smart City API. See the instructions on menu item “Development Tools”.

As a first step you have to create a Python program according to the following guideline. The Guideline consist of an example that allow you to exploit Flask library for API construction in the context of Snap4City docker container and service. This example is only for the system level, another example will be provided for the Smart City API access.

The following example in python is accepting as parameters a couple of variables called “val1” and “val2”, integer and float respectively. The code can be debugged on your local computer for the parameter passing by using

http://localhost:8080/scriptBello?val1=3&val2=3

the result of that call is:

import json

import flask

import os

import sys

app = flask.Flask(__name__)

os.environ["FLASK_APP"]= __name__ + ".py"

append_services = []

 

@app.route('/scriptBello'methods=['GET''POST'])

def scriptBello():

    results = []  # Temp for result to return to html

    global append_services

    try:

        if flask.request.method == 'GET':

            valore1 = int(flask.request.values.get('val1'))

            valore2 = float(flask.request.values.get('val2'))

            print("The values: ",valore1," e ",valore2)

            append_services.append ("The values:---")

            if valore1 < valore2:

                print ("Ergo: ",valore1+valore2)

                append_services.append ("Ergo:---")

                return str(valore1+valore2)

            else:

                print ("Otherwise: ", valore1 * valore2)

                append_services.append ("Otherwise:---")

                return str(valore1 * valore2)

        else:

            yourarg = "Nothing"

            print ("Nothing: ")

            append_services.append ("Nothing:---")

    #        return yourarg

    # This part for generate HTML for return TO BE PLACEDD WHERE NEEDED

            html = ''

            for i in results:

                html += '<h2>Ip: ' + i[] + '</br>'

                html += '<h3>From ' + i[1][] + ' to ' + i[1][1]

            return html

 

    except Exception as e:

        print ("Error2: "+str(e))

        message = "Error2: "+str(e);

        return message

 

if __name__ == "__main__":

    app.run(host='0.0.0.0'port=8080)

 

 

If you put some errors in the parameters such as:

http://localhost:8080/scriptBello?val1=3.4&val2=3

the result of that call is:

 

Creation of the IOT App managing and Exploiting the Data Analytic

Thus, once created the Python and tested you are ready to upload on cloud and manage it by sending specific parameters. So that you can schedule the execution and use in multiple instances, etc.

The first step is to create an IOT App or use it in flow of any your IOT App. In the following a minimal flow is presented. Once created the flow:

The block Python.data.anaytic is part of the Snap4City Advanced library published since July 2020. Your IOT App has to be red in the list of your IOT App to have that internally loaded. If you have a previous version you can use the block update to pass at the new version. If you have problem please inform us to solve them.

Open the Python.data.anaytic block to setup according to the script you have create in Python. In the following example we have created a script called FirstTesrt3.py (which you can find below in the text of  this web page). The script contains the name of the entry point of the call.

PLEASE NOTE THAT. if you need from the python file to:

  • use special library please ask us to add them to the image of the container
  • exploit additional files for loading you can:
    • put them as static values into the py code
    • put in zip file. Actually you can upload instead of the .py file a ZIP file with data files and your python has to be named "daScript.py" mandatory. from the Python file daScript.py you can use the uploaded files referring them from "/data/file.txt" for the file named file.txt into your zip. Please note that there is limitation about the zip of the files which can be loaded in this manner into the container that is light, typically few Mbyte max in total. It is supposed that you use API to get data, and these files are only used to pass at the script the parameters of the machine learning algorithm that are too large to be passed via json message into the IOT App.

**BEFORE** clicking on DONE, you have to create the container. To this end, you have to click on te button “Create Python Data Analytic” in the setting of the block. All is succesful if the wolloing message apper.

The function block into the IOT Application is preparing the JSON for the invocation of the Data Analytic services in Python as:

var pay = {};

pay.val1 = 8;

pay.val2 = 11;

msg.payload=pay;

return msg;

The created Data Analytic in Python is a container, and appears for you into the list of IOT Applications as follows. Please note that if you cancel the block from the IOT App and deploy the Data Analytic will also disappear. If not, please delete the obsolete Data Analytic manually to keep your memory space clean 😊

In fact, in the following you see my “python example” with the IOT App and the “ScriptBello”, data analytic in Python (see logo and icon to recognize).

Thus if executed by Injection you obtain:

If the variable passed is wrong as above (a float at the place of an integer as expected) you get:

This means that you can communicate your errors in the debug view of the IOT App when needed. Please be careful in creating your code in a way that can report back the status of the  process.

Appendix the code in Python as image from Visual Studio: