Skip to content

Commit 038bc26

Browse files
authored
Merge pull request #299919 from hallvictoria/hallvictoria/python-build25
Python & NodeJS Build updates
2 parents 513fa6f + 786eeef commit 038bc26

12 files changed

+1291
-827
lines changed

articles/azure-functions/functions-bindings-cosmosdb-v2-input.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ The following example shows a [C# function](functions-dotnet-class-library.md) t
246246
The example shows how to use a binding expression in the `SqlQuery` parameter. You can pass route data to the `SqlQuery` parameter as shown, but currently [you can't pass query string values](https://github.com/Azure/azure-functions-host/issues/2554#issuecomment-392084583).
247247

248248
> [!NOTE]
249-
> If you need to query by just the ID, it is recommended to use a look up, like the [previous examples](#http-trigger-look-up-id-from-query-string-c), as it will consume less [request units](/azure/cosmos-db/request-units). Point read operations (GET) are [more efficient](/azure/cosmos-db/optimize-cost-reads-writes) than queries by ID.
249+
> If you need to query by just the ID, it is recommended to use a lookup, like the [previous examples](#http-trigger-look-up-id-from-query-string-c), as it consumes less [request units](/azure/cosmos-db/request-units). Point read operations (GET) are [more efficient](/azure/cosmos-db/optimize-cost-reads-writes) than queries by ID.
250250
>
251251
252252
```cs
@@ -635,7 +635,7 @@ public class DocByIdFromRoute {
635635
The following example shows a Java function that retrieves a single document. The function is triggered by an HTTP request that uses a route parameter to specify the ID to look up. That ID is used to retrieve a document from the specified database and collection, converting the result set to a `ToDoItem[]`, since many documents may be returned, depending on the query criteria.
636636

637637
> [!NOTE]
638-
> If you need to query by just the ID, it is recommended to use a look up, like the [previous examples](#http-trigger-look-up-id-from-query-string---pojo-parameter-java), as it will consume less [request units](/azure/cosmos-db/request-units). Point read operations (GET) are [more efficient](/azure/cosmos-db/optimize-cost-reads-writes) than queries by ID.
638+
> If you need to query by just the ID, it is recommended to use a lookup, like the [previous examples](#http-trigger-look-up-id-from-query-string---pojo-parameter-java), as it consumes less [request units](/azure/cosmos-db/request-units). Point read operations (GET) are [more efficient](/azure/cosmos-db/optimize-cost-reads-writes) than queries by ID.
639639
>
640640
641641
```java
@@ -1260,6 +1260,34 @@ This section contains the following examples that read a single document by spec
12601260

12611261
The examples depend on whether you use the [v1 or v2 Python programming model](functions-reference-python.md).
12621262

1263+
### Using SDK-Type Bindings for Cosmos DB (Preview)
1264+
This example uses SDK types to directly access the underlying [`CosmosClient`](https://github.com/Azure/azure-functions-python-extensions/blob/dev/azurefunctions-extensions-bindings-cosmosdb/samples/cosmosdb_samples_cosmosclient/function_app.py) object provided by the Cosmos DB input binding:
1265+
1266+
The function loops through all the databases and logs their IDs.
1267+
```python
1268+
import logging
1269+
import azure.functions as func
1270+
import azurefunctions.extensions.bindings.cosmosdb as cosmos
1271+
1272+
app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
1273+
1274+
@app.route(route="cosmos")
1275+
@app.cosmos_db_input(arg_name="client",
1276+
connection="CosmosDBConnection",
1277+
database_name=None,
1278+
container_name=None)
1279+
def get_docs(req: func.HttpRequest, client: cosmos.CosmosClient):
1280+
databases = client.list_databases()
1281+
for db in databases:
1282+
logging.info(f"Found database with ID: {db.get('id')}")
1283+
1284+
return "ok"
1285+
```
1286+
1287+
For examples of using other SDK types, see the [`ContainerProxy`](https://github.com/Azure/azure-functions-python-extensions/blob/dev/azurefunctions-extensions-bindings-cosmosdb/samples/cosmosdb_samples_containerproxy/function_app.py) and [`DatabaseProxy`](https://github.com/Azure/azure-functions-python-extensions/blob/dev/azurefunctions-extensions-bindings-cosmosdb/samples/cosmosdb_samples_databaseproxy/function_app.py) samples. For a step-by-step tutorial on how to include SDK-type bindings in your function app, follow the [Python SDK Bindings for CosmosDB Sample](https://github.com/Azure-Samples/azure-functions-cosmosdb-sdk-bindings-python).
1288+
1289+
To learn more, including what other SDK type bindings are supported, see [SDK type bindings](functions-reference-python.md#sdk-type-bindings).
1290+
12631291
### Queue trigger, look up ID from JSON
12641292

12651293
The following example shows an Azure Cosmos DB input binding. The function reads a single document and updates the document's text value.
@@ -1682,6 +1710,15 @@ Updates to documents are not made automatically upon function exit. To update do
16821710
::: zone-end
16831711
::: zone pivot="programming-language-python"
16841712
Data is made available to the function via a `DocumentList` parameter. Changes made to the document are not automatically persisted.
1713+
Functions also support Python SDK type bindings for Azure Cosmos, which lets you work with data using these underlying SDK types:
1714+
1715+
+ [`ContainerProxy`](https://github.com/Azure/azure-functions-python-extensions/blob/dev/azurefunctions-extensions-bindings-cosmosdb/samples/cosmosdb_samples_containerproxy/function_app.py)
1716+
+ [`CosmosClient`](https://github.com/Azure/azure-functions-python-extensions/blob/dev/azurefunctions-extensions-bindings-cosmosdb/samples/cosmosdb_samples_cosmosclient/function_app.py)
1717+
+ [`DatabaseProxy`](https://github.com/Azure/azure-functions-python-extensions/blob/dev/azurefunctions-extensions-bindings-cosmosdb/samples/cosmosdb_samples_databaseproxy/function_app.py)
1718+
1719+
> [!IMPORTANT]
1720+
> > Support for CosmosDB SDK types for Python is in Preview and is only supported for the Python v2 programming model. For more information, see [SDK types in Python](./functions-reference-python.md#sdk-type-bindings).
1721+
16851722
::: zone-end
16861723

16871724
[!INCLUDE [functions-cosmosdb-connections](../../includes/functions-cosmosdb-connections.md)]

articles/azure-functions/functions-bindings-cosmosdb-v2.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,27 @@ Earlier versions of extensions in the isolated worker process only support bindi
199199

200200
:::zone-end
201201

202+
::: zone pivot="programming-language-python"
203+
204+
## SDK Binding Types
205+
206+
SDK Type support for Azure Cosmos is in Preview. Follow the [Python SDK Bindings for CosmosDB Sample](https://github.com/Azure-Samples/azure-functions-cosmosdb-sdk-bindings-python) to get started with SDK Types for Cosmos in Python.
207+
> [!IMPORTANT]
208+
> Using SDK type bindings requires the [Python v2 programming model](functions-reference-python.md?pivots=python-mode-decorators#sdk-type-bindings).
209+
210+
---
211+
| Binding | Parameter types | Samples |
212+
|----------------|----------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
213+
| CosmosDB input | [ContainerProxy],<br/>[CosmosClient],<br/>[DatabaseProxy]<br/> | [`ContainerProxy`](https://github.com/Azure/azure-functions-python-extensions/blob/dev/azurefunctions-extensions-bindings-cosmosdb/samples/cosmosdb_samples_containerproxy/function_app.py),<br/>[`CosmosClient`](https://github.com/Azure/azure-functions-python-extensions/tree/dev/azurefunctions-extensions-bindings-cosmosdb/samples/cosmosdb_samples_cosmosclient/function_app.py),<br/>[`DatabaseProxy`](https://github.com/Azure/azure-functions-python-extensions/tree/dev/azurefunctions-extensions-bindings-cosmosdb/samples/cosmosdb_samples_databaseproxy/function_app.py)<br/> |
214+
215+
---
216+
217+
[CosmosClient]: /python/api/azure-cosmos/azure.cosmos.cosmosclient
218+
[DatabaseProxy]: /python/api/azure-cosmos/azure.cosmos.databaseproxy
219+
[ContainerProxy]: /python/api/azure-cosmos/azure.cosmos.containerproxy
220+
221+
:::zone-end
222+
202223
## Exceptions and return codes
203224

204225
| Binding | Reference |

articles/azure-functions/functions-bindings-http-webhook-trigger.md

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,11 +414,11 @@ Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
414414
::: zone pivot="programming-language-python"
415415
# [v2](#tab/python-v2)
416416

417-
This example uses [HTTP streams](functions-reference-python.md#http-streams-preview) to return chunked response data.
417+
This example is an HTTP triggered function that uses [HTTP streams](functions-reference-python.md#http-streams) to return chunked response data. You might use these capabilities to support scenarios like sending event data through a pipeline for real time visualization or detecting anomalies in large sets of data and providing instant notifications.
418418

419419
:::code language="python" source="~/functions-python-extensions/azurefunctions-extensions-http-fastapi/samples/fastapi_samples_streaming_download/function_app.py" range="5-26" :::
420420

421-
To learn more, including how to enable HTTP streams in your project, see [HTTP streams](functions-reference-python.md#http-streams-preview).
421+
To learn more, including how to enable HTTP streams in your project, see [HTTP streams](functions-bindings-http-webhook-trigger.md?tabs=python-v2&pivots=programming-language-python#http-streams-1).
422422

423423
This example shows a trigger binding and a Python function that uses the binding. The function looks for a `name` parameter either in the query string or the body of the HTTP request.
424424

@@ -1011,10 +1011,86 @@ You can now stream requests to and responses from your HTTP endpoint in Node.js
10111011
::: zone pivot="programming-language-python"
10121012
### HTTP streams
10131013

1014-
HTTP streams support in Python lets you accept and return data from your HTTP endpoints using FastAPI request and response APIs enabled in your functions. These APIs enable the host to process data in HTTP messages as chunks instead of having to read an entire message into memory. For more information, see [HTTP streams in Python](./functions-reference-python.md#http-streams-preview)
1014+
HTTP streams support in Python lets you accept and return data from your HTTP endpoints using FastAPI request and response APIs enabled in your functions. These APIs enable the host to process data in HTTP messages as chunks instead of having to read an entire message into memory.
1015+
1016+
### Prerequisites
1017+
1018+
* [Azure Functions runtime](functions-versions.md?pivots=programming-language-python) version 4.34.1, or a later version.
1019+
* [Python](https://www.python.org/downloads/) version 3.8, or a later [supported version](functions-reference-python.md?tabs=get-started&pivots=python-mode-decorators#python-version).
1020+
1021+
### Enable HTTP streams
1022+
1023+
HTTP streams are disabled by default. You need to enable this feature in your application settings and also update your code to use the FastAPI package. Note that when enabling HTTP streams, the function app will default to using HTTP streaming, and the original HTTP functionality will not work.
1024+
1025+
1. Add the `azurefunctions-extensions-http-fastapi` extension package to the `requirements.txt` file in the project, which should include at least these packages:
1026+
1027+
:::code language="text" source="~/functions-python-extensions/azurefunctions-extensions-http-fastapi/samples/fastapi_samples_streaming_download/requirements.txt" range="5-6" :::
1028+
1029+
1. Add this code to the `function_app.py` file in the project, which imports the FastAPI extension:
1030+
1031+
:::code language="python" source="~/functions-python-extensions/azurefunctions-extensions-http-fastapi/samples/fastapi_samples_streaming_download/function_app.py" range="8" :::
1032+
1033+
1. When you deploy to Azure, add the following [application setting](./functions-how-to-use-azure-function-app-settings.md#settings) in your function app:
1034+
1035+
`"PYTHON_ENABLE_INIT_INDEXING": "1"`
1036+
1037+
When running locally, you also need to add these same settings to the `local.settings.json` project file.
1038+
1039+
### HTTP streams examples
1040+
1041+
After you enable the HTTP streaming feature, you can create functions that stream data over HTTP.
1042+
1043+
This example is an HTTP triggered function that receives and processes streaming data from a client in real time. It demonstrates streaming upload capabilities that can be helpful for scenarios like processing continuous data streams and handling event data from IoT devices.
1044+
1045+
:::code language="python" source="~/functions-python-extensions/azurefunctions-extensions-http-fastapi/samples/fastapi_samples_streaming_upload/function_app.py" range="5-25" :::
1046+
1047+
### Calling HTTP streams
1048+
1049+
You must use an HTTP client library to make streaming calls to a function's FastAPI endpoints. The client tool or browser you're using might not natively support streaming or could only return the first chunk of data.
1050+
1051+
You can use a client script like this to send streaming data to an HTTP endpoint:
1052+
1053+
```python
1054+
import httpx # Be sure to add 'httpx' to 'requirements.txt'
1055+
import asyncio
1056+
1057+
async def stream_generator(file_path):
1058+
chunk_size = 2 * 1024 # Define your own chunk size
1059+
with open(file_path, 'rb') as file:
1060+
while chunk := file.read(chunk_size):
1061+
yield chunk
1062+
print(f"Sent chunk: {len(chunk)} bytes")
1063+
1064+
async def stream_to_server(url, file_path):
1065+
timeout = httpx.Timeout(60.0, connect=60.0)
1066+
async with httpx.AsyncClient(timeout=timeout) as client:
1067+
response = await client.post(url, content=stream_generator(file_path))
1068+
return response
1069+
1070+
async def stream_response(response):
1071+
if response.status_code == 200:
1072+
async for chunk in response.aiter_raw():
1073+
print(f"Received chunk: {len(chunk)} bytes")
1074+
else:
1075+
print(f"Error: {response}")
1076+
1077+
async def main():
1078+
print('helloworld')
1079+
# Customize your streaming endpoint served from core tool in variable 'url' if different.
1080+
url = 'http://localhost:7071/api/streaming_upload'
1081+
file_path = r'<file path>'
1082+
1083+
response = await stream_to_server(url, file_path)
1084+
print(response)
1085+
1086+
if __name__ == "__main__":
1087+
asyncio.run(main())
1088+
```
1089+
10151090

10161091
>[!IMPORTANT]
1017-
> HTTP streams support for Python is currently in preview and is only supported for the Python v2 programming model.
1092+
> HTTP streams support for Python is generally available and is only supported for the Python v2 programming model.
1093+
10181094
::: zone-end
10191095
### Working with client identities
10201096

0 commit comments

Comments
 (0)