Skip to content

Commit b4beaaf

Browse files
Add Python samples (#253)
* Initial samples * cleanup * AddProductsArray * Add product class * remove unneeded * More samples + cleanup * update python samples to match js samples * update param name and runtime version * remove extra change Co-authored-by: chgagnon <chgagnon@microsoft.com>
1 parent 8f92b00 commit b4beaaf

File tree

34 files changed

+624
-0
lines changed

34 files changed

+624
-0
lines changed

samples/samples-python/.funcignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.venv

samples/samples-python/.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Azure Functions localsettings file
2+
local.settings.json
3+
4+
# Azurite artifacts
5+
__blobstorage__
6+
__queuestorage__
7+
__azurite_db*__.json
8+
9+
# Python virtual environment
10+
.venv
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"recommendations": [
3+
"ms-azuretools.vscode-azurefunctions",
4+
"ms-python.python"
5+
]
6+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Attach to Python Functions",
6+
"type": "python",
7+
"request": "attach",
8+
"port": 9091,
9+
"preLaunchTask": "func: host start"
10+
}
11+
]
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"azureFunctions.deploySubpath": ".",
3+
"azureFunctions.scmDoBuildDuringDeployment": true,
4+
"azureFunctions.pythonVenv": ".venv",
5+
"azureFunctions.projectLanguage": "Python",
6+
"azureFunctions.projectRuntime": "~4",
7+
"debug.internalConsoleOptions": "neverOpen"
8+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"type": "func",
6+
"command": "host start",
7+
"problemMatcher": "$func-python-watch",
8+
"isBackground": true,
9+
"dependsOn": "pip install (functions)",
10+
},
11+
{
12+
"label": "pip install (functions)",
13+
"type": "shell",
14+
"osx": {
15+
"command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
16+
},
17+
"windows": {
18+
"command": "${config:azureFunctions.pythonVenv}\\Scripts\\python -m pip install -r requirements.txt"
19+
},
20+
"linux": {
21+
"command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
22+
},
23+
"problemMatcher": []
24+
}
25+
]
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import azure.functions as func
5+
import json
6+
7+
def main(req: func.HttpRequest, product: func.Out[func.SqlRow]) -> func.HttpResponse:
8+
"""Upsert the product, which will insert it into the Products table if the primary key (ProductId) for that item doesn't exist.
9+
If it does then update it to have the new name and cost.
10+
"""
11+
12+
# Note that this expects the body to be a JSON object which
13+
# have a property matching each of the columns in the table to upsert to.
14+
body = json.loads(req.get_body())
15+
row = func.SqlRow.from_dict(body)
16+
product.set(row)
17+
18+
return func.HttpResponse(
19+
body=req.get_body(),
20+
status_code=201,
21+
mimetype="application/json"
22+
)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"scriptFile": "__init__.py",
3+
"bindings": [
4+
{
5+
"authLevel": "function",
6+
"name": "req",
7+
"direction": "in",
8+
"type": "httpTrigger",
9+
"methods": [
10+
"post"
11+
],
12+
"route": "addproduct"
13+
},
14+
{
15+
"name": "$return",
16+
"type": "http",
17+
"direction": "out"
18+
},
19+
{
20+
"name": "product",
21+
"type": "sql",
22+
"direction": "out",
23+
"commandText": "[dbo].[Products]",
24+
"connectionStringSetting": "SqlConnectionString"
25+
}
26+
],
27+
"disabled": false
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import azure.functions as func
5+
from Common.product import Product
6+
7+
def main(req: func.HttpRequest, product: func.Out[func.SqlRow]) -> func.HttpResponse:
8+
row = func.SqlRow(Product(req.params["productId"], req.params["name"], req.params["cost"]))
9+
product.set(row)
10+
11+
return func.HttpResponse(
12+
body=row.to_json(),
13+
status_code=201,
14+
mimetype="application/json"
15+
)
16+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"scriptFile": "__init__.py",
3+
"bindings": [
4+
{
5+
"authLevel": "function",
6+
"name": "req",
7+
"direction": "in",
8+
"type": "httpTrigger",
9+
"methods": [
10+
"get"
11+
],
12+
"route": "addproduct-params"
13+
},
14+
{
15+
"name": "$return",
16+
"type": "http",
17+
"direction": "out"
18+
},
19+
{
20+
"name": "product",
21+
"type": "sql",
22+
"direction": "out",
23+
"commandText": "[dbo].[Products]",
24+
"connectionStringSetting": "SqlConnectionString"
25+
}
26+
],
27+
"disabled": false
28+
}

0 commit comments

Comments
 (0)