Create your first Python function

Asavari Tayal edited this page Oct 5, 2018 · 17 revisions

The following steps are supported on a Mac, Windows, or Linux computer.

Prerequisites

To build and test locally, you will need to:

Create and activate a virtual environment

To create a Functions project, it is required that you work in a Python 3.6 virtual environment. Run the following commands to create and activate a virtual environment named env.

# In Bash
python3.6 -m venv env
source env/bin/activate

# In PowerShell
py -3.6 -m venv env
env\scripts\activate

Create a local Functions project

You can now create a local Functions project. This directory is the equivalent of a Function App in Azure. It can contain multiple functions that share the same local and hosting configuration.

In the terminal window or from a command prompt, run the following command:

func init MyFunctionProj --worker-runtime python

You will see something like the following output.

Installing wheel package
Installing azure-functions package
Installing azure-functions-worker package
Running pip freeze
Writing .gitignore
Writing host.json
Writing local.settings.json
Writing /MyFunctionProj/.vscode/extensions.json

A new folder named MyFunctionProj is created. To continue, change directory to this folder.

cd MyFunctionProj

Create a function

To create a function, run the following command:

func new

Pick HTTP Trigger as the desired template and enter a function name of HttpTrigger.

Select a template:
1. Blob trigger
2. Cosmos DB trigger
3. Event Grid trigger
4. Event Hub trigger
5. HTTP trigger
6. Queue trigger
7. Service Bus Queue trigger
8. Service Bus Topic trigger
9. Timer trigger

Choose option: 5
Function name: HttpTrigger

You will see something like the following output.

Writing /MyFunctionProj/HttpTrigger/sample.dat
Writing /MyFunctionProj/HttpTrigger/__init__.py
Writing /MyFunctionProj/HttpTrigger/function.json
The function "HttpTrigger" was created successfully from the "HTTP trigger" template.

A sub-folder named HttpTrigger is created. This contains __init__.py which is the primary script file and function.json file which describes the trigger and bindings used by the function. To learn more about the programming model, you can refer to the Azure Functions Python developer guide.

Run the function locally

Use the following command to run the Functions host locally.

func host start

When the Functions host starts, it outputs the URL of your HTTP-triggered function. (Note that the entire output has been truncated for readability.)


                  %%%%%%
                 %%%%%%
            @   %%%%%%    @
          @@   %%%%%%      @@
       @@@    %%%%%%%%%%%    @@@
     @@      %%%%%%%%%%        @@
       @@         %%%%       @@
         @@      %%%       @@
           @@    %%      @@
                %%
                %
...
Now listening on: http://0.0.0.0:7071
Application started. Press Ctrl+C to shut down.
...

Http Functions:

        HttpTrigger: http://localhost:7071/api/HttpTrigger

Copy the URL of your function from the output and paste it into your browser's address bar. Append the query string ?name=<yourname> to this URL and execute the request.

http://localhost:7071/api/HttpTrigger?name=<yourname>

The following screenshot shows the response from the function, when it is triggered from the browser:

You are now ready to publish your local functions project to the Function App in Azure.

Create a serverless linux app in Azure

https://aka.ms/functions-consumptionlinux-preview

Publish your functions

Using the Azure Functions Core Tools, run the following command. Replace <app_name> with the name of your app.

func azure functionapp publish <app_name>

You will see something like the following output, which has been truncated for readability.

Getting site publishing info...

...

Preparing archive...
Uploading content...
Upload completed successfully.
Deployment completed successfully.
Syncing triggers...

Test the function

Enter the same URL in the address of your web browser. Again, replace the <app_name> placeholder with the name of your function app, and append the query string &name=<yourname> to the URL and execute the request.

http://<app_name>.azurewebsites.net/api/HttpTriggerPython?name=<yourname>

Next steps

Learn more about developing Azure Functions using Python.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.