Skip to content
Advertisement

Is there a way to host a python function (in azure or elsewhere) with list input/output?

I’m getting into Azure a little bit, and have been through their tutorials here. This, however, seems like it’s only good for a string query to the HTTPTrigger. I’m wondering if there is a way to do something like:

def main(req:HttpRequest) -> HttpResponse:
    input_list = req.params.get("input_list")
    # DO SOMETHING INTERESTING WITH LIST
    output_list = interesting_function(input_list)
    return HttpResponse(output_list)

I’m not too familiar with how Azure functions work, or if this is even possible with their framework (or at all anywhere). My idea is hosting a function that I can call from python (as long as I have an internet connection) that will take a list as an input and do some operations on that list, and return it to me.

Advertisement

Answer

Azure Functions is a good place to start, though there are other ways. Starting with Azure Functions is fairly easy, which given you mentioned code, I suspect you’re after (e.g. Azure Logic Apps can be an alternative, if you prefer Design-driven approach), both are “serverless” (Azure PaaS).

To the other part of your question, taking a “list” – truly the “input binding” is what determines the structure of the request sent. Input mentioned in your message is via an HTTP Trigger, that is someone calls your function and pass something. That argument could be a JSON string, by example, which could have any structure you desire (in this example reading 2 query params and returning them concatenated).

import logging
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
    category = req.route_params.get('category')
    id = req.route_params.get('id')
    message = f"Category: {category}, ID: {id}"
    return func.HttpResponse(message)

All detailed at https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook-trigger?tabs=in-process%2Cfunctionsv2&pivots=programming-language-python

Advertisement