Search code examples
pythonsocketsdeploymentpublish

How (in what form) to share (deliver) a Python function?


The final outcome of my work should be a Python function that takes a JSON object as the only input and return another JSON object as output. To keep it more specific, I am a data scientist, and the function that I am speaking about, is derived from data and it delivers predictions (in other words, it is a machine learning model).

So, my question is how to deliver this function to the "tech team" that is going to incorporate it into a web-service.

At the moment I face few problems. First, the tech team does not necessarily work in Python environment. So, they cannot just "copy and paste" my function into their code. Second, I want to make sure that my function runs in the same environment as mine. For example, I can imagine that I use some library that the tech team does not have or they have a version that differ from the version that I use.

ADDED

As a possible solution I consider the following. I start a Python process that listen to a socket, accept incoming strings, transforms them into JSON, gives the JSON to the "published" function and returns the output JSON as a string. Does this solution have disadvantages? In other words, is it a good idea to "publish" a Python function as a background process listening to a socket?


Solution

  • You have the right idea with using a socket but there are tons of frameworks doing exactly what you want. Like hleggs, I suggest you checkout Flask to build a microservice. This will let the other team post JSON objects in an HTTP request to your flask application and receive JSON objects back. No knowledge of the underlying system or additional requirements required!

    Here's a template for a flask app that replies and responds with JSON

    from flask import Flask, request, jsonify
    
    app = Flask(__name__)
    
    @app.route('/', methods=['POST'])
    def index():
        json = request.json
        return jsonify(your_function(json))
    
    
    if __name__=='__main__':
        app.run(host='0.0.0.0', port=5000)
    

    Edit: embeded my code directly as per Peter Britain's advice