diff --git a/docs/SetupGuide_Python.md b/docs/SetupGuide_Python.md index bfc2ad2f2..5566e631d 100644 --- a/docs/SetupGuide_Python.md +++ b/docs/SetupGuide_Python.md @@ -19,6 +19,7 @@ - [Samples for Output Bindings](#samples-for-output-bindings) - [Array](#array) - [Single Row](#single-row) + - [Python V2 Model](#python-v2-model) - [Trigger Binding](#trigger-binding) ## Setup Function Project @@ -200,6 +201,10 @@ See the [AddProductsArray](https://github.com/Azure/azure-functions-sql-extensio See the [AddProduct](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-python/AddProduct) sample +## Python V2 Model + +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). + ## Trigger Binding > Trigger binding support is only available for in-proc C# functions at present. \ No newline at end of file diff --git a/samples/samples-python-v2/.funcignore b/samples/samples-python-v2/.funcignore new file mode 100644 index 000000000..9966315f8 --- /dev/null +++ b/samples/samples-python-v2/.funcignore @@ -0,0 +1,8 @@ +.git* +.vscode +__azurite_db*__.json +__blobstorage__ +__queuestorage__ +local.settings.json +test +.venv \ No newline at end of file diff --git a/samples/samples-python-v2/.gitignore b/samples/samples-python-v2/.gitignore new file mode 100644 index 000000000..7685fc4ac --- /dev/null +++ b/samples/samples-python-v2/.gitignore @@ -0,0 +1,135 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don’t work, or not +# install all needed dependencies. +#Pipfile.lock + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# Azure Functions artifacts +bin +obj +appsettings.json +local.settings.json + +# Azurite artifacts +__blobstorage__ +__queuestorage__ +__azurite_db*__.json +.python_packages \ No newline at end of file diff --git a/samples/samples-python-v2/.pylintrc b/samples/samples-python-v2/.pylintrc new file mode 100644 index 000000000..439fba0a1 --- /dev/null +++ b/samples/samples-python-v2/.pylintrc @@ -0,0 +1,16 @@ +[MASTER] + +ignore=.venv + +# List of plugins (as comma separated values of python module names) to load, +# usually to register additional checkers. +load-plugins=pylintfileheader + +file-header=# Copyright \(c\) Microsoft Corporation\. All rights reserved\.[\r\n]+# Licensed under the MIT License\. + +[MESSAGES CONTROL] +# Disable the message, report, category or checker with the given id(s). You +# can either give multiple identifier separated by comma (,) or put this option +# multiple time (only on the command line, not in the configuration file where +# it should appear only once). +disable= R0801, W0108, W0613, C0103, C0114, C0115, C0116, E0401, C0301, R0913, R0914 \ No newline at end of file diff --git a/samples/samples-python-v2/.vscode/extensions.json b/samples/samples-python-v2/.vscode/extensions.json new file mode 100644 index 000000000..3f63eb97d --- /dev/null +++ b/samples/samples-python-v2/.vscode/extensions.json @@ -0,0 +1,6 @@ +{ + "recommendations": [ + "ms-azuretools.vscode-azurefunctions", + "ms-python.python" + ] +} \ No newline at end of file diff --git a/samples/samples-python-v2/.vscode/launch.json b/samples/samples-python-v2/.vscode/launch.json new file mode 100644 index 000000000..4508b4587 --- /dev/null +++ b/samples/samples-python-v2/.vscode/launch.json @@ -0,0 +1,12 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Attach to Python Functions", + "type": "python", + "request": "attach", + "port": 9091, + "preLaunchTask": "func: host start" + } + ] +} \ No newline at end of file diff --git a/samples/samples-python-v2/.vscode/settings.json b/samples/samples-python-v2/.vscode/settings.json new file mode 100644 index 000000000..60e70c224 --- /dev/null +++ b/samples/samples-python-v2/.vscode/settings.json @@ -0,0 +1,9 @@ +{ + "azureFunctions.deploySubpath": ".", + "azureFunctions.scmDoBuildDuringDeployment": true, + "azureFunctions.pythonVenv": ".venv", + "azureFunctions.projectLanguage": "Python", + "azureFunctions.projectRuntime": "~4", + "debug.internalConsoleOptions": "neverOpen", + "azureFunctions.projectLanguageModel": 2 +} \ No newline at end of file diff --git a/samples/samples-python-v2/.vscode/tasks.json b/samples/samples-python-v2/.vscode/tasks.json new file mode 100644 index 000000000..ba75962d0 --- /dev/null +++ b/samples/samples-python-v2/.vscode/tasks.json @@ -0,0 +1,27 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "type": "func", + "label": "func: host start", + "command": "host start", + "problemMatcher": "$func-python-watch", + "isBackground": true, + "dependsOn": "pip install (functions)" + }, + { + "label": "pip install (functions)", + "type": "shell", + "osx": { + "command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt" + }, + "windows": { + "command": "${config:azureFunctions.pythonVenv}\\Scripts\\python -m pip install -r requirements.txt" + }, + "linux": { + "command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt" + }, + "problemMatcher": [] + } + ] +} \ No newline at end of file diff --git a/samples/samples-python-v2/function_app.py b/samples/samples-python-v2/function_app.py new file mode 100644 index 000000000..acff6b43e --- /dev/null +++ b/samples/samples-python-v2/function_app.py @@ -0,0 +1,51 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import json +import azure.functions as func +from azure.functions.decorators.core import DataType + +app = func.FunctionApp() + +# Learn more at aka.ms/pythonprogrammingmodel + +# The input binding executes the `SELECT * FROM Products WHERE Cost = @Cost` query. +# The Parameters argument passes the `{cost}` specified in the URL that triggers the function, +# `getproducts/{cost}`, as the value of the `@Cost` parameter in the query. +# CommandType is set to `Text`, since the constructor argument of the binding is a raw query. +@app.function_name(name="GetProducts") +@app.route(route="getproducts/{cost}") +@app.generic_input_binding(arg_name="products", type="sql", + CommandText="SELECT * FROM Products WHERE Cost = @Cost", + CommandType="Text", + Parameters="@Cost={cost}", + ConnectionStringSetting="SqlConnectionString", + data_type=DataType.STRING) +def get_products(req: func.HttpRequest, products: func.SqlRowList) -> func.HttpResponse: + rows = list(map(lambda r: json.loads(r.to_json()), products)) + + return func.HttpResponse( + json.dumps(rows), + status_code=200, + mimetype="application/json" + ) + +# The output binding upserts the product, which will insert it into the Products table if +# the primary key (ProductId) for that item doesn't exist. If it does then update it to +# have the new name and cost. +@app.function_name(name="AddProduct") +@app.route(route="addproduct") +@app.generic_output_binding(arg_name="product", type="sql", + CommandText="[dbo].[Products]", + ConnectionStringSetting="SqlConnectionString", + data_type=DataType.STRING) +def add_product(req: func.HttpRequest, product: func.Out[func.SqlRow]) -> func.HttpResponse: + body = json.loads(req.get_body()) + row = func.SqlRow.from_dict(body) + product.set(row) + + return func.HttpResponse( + body=req.get_body(), + status_code=201, + mimetype="application/json" + ) diff --git a/samples/samples-python-v2/host.json b/samples/samples-python-v2/host.json new file mode 100644 index 000000000..bb5d0fca7 --- /dev/null +++ b/samples/samples-python-v2/host.json @@ -0,0 +1,15 @@ +{ + "version": "2.0", + "logging": { + "applicationInsights": { + "samplingSettings": { + "isEnabled": true, + "excludedTypes": "Request" + } + } + }, + "extensionBundle": { + "id": "Microsoft.Azure.Functions.ExtensionBundle.Preview", + "version": "[4.0.0, 5.0.0)" + } +} \ No newline at end of file diff --git a/samples/samples-python-v2/local.settings.json b/samples/samples-python-v2/local.settings.json new file mode 100644 index 000000000..9f6f238ce --- /dev/null +++ b/samples/samples-python-v2/local.settings.json @@ -0,0 +1,10 @@ +{ + "IsEncrypted": false, + "Values": { + "FUNCTIONS_WORKER_RUNTIME": "python", + "AzureWebJobsStorage": "UseDevelopmentStorage=true", + "AzureWebJobsFeatureFlags": "EnableWorkerIndexing", + "SqlConnectionString": "", + "PYTHON_ISOLATE_WORKER_DEPENDENCIES": "1" + } +} \ No newline at end of file diff --git a/samples/samples-python-v2/requirements.txt b/samples/samples-python-v2/requirements.txt new file mode 100644 index 000000000..e97435339 --- /dev/null +++ b/samples/samples-python-v2/requirements.txt @@ -0,0 +1,5 @@ +# DO NOT include azure-functions-worker in this file +# The Python Worker is managed by Azure Functions platform +# Manually managing azure-functions-worker may cause unexpected issues + +azure-functions \ No newline at end of file