Azure Functions >> Tips and Tricks 1

2022-08-16 Azure

Table of Contents

This tutorial will introduce how to create a Azure function and run it.

Azure

Azure function Python example

1. Create an Azure account for free

You can read [Create a free Azure account] section of the following article.

[Create a free Azure account]
https://thats-it-code.com/azure/how-to-create-a-static-html-website-with-basic-authentication-on-azure-in-10-minutes/

2. Install Azure Functions Core Tools

npm i -g azure-functions-core-tools@<version> --unsafe-perm true

3. Install Python and Visual Studio Code

You should confirm which versions of Python that Azure fuctions supports.
Languages by runtime version

And install the supported version of Python.
You can read the following article.
https://thats-it-code.com/programming/lets-create-a-programming-environment/#so-lets-get-started

4. Install VS Code extensions

After installing VS Code, you also need to install Python Extension for VS Code.

And we also need to install the Azure Functions Extension for VS Code.

Click Azure Functions extentsion button on the left side of the VS Code window and select Sign in to Azure in the RESOURCE pane.
Azure functions Tips and Tricks

Azure functions Tips and Tricks

5. Create a Azure functions

  • Click the Azure Functions extentsion button on the left side of the VS Code window. And click + button of the WORKSPACE section and click “Create Functions”.
    Azure functions Tips and Tricks

  • Select “HTTP trigger” and enter the name.
    Azure functions Tips and Tricks

Azure functions Tips and Tricks

  • Select “Function” and Enter.
    Azure functions Tips and Tricks

You will find the Azure Functions is created successfully.
Azure functions Tips and Tricks

  • Switch to the File Explorer, you will find the Azure Functions folder is created.
    Azure functions Tips and Tricks

  • Open “__init__.py” file.
    Azure functions Tips and Tricks

import logging

import azure.functions as func


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

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

    if name:
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
    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
        )

6. Run the Azure Functions

Press F5 key to run the Azure Functions.
Azure functions Tips and Tricks

Open the api url in the browser. Azure functions Tips and Tricks

We can also give the name in the query string.
Azure functions Tips and Tricks

And the message will change.
Azure functions Tips and Tricks

Python Azure Functions return json

We can use the json module to return json.
We need to import the json module at the top.

import json

And use the dumps() method of json module to return json data.

return func.HttpResponse(
            json.dumps({"message": f"Hello, {name}. This HTTP triggered function executed successfully."}),
        )

When we open the url again, we can see the json format message is returned.
Azure functions Tips and Tricks

Python Azure Functions logging

We can use the logging module to log the message. The logging module is tied to Application Insights and we can query and analyze the log results later.

import logging

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

You can use the following methods to output different levels of log.

Method Description
critical(message) Outputs a critical message.
error(message) Outputs an error message.
warning(message) Outputs a warning message.
info(message) Outputs an informational message.
debug(message) Outputs a debug message.

Subscribe and be the FIRST reader of our latest articles

* indicates required

Contact us