Using Azure functions trigger a python code through powerapps?

Jenifa Thomas 20 Reputation points
2023-09-22T05:28:17.5033333+00:00

I have created a function app in azure -

This is the code inside the function app.For this code I want to take input of month and year from power apps. And display the output that is num_sundays in the power apps.How can I achieve this?

import logging

import azure.functions as func
import calendar

def count_sundays(year, month):
    # Calculate the number of days in the given month
    _, last_day = calendar.monthrange(year, month)
    
    # Count the number of Sundays (weekday 6) in the month
    num_sundays = sum(1 for day in range(1, last_day + 1) if calendar.weekday(year, month, day) == 6)
    
    return num_sundays


def main(req: func.HttpRequest) -> func.HttpResponse:
    req_body = req.get_json()
    year = req_body.get('year')
    month = req_body.get('month')
    
    num_sundays = count_sundays(year, month)
    response = {"sundays_in_month": num_sundays}
    
    if response:
      return func.HttpResponse(num_sundays)
    else:
      return func.HttpResponse(
            "This HTTP triggered function executed successfully",
            status_code=200)

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,998 questions
{count} votes

Accepted answer
  1. Bruno Lucas 4,431 Reputation points MVP
    2023-09-25T08:30:57.1633333+00:00

    Hi @Jenifa Thomas

    Did you manage to run and deploy that code?

    you may need this line:

    User's image

    The rest is more a question for the Power Platform forums to help:

    https://powerusers.microsoft.com/t5/Power-Apps-Community/ct-p/PowerApps1

    This post suggests "registering a custom API in PowerApps" but I couldn't find it anymore. That post also seems pretty old:

    https://powerapps.microsoft.com/nl-be/blog/using-azure-functions-in-powerapps/

    Most other sites suggest using power automate to invoke the azure function and call the power automate in the canvas app. For that you can use the Http task

    https://brunolucas.blog/2022/09/21/using-azure-functions-to-integrate-with-dataverse-azure-functions-v4-and-dataverse-client-with-msal/#AZFLA

    User's image

    Than add the power automate to the app:

    https://learn.microsoft.com/en-us/power-apps/maker/canvas-apps/using-logic-flows#add-a-flow-to-an-app


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.