Skip to content

Commit 5735e25

Browse files
authored
Merge pull request #283207 from alexwolfmsft/python-quickstart-changes
Update quickstart for azd
2 parents f499957 + b2fc391 commit 5735e25

File tree

3 files changed

+72
-60
lines changed

3 files changed

+72
-60
lines changed

articles/cosmos-db/mongodb/how-to-python-get-started.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ This article shows you how to connect to Azure Cosmos DB for MongoDB using the P
2020
> [!NOTE]
2121
> The [example code snippets](https://github.com/Azure-Samples/azure-cosmos-db-mongodb-python-getting-started) are available on GitHub as a Python project.
2222
23-
This article shows you how to communicate with the Azure Cosmos DBs API for MongoDB by using one of the open-source MongoDB client drivers for Python, [PyMongo](https://www.mongodb.com/docs/drivers/pymongo/).
23+
This article shows you how to communicate with the Azure Cosmos DB's API for MongoDB by using one of the open-source MongoDB client drivers for Python, [PyMongo](https://www.mongodb.com/docs/drivers/pymongo/).
2424

2525
## Prerequisites
2626

2727
* An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free).
2828
* [Python 3.8+](https://www.python.org/downloads/)
2929
* [Azure Command-Line Interface (CLI)](/cli/azure/) or [Azure PowerShell](/powershell/azure/)
30-
* [Azure Cosmos DB for MongoDB resource](quickstart-python.md#create-an-azure-cosmos-db-account)
30+
* [Azure Cosmos DB for MongoDB resource](/azure/cosmos-db/nosql/quickstart-portal)
3131

3232
## Create a new Python app
3333

48.2 KB
Loading

articles/cosmos-db/mongodb/quickstart-python.md

Lines changed: 70 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,118 +8,130 @@ ms.service: azure-cosmos-db
88
ms.subservice: mongodb
99
ms.devlang: python
1010
ms.topic: quickstart
11-
ms.date: 11/08/2022
11+
ms.date: 08/01/2024
1212
ms.custom: devx-track-azurecli, devx-track-python
13+
zone_pivot_groups: azure-cosmos-db-quickstart-env
1314
---
1415

1516
# Quickstart: Azure Cosmos DB for MongoDB for Python with MongoDB driver
1617

1718
[!INCLUDE[MongoDB](~/reusable-content/ce-skilling/azure/includes/cosmos-db/includes/appliesto-mongodb.md)]
1819

19-
> [!div class="op_single_selector"]
20-
>
21-
> * [.NET](quickstart-dotnet.md)
22-
> * [Python](quickstart-python.md)
23-
> * [Java](quickstart-java.md)
24-
> * [Node.js](quickstart-nodejs.md)
25-
> * [Go](quickstart-go.md)
26-
>
20+
[!INCLUDE[Developer Quickstart selector](includes/quickstart-dev-selector.md)]
2721

28-
Get started with the PyMongo package to create databases, collections, and documents within your Azure Cosmos DB resource. Follow these steps to install the package and try out example code for basic tasks.
22+
Get started with MongoDB to create databases, collections, and docs within your Azure Cosmos DB resource. Follow these steps to deploy a minimal solution to your environment using the Azure Developer CLI.
2923

30-
> [!NOTE]
31-
> The [example code snippets](https://github.com/Azure-Samples/azure-cosmos-db-mongodb-python-getting-started) are available on GitHub as a Python project.
32-
33-
In this quickstart, you'll communicate with the Azure Cosmos DB’s API for MongoDB by using one of the open-source MongoDB client drivers for Python, [PyMongo](https://www.mongodb.com/docs/drivers/pymongo/). Also, you'll use the [MongoDB extension commands](./custom-commands.md), which are designed to help you create and obtain database resources that are specific to the [Azure Cosmos DB capacity model](../resource-model.md).
24+
[API for MongoDB reference documentation](https://www.mongodb.com/docs/drivers/python-drivers/) | [pymongo package](https://pypi.org/project/pymongo/)
25+
| [Azure Developer CLI](/azure/developer/azure-developer-cli/overview)
3426

3527
## Prerequisites
3628

37-
* An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free).
38-
* [Python 3.8+](https://www.python.org/downloads/)
39-
* [Azure Command-Line Interface (CLI)](/cli/azure/) or [Azure PowerShell](/powershell/azure/)
29+
[!INCLUDE [Developer Quickstart prerequisites](includes/quickstart-dev-prereqs.md)]
4030

41-
### Prerequisite check
31+
## Setting up
4232

43-
* In a terminal or command window, run `python --version` to check that you have a recent version of Python.
44-
* Run ``az --version`` (Azure CLI) or `Get-Module -ListAvailable Az*` (Azure PowerShell) to check that you have the appropriate Azure command-line tools installed.
33+
Deploy this project's development container to your environment. Then, use the Azure Developer CLI (`azd`) to create an Azure Cosmos DB for MongoDB account and deploy a containerized sample application. The sample application uses the client library to manage, create, read, and query sample data.
4534

46-
## Setting up
35+
::: zone pivot="devcontainer-codespace"
4736

48-
This section walks you through creating an Azure Cosmos DB account and setting up a project that uses the MongoDB npm package.
37+
[![Open in GitHub Codespaces](https://img.shields.io/static/v1?style=for-the-badge&label=GitHub+Codespaces&message=Open&color=brightgreen&logo=github)](https://codespaces.new/alexwolfmsft/cosmos-db-mongodb-python-quickstart?template=false&quickstart=1&azure-portal=true)
4938

50-
### Create an Azure Cosmos DB account
39+
::: zone-end
5140

52-
This quickstart will create a single Azure Cosmos DB account using the API for MongoDB.
41+
::: zone pivot="devcontainer-vscode"
5342

54-
#### [Azure CLI](#tab/azure-cli)
43+
[![Open in Dev Container](https://img.shields.io/static/v1?style=for-the-badge&label=Dev+Containers&message=Open&color=blue&logo=visualstudiocode)](https://vscode.dev/redirect?url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https://github.com/alexwolfmsft/cosmos-db-mongodb-python-quickstart)
5544

56-
[!INCLUDE [Azure CLI - create resources](./includes/azure-cli-create-resource-group-and-resource.md)]
45+
::: zone-end
5746

58-
#### [PowerShell](#tab/azure-powershell)
47+
::: zone pivot="devcontainer-codespace"
5948

60-
[!INCLUDE [Powershell - create resource group and resources](./includes/powershell-create-resource-group-and-resource.md)]
49+
> [!IMPORTANT]
50+
> GitHub accounts include an entitlement of storage and core hours at no cost. For more information, see [included storage and core hours for GitHub accounts](https://docs.github.com/billing/managing-billing-for-github-codespaces/about-billing-for-github-codespaces#monthly-included-storage-and-core-hours-for-personal-accounts).
6151
62-
#### [Portal](#tab/azure-portal)
52+
::: zone-end
6353

64-
[!INCLUDE [Portal - create resource](./includes/portal-create-resource.md)]
54+
::: zone pivot="devcontainer-vscode"
6555

66-
---
56+
::: zone-end
6757

68-
### Get MongoDB connection string
58+
1. Open a terminal in the root directory of the project.
6959

70-
#### [Azure CLI](#tab/azure-cli)
60+
1. Authenticate to the Azure Developer CLI using `azd auth login`. Follow the steps specified by the tool to authenticate to the CLI using your preferred Azure credentials.
7161

72-
[!INCLUDE [Azure CLI - get connection string](./includes/azure-cli-get-connection-string.md)]
62+
```azurecli
63+
azd auth login
64+
```
7365
74-
#### [PowerShell](#tab/azure-powershell)
66+
1. Use `azd init` to initialize the project.
7567
76-
[!INCLUDE [Powershell - get connection string](./includes/powershell-get-connection-string.md)]
68+
```azurecli
69+
azd init --template cosmos-db-mongodb-python-quickstart
70+
```
7771
78-
#### [Portal](#tab/azure-portal)
72+
> [!NOTE]
73+
> This quickstart uses the [azure-samples/cosmos-db-mongodb-python-quickstart](https://github.com/alexwolfmsft/cosmos-db-mongodb-python-quickstart) template GitHub repository. The Azure Developer CLI automatically clones this project to your machine if it is not already there.
7974
80-
[!INCLUDE [Portal - get connection string](./includes/portal-get-connection-string-from-resource.md)]
75+
1. During initialization, configure a unique environment name.
8176
82-
---
77+
> [!TIP]
78+
> The environment name will also be used as the target resource group name. For this quickstart, consider using `msdocs-cosmos-db`.
8379
84-
### Create a new Python app
80+
1. Deploy the Azure Cosmos DB account using `azd up`. The Bicep templates also deploy a sample web application.
8581
86-
1. Create a new empty folder using your preferred terminal and change directory to the folder.
82+
```azurecli
83+
azd up
84+
```
8785
88-
> [!NOTE]
89-
> If you just want the finished code, download or fork and clone the [example code snippets](https://github.com/Azure-Samples/azure-cosmos-db-mongodb-python-getting-started) repo that has the full example. You can also `git clone` the repo in Azure Cloud Shell to walk through the steps shown in this quickstart.
86+
1. During the provisioning process, select your subscription and desired location. Wait for the provisioning process to complete. The process can take **approximately five minutes**.
87+
88+
1. Once the provisioning of your Azure resources is done, a URL to the running web application is included in the output.
89+
90+
```output
91+
Deploying services (azd deploy)
92+
93+
(✓) Done: Deploying service web
94+
- Endpoint: <https://[container-app-sub-domain].azurecontainerapps.io>
95+
96+
SUCCESS: Your application was provisioned and deployed to Azure in 5 minutes 0 seconds.
97+
```
98+
99+
1. Use the URL in the console to navigate to your web application in the browser. Observe the output of the running app.
90100
91-
2. Create a *requirements.txt* file that lists the [PyMongo](https://www.mongodb.com/docs/drivers/pymongo/) and [python-dotenv](https://pypi.org/project/python-dotenv/) packages.
101+
:::image type="content" source="media/quickstart-python/cosmos-python-app.png" alt-text="Screenshot of the running web application.":::
92102
93-
```text
103+
---
104+
105+
### Install the client library
106+
107+
1. Create a `requirements.txt` file in your app directory that lists the [PyMongo](https://www.mongodb.com/docs/drivers/pymongo/) and [python-dotenv](https://pypi.org/project/python-dotenv/) packages.
108+
109+
```bash
94110
# requirements.txt
95111
pymongo
96112
python-dotenv
97113
```
98114
99-
3. Create a virtual environment and install the packages.
115+
1. Create a virtual environment and install the packages.
116+
117+
## [Windows](#tab/windows-package)
100118
101-
#### [Windows](#tab/venv-windows)
102-
103119
```bash
104120
# py -3 uses the global python interpreter. You can also use python3 -m venv .venv.
105121
py -3 -m venv .venv
106122
source .venv/Scripts/activate
107123
pip install -r requirements.txt
108124
```
109-
110-
#### [Linux / macOS](#tab/venv-linux+macos)
111-
125+
126+
## [Linux/macOS](#tab/linux-package)
127+
112128
```bash
113129
python3 -m venv .venv
114130
source .venv/bin/activate
115131
pip install -r requirements.txt
116132
```
117-
118-
---
119-
120-
### Configure environment variables
121133
122-
[!INCLUDE [Multi-tab](./includes/environment-variables-connection-string.md)]
134+
---
123135
124136
## Object model
125137
@@ -169,7 +181,7 @@ For the steps below, the database won't use sharding and shows a synchronous app
169181
170182
:::code language="python" source="~/azure-cosmos-db-mongodb-python-getting-started/001-quickstart/run.py" id="constant_values":::
171183
172-
### Connect to Azure Cosmos DBs API for MongoDB
184+
### Connect to Azure Cosmos DB's API for MongoDB
173185
174186
Use the [MongoClient](https://pymongo.readthedocs.io/en/stable/api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient) object to connect to your Azure Cosmos DB for MongoDB resource. The connect method returns a reference to the database.
175187
@@ -271,4 +283,4 @@ Remove-AzResourceGroup @parameters
271283

272284
1. On the **Are you sure you want to delete** dialog, enter the name of the resource group, and then select **Delete**.
273285

274-
---
286+
---

0 commit comments

Comments
 (0)