✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚾️ It's finally Baseball season! Root for the home team... & Register for our Sports Analytics Webinar!

Problem deploying plotly on azure function

I have a question regarding deploying a function in the Azure portal.

I had a project for data visualizations that I wrote that code with Matplotlib and I deployed it on Azure, and right now is on production.

Then, I added some other graphs but this time I used Plotly. I am able to run the Azure function locally and I don’t have any problems with it. It will return the value correctly with status 200ok on Postman. However, when I deploy it on the Azure portal, I got status 500 internal servers!

Can anyone help me with this?

Note that I have a requirements file that has Plotly and everything I need on it in the same folder that I am deploying. Should I install plotly on Microsoft Azure myself?!? I assume that it can read from the requirements file

Note that I don’t have this problem with the graphs that I used Matplotlib for them. I just have this problem with the graphs I used Plotly for them.

Below, I put the small sample of my code that I can run it locally on postman, and it will return status 200Ok with string but when I deploy it on Azure, I receive 500 internal server problem

import logging
import azure.functions as func
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
#import cv2
import base64
import os
import tempfile
from cv2 import cv2
import plotly.graph_objs as go


def graphing(shortID, data, labels, dims = (600, 300), format = 'png', **render_details):
    if shortID == 'M':
        print("shortID", shortID)
        myfig = go.Figure(go.Bar(
            x=data[0],
            y=data[1],
            orientation='h'))
        
        tempFilePath = tempfile.gettempdir()

        try:
            #print("came to try")
            #print(format)
            file_path = os.path.join(tempFilePath, 'plot.'+ format)
            myfig.write_image(file_path)
            img = cv2.imread(file_path)
            _, im_arr = cv2.imencode('.'+format, img)
            im_bytes = im_arr.tobytes()
            response = base64.b64encode(im_bytes)
            os.remove(file_path)
        except:
            #print("came to except")
            file_path = os.path.join(tempFilePath, 'plot.png')
            myfig.write_image(file_path)
            img = cv2.imread(file_path)
            _, im_arr = cv2.imencode('.png', img)
            im_bytes = im_arr.tobytes()
            response = base64.b64encode(im_bytes)
            #response = im_b64.decode("utf-8")
            os.remove(file_path)

    return response

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

    shortID = req.params.get('shortID')
    data = req.params.get('data')
    labels = req.params.get('labels')
    image_format =req.params.get('format')
    #print("image_format", image_format)
    if not shortID or not data or not labels:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            shortID = req_body.get('shortID')   
            data = req_body.get('data')
            labels = req_body.get('labels')
            image_format = req_body.get('format')
    #print("image_format", image_format)
    if shortID and data and labels:
        im_str = graphing(shortID, data, labels, format = image_format)
    
    

    if shortID:
        if image_format:
            try:
                print("try image")
                return func.HttpResponse(body=im_str, mimetype='image/'+image_format)
            except:
                print("except image")
                return func.HttpResponse(body=im_str, mimetype='image/png')
        else:
            print("else image")
            return func.HttpResponse(body=im_str, mimetype='image/png')
    else:
        return func.HttpResponse(
             "Please pass required parameters on the query string or in the request body",
             status_code=400
        )

Here is the sample data when you want to run the code:

{

"shortID": "M",

"data": [[20, 14, 23],["Dispositions","Parenting","Education"], []],

"labels": ["None", "None"],

"format": "jpg"

}

Here is what I have in requirements.txt file:

DO NOT include azure-functions-worker in this file

The Python Worker is managed by Azure Functions platform

Manually managing azure-functions-worker may cause unexpected issues

azure-functions
numpy
matplotlib
opencv-python
plotly
scikit-image
pandas
psutil
requests