0

I am new to the azure function app. I am developing an app to generate wordcloud and for that purpose, I am fetching my data from the Cosmos DB. Everything is working fine locally using VS Code. When I am deploying my azure function on an azure function app, deployment is getting succeeded and in the browser, I am getting the below message.

This HTTP-triggered function was executed successfully. Pass a name in the query string or the request body for a personalized response.

It means the deployment is successful. But when I pass the Query parameters and call the get_wordcloud function, it throws a 500 Internal Server Error. My guess is it is not able to write the image file in the Azure environment. Below is my __init__.py file:

import logging

import azure.functions as func

from azure.cosmos import CosmosClient
import os

from pandas import json_normalize

from wordcloud import WordCloud, STOPWORDS

from PIL import Image


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    db_name = req.params.get('db_name')
    container_name = req.params.get('container_name')
    if not db_name and not container_name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            db_name = req_body.get('db_name')
            container_name = req_body.get('container_name')

    if db_name and container_name:
        url = os.environ.get('ACCOUNT_URI')
        
        key = os.environ.get('ACCOUNT_KEY')

        client = CosmosClient(url, credential=key)

        database = client.get_database_client(db_name)
        container = database.get_container_client(container_name)
        print(database)
        print(container)

        query = 'SELECT * FROM c'
        result = list(container.query_items(
            query, enable_cross_partition_query=True))

        df = json_normalize(result)
        stopwords = set(STOPWORDS)
        wordcloud = WordCloud(
            background_color='white',
            stopwords=stopwords,
            max_words=500,
            width=1080,
            height=640,
        ).generate(str(df))
        wordcloud.to_file("wordcloud_result.png")
        file = open(u'wordcloud_result.png', 'rb')
        result = file.read()
        return func.HttpResponse(result)
    else:
        return func.HttpResponse(
            "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
            status_code=200
        )

Below is the function.json file:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ]
}

In the __init__.py when I am replacing the below code

wordcloud.to_file("wordcloud_result.png")
file = open(u'wordcloud_result.png', 'rb')
result = file.read()
return func.HttpResponse(result)

with the following code

return func.HttpResponse('Successful')

it's working successfully.

Sneha Roy
  • 81
  • 1
  • 3
  • 16
  • 500 status code means there's an unhandled exception. I would recommend putting your entire Function code in try/catch block and log the exception. Please share that exception in your question then. – Gaurav Mantri Jul 16 '21 at 03:51

1 Answers1

0

not a python user, but what I notice is that you don't have a header content-type (application/octet-stream) and you should return a StreamContent... have a look at this version of your question for C# How do I return a blob from a HTTP triggered Azure Function?

AndreiC
  • 1,490
  • 4
  • 16
  • 34