Skip to content

Commit 65690a6

Browse files
Add GetProducts and AddProduct sample in python v2 model (#688)
* add python v2 samples * add v2 samples link to python setup guide * Update docs/SetupGuide_Python.md Co-authored-by: Maddy <[email protected]> --------- Co-authored-by: Maddy <[email protected]>
1 parent 7d28bd3 commit 65690a6

File tree

12 files changed

+299
-0
lines changed

12 files changed

+299
-0
lines changed

docs/SetupGuide_Python.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- [Samples for Output Bindings](#samples-for-output-bindings)
2020
- [Array](#array)
2121
- [Single Row](#single-row)
22+
- [Python V2 Model](#python-v2-model)
2223
- [Trigger Binding](#trigger-binding)
2324

2425
## Setup Function Project
@@ -200,6 +201,10 @@ See the [AddProductsArray](https://github.com/Azure/azure-functions-sql-extensio
200201
201202
See the [AddProduct](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-python/AddProduct) sample
202203
204+
## Python V2 Model
205+
206+
See the Python V2 Model samples [here](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-python-v2/). More information about the Python V2 Model can be found [here](https://learn.microsoft.com/azure/azure-functions/functions-reference-python?tabs=asgi%2Capplication-level&pivots=python-mode-decorators).
207+
203208
## Trigger Binding
204209
205210
> Trigger binding support is only available for in-proc C# functions at present.

samples/samples-python-v2/.funcignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.git*
2+
.vscode
3+
__azurite_db*__.json
4+
__blobstorage__
5+
__queuestorage__
6+
local.settings.json
7+
test
8+
.venv

samples/samples-python-v2/.gitignore

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
pip-wheel-metadata/
24+
share/python-wheels/
25+
*.egg-info/
26+
.installed.cfg
27+
*.egg
28+
MANIFEST
29+
30+
# PyInstaller
31+
# Usually these files are written by a python script from a template
32+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
33+
*.manifest
34+
*.spec
35+
36+
# Installer logs
37+
pip-log.txt
38+
pip-delete-this-directory.txt
39+
40+
# Unit test / coverage reports
41+
htmlcov/
42+
.tox/
43+
.nox/
44+
.coverage
45+
.coverage.*
46+
.cache
47+
nosetests.xml
48+
coverage.xml
49+
*.cover
50+
.hypothesis/
51+
.pytest_cache/
52+
53+
# Translations
54+
*.mo
55+
*.pot
56+
57+
# Django stuff:
58+
*.log
59+
local_settings.py
60+
db.sqlite3
61+
62+
# Flask stuff:
63+
instance/
64+
.webassets-cache
65+
66+
# Scrapy stuff:
67+
.scrapy
68+
69+
# Sphinx documentation
70+
docs/_build/
71+
72+
# PyBuilder
73+
target/
74+
75+
# Jupyter Notebook
76+
.ipynb_checkpoints
77+
78+
# IPython
79+
profile_default/
80+
ipython_config.py
81+
82+
# pyenv
83+
.python-version
84+
85+
# pipenv
86+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
87+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
88+
# having no cross-platform support, pipenv may install dependencies that don’t work, or not
89+
# install all needed dependencies.
90+
#Pipfile.lock
91+
92+
# celery beat schedule file
93+
celerybeat-schedule
94+
95+
# SageMath parsed files
96+
*.sage.py
97+
98+
# Environments
99+
.env
100+
.venv
101+
env/
102+
venv/
103+
ENV/
104+
env.bak/
105+
venv.bak/
106+
107+
# Spyder project settings
108+
.spyderproject
109+
.spyproject
110+
111+
# Rope project settings
112+
.ropeproject
113+
114+
# mkdocs documentation
115+
/site
116+
117+
# mypy
118+
.mypy_cache/
119+
.dmypy.json
120+
dmypy.json
121+
122+
# Pyre type checker
123+
.pyre/
124+
125+
# Azure Functions artifacts
126+
bin
127+
obj
128+
appsettings.json
129+
local.settings.json
130+
131+
# Azurite artifacts
132+
__blobstorage__
133+
__queuestorage__
134+
__azurite_db*__.json
135+
.python_packages

samples/samples-python-v2/.pylintrc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[MASTER]
2+
3+
ignore=.venv
4+
5+
# List of plugins (as comma separated values of python module names) to load,
6+
# usually to register additional checkers.
7+
load-plugins=pylintfileheader
8+
9+
file-header=# Copyright \(c\) Microsoft Corporation\. All rights reserved\.[\r\n]+# Licensed under the MIT License\.
10+
11+
[MESSAGES CONTROL]
12+
# Disable the message, report, category or checker with the given id(s). You
13+
# can either give multiple identifier separated by comma (,) or put this option
14+
# multiple time (only on the command line, not in the configuration file where
15+
# it should appear only once).
16+
disable= R0801, W0108, W0613, C0103, C0114, C0115, C0116, E0401, C0301, R0913, R0914
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
"azureFunctions.projectLanguageModel": 2
9+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"type": "func",
6+
"label": "func: host start",
7+
"command": "host start",
8+
"problemMatcher": "$func-python-watch",
9+
"isBackground": true,
10+
"dependsOn": "pip install (functions)"
11+
},
12+
{
13+
"label": "pip install (functions)",
14+
"type": "shell",
15+
"osx": {
16+
"command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
17+
},
18+
"windows": {
19+
"command": "${config:azureFunctions.pythonVenv}\\Scripts\\python -m pip install -r requirements.txt"
20+
},
21+
"linux": {
22+
"command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
23+
},
24+
"problemMatcher": []
25+
}
26+
]
27+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import json
5+
import azure.functions as func
6+
from azure.functions.decorators.core import DataType
7+
8+
app = func.FunctionApp()
9+
10+
# Learn more at aka.ms/pythonprogrammingmodel
11+
12+
# The input binding executes the `SELECT * FROM Products WHERE Cost = @Cost` query.
13+
# The Parameters argument passes the `{cost}` specified in the URL that triggers the function,
14+
# `getproducts/{cost}`, as the value of the `@Cost` parameter in the query.
15+
# CommandType is set to `Text`, since the constructor argument of the binding is a raw query.
16+
@app.function_name(name="GetProducts")
17+
@app.route(route="getproducts/{cost}")
18+
@app.generic_input_binding(arg_name="products", type="sql",
19+
CommandText="SELECT * FROM Products WHERE Cost = @Cost",
20+
CommandType="Text",
21+
Parameters="@Cost={cost}",
22+
ConnectionStringSetting="SqlConnectionString",
23+
data_type=DataType.STRING)
24+
def get_products(req: func.HttpRequest, products: func.SqlRowList) -> func.HttpResponse:
25+
rows = list(map(lambda r: json.loads(r.to_json()), products))
26+
27+
return func.HttpResponse(
28+
json.dumps(rows),
29+
status_code=200,
30+
mimetype="application/json"
31+
)
32+
33+
# The output binding upserts the product, which will insert it into the Products table if
34+
# the primary key (ProductId) for that item doesn't exist. If it does then update it to
35+
# have the new name and cost.
36+
@app.function_name(name="AddProduct")
37+
@app.route(route="addproduct")
38+
@app.generic_output_binding(arg_name="product", type="sql",
39+
CommandText="[dbo].[Products]",
40+
ConnectionStringSetting="SqlConnectionString",
41+
data_type=DataType.STRING)
42+
def add_product(req: func.HttpRequest, product: func.Out[func.SqlRow]) -> func.HttpResponse:
43+
body = json.loads(req.get_body())
44+
row = func.SqlRow.from_dict(body)
45+
product.set(row)
46+
47+
return func.HttpResponse(
48+
body=req.get_body(),
49+
status_code=201,
50+
mimetype="application/json"
51+
)

samples/samples-python-v2/host.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": "2.0",
3+
"logging": {
4+
"applicationInsights": {
5+
"samplingSettings": {
6+
"isEnabled": true,
7+
"excludedTypes": "Request"
8+
}
9+
}
10+
},
11+
"extensionBundle": {
12+
"id": "Microsoft.Azure.Functions.ExtensionBundle.Preview",
13+
"version": "[4.0.0, 5.0.0)"
14+
}
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"IsEncrypted": false,
3+
"Values": {
4+
"FUNCTIONS_WORKER_RUNTIME": "python",
5+
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
6+
"AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
7+
"SqlConnectionString": "",
8+
"PYTHON_ISOLATE_WORKER_DEPENDENCIES": "1"
9+
}
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# DO NOT include azure-functions-worker in this file
2+
# The Python Worker is managed by Azure Functions platform
3+
# Manually managing azure-functions-worker may cause unexpected issues
4+
5+
azure-functions

0 commit comments

Comments
 (0)