Note: สร้าง functions project ด้วย vscode extension ง่ายกว่าสร้างด้วยคำสั่ง func init
(Create an Azure Function by using Visual Studio Code) เพราะจะสร้างไฟล์ config ต่างๆของ vscode และ .venv มาให้ด้วย , สร้างด้วยคำสั่งมีประโยชน์ตอนสร้าง function ด้วย func new
Create a new functions project with func init
สร้าง functions project ชื่อ FunctionPy2207
> mkdir FunctionPy2207 > cd .\FunctionPy2207\
To create a new functions project, run func init
on the command line.
> func init Use the up/down arrow keys to select a worker runtime: dotnet dotnet (isolated process) node python powershell custom > func init Use the up/down arrow keys to select a worker runtime:python Found Python version 3.9.10 (py). Writing requirements.txt Writing getting_started.md Writing .gitignore Writing host.json Writing local.settings.json Writing C:\Project\FunctionPy2207\.vscode\extensions.json
ดูไฟล์ที่คำสั่ง func init
สร้างมาให้ เมื่อเลือกเป็น python
$ tree . ├── getting_started.md ├── host.json ├── local.settings.json └── requirements.txt 0 directories, 4 files
host.json
{ "version": "2.0", "logging": { "applicationInsights": { "samplingSettings": { "isEnabled": true, "excludedTypes": "Request" } } }, "extensionBundle": { "id": "Microsoft.Azure.Functions.ExtensionBundle", "version": "[2.*, 3.0.0)" } }
local.settings.json
{ "IsEncrypted": false, "Values": { "FUNCTIONS_WORKER_RUNTIME": "python", "AzureWebJobsStorage": "UseDevelopmentStorage=true" } }
requirements.txt
# Do not include azure-functions-worker as it may conflict with the Azure Functions platform azure-functions
getting_started.md
## Getting Started with Azure Function ### Last updated: March 8th 2021 #### Project Structure The main project folder (<project_root>) can contain the following files: * **local.settings.json** - Used to store app settings and connection strings when running locally. This file doesn't get published to Azure. To learn more, see [local.settings.file](https://aka.ms/azure-functions/python/local-settings). * **requirements.txt** - Contains the list of Python packages the system installs when publishing to Azure. * **host.json** - Contains global configuration options that affect all functions in a function app. This file does get published to Azure. Not all options are supported when running locally. To learn more, see [host.json](https://aka.ms/azure-functions/python/host.json). * **.vscode/** - (Optional) Contains store VSCode configuration. To learn more, see [VSCode setting](https://aka.ms/azure-functions/python/vscode-getting-started). * **.venv/** - (Optional) Contains a Python virtual environment used by local development. * **Dockerfile** - (Optional) Used when publishing your project in a [custom container](https://aka.ms/azure-functions/python/custom-container). * **tests/** - (Optional) Contains the test cases of your function app. For more information, see [Unit Testing](https://aka.ms/azure-functions/python/unit-testing). * **.funcignore** - (Optional) Declares files that shouldn't get published to Azure. Usually, this file contains .vscode/ to ignore your editor setting, .venv/ to ignore local Python virtual environment, tests/ to ignore test cases, and local.settings.json to prevent local app settings being published. Each function has its own code file and binding configuration file ([**function.json**](https://aka.ms/azure-functions/python/function.json)). #### Developing your first Python function using VS Code If you have not already, please checkout our [quickstart](https://aka.ms/azure-functions/python/quickstart) to get you started with Azure Functions developments in Python. #### Publishing your function app to Azure For more information on deployment options for Azure Functions, please visit this [guide](https://docs.microsoft.com/en-us/azure/azure-functions/create-first-function-vs-code-python#publish-the-project-to-azure). #### Next Steps * To learn more about developing Azure Functions, please visit [Azure Functions Developer Guide](https://aka.ms/azure-functions/python/developer-guide). * To learn more specific guidance on developing Azure Functions with Python, please visit [Azure Functions Developer Python Guide](https://aka.ms/azure-functions/python/python-developer-guide).
Create a new function with func new
สร้าง function ชื่อ HttpExample โดยใช้ template แบบ HttpTrigger
> func new Use the up/down arrow keys to select a template: Azure Blob Storage trigger Azure Cosmos DB trigger Durable Functions activity Durable Functions entity Durable Functions HTTP starter Durable Functions orchestrator Azure Event Grid trigger Azure Event Hub trigger HTTP trigger Kafka output Kafka trigger Azure Queue Storage trigger RabbitMQ trigger Azure Service Bus Queue trigger Azure Service Bus Topic trigger Timer trigger > func new Use the up/down arrow keys to select a template:HTTP trigger Function name: [HttpTrigger] HttpExample Writing C:\Project\FunctionPy2207\HttpExample\__init__.py Writing C:\Project\FunctionPy2207\HttpExample\function.json The function "HttpExample" was created successfully from the "HTTP trigger" template.
จะได้ไฟล์ HttpExample/__init__.py และไฟล์ HttpExample/function.json
HttpExample/__init__.py
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 )
HttpExample/function.json
{ "scriptFile": "__init__.py", "bindings": [ { "authLevel": "function", "type": "httpTrigger", "direction": "in", "name": "req", "methods": [ "get", "post" ] }, { "type": "http", "direction": "out", "name": "$return" } ] }
Run functions locally
> func start Found Python version 3.9.10 (py). Azure Functions Core Tools Core Tools Version: 4.0.4629 Commit hash: N/A (64-bit) Function Runtime Version: 4.6.1.18388 Functions: HttpExample: [GET,POST] http://localhost:7071/api/HttpExample For detailed output, run func with --verbose flag.
ติดตั้ง package เพิ่ม
สร้าง Virtual Environments ชื่อ .venv
> python -m venv .venv
activate
> .\.venv\Scripts\activate
ติดตั้ง numpy
> python -m pip install numpy
ไฟล์ requirements.txt
# Do not include azure-functions-worker as it may conflict with the Azure Functions platform azure-functions numpy
เพิ่ม import
ที่ไฟล์ __init__.py
import numpy
ลองรัน func start
ถ้าไม่ error ก็ติดตั้ง package สำเร็จ