Skip to content

Commit 4188536

Browse files
committed
Minor formatting changes
1 parent 00bfbb4 commit 4188536

File tree

1 file changed

+75
-60
lines changed

1 file changed

+75
-60
lines changed

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

Lines changed: 75 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -89,106 +89,116 @@ For this sample code, the container will use the category as a logical partition
8989

9090
### Authenticate the client
9191

92-
#### [Sync](#tab/sync)
93-
9492
From the project directory, open the *app.py* file. In your editor, import the `os` and `json` modules. Then, import the `CosmosClient` and `PartitionKey` classes from the `azure.cosmos` module.
9593

94+
#### [Sync](#tab/sync)
95+
9696
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="imports":::
9797

98-
Create variables for the `COSMOS_ENDPOINT` and `COSMOS_KEY` environment variables using `os.environ`.
98+
#### [Async](#tab/async)
9999

100-
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="environment_variables":::
100+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" id="imports":::
101101

102-
Create a new client instance using the [`CosmosClient`](/python/api/azure-cosmos/azure.cosmos.cosmos_client.cosmosclient) class constructor and the two variables you created as parameters.
102+
---
103103

104-
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="create_client":::
104+
Create constants for the `COSMOS_ENDPOINT` and `COSMOS_KEY` environment variables using `os.environ`.
105+
106+
#### [Sync](#tab/sync)
107+
108+
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="environment_variables":::
105109

106110
#### [Async](#tab/async)
107111

108-
From the project directory, open the *app.py* file. In your editor, import the `os`, `json`, and `asyncio` modules. Then, import the `CosmosClient` and `PartitionKey` classes from the `azure.cosmos` module.
112+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" id="environment_variables":::
109113

110-
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" id="imports":::
114+
---
111115

112-
Create variables for the `COSMOS_ENDPOINT` and `COSMOS_KEY` environment variables using `os.environ`.
116+
Create constants for the database and container names.
113117

114-
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" id="environment_variables":::
118+
#### [Sync](#tab/sync)
115119

116-
Create constants for Database and Container names.
120+
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="constants":::
121+
122+
#### [Async](#tab/async)
117123

118124
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" id="constants":::
119125

120-
Define a coroutine function and create a new client instance using the [`CosmosClient`](/python/api/azure-cosmos/azure.cosmos.cosmos_client.cosmosclient) class constructor and the environment variables you created as parameters.
126+
---
127+
128+
Create a new client instance using the [`CosmosClient`](/python/api/azure-cosmos/azure.cosmos.cosmos_client.cosmosclient) class constructor and the two variables you created as parameters.
121129

122-
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="24-26":::
130+
#### [Sync](#tab/sync)
131+
132+
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="create_client":::
133+
134+
#### [Async](#tab/async)
135+
136+
> [!IMPORTANT]
137+
> Please the client instance in a coroutine function named `manage_cosmos`. Within the coroutine function, define the new client with the `async with` keywords. Outside of the coroutine function, use the `asyncio.run` function to execute the coroutine asynchronously.
138+
139+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="24-26,70":::
123140

124141
---
125142

126143
### Create a database
127144

128-
#### [Sync](#tab/sync)
129-
130145
Use the [`CosmosClient.create_database_if_not_exists`](/python/api/azure-cosmos/azure.cosmos.cosmos_client.cosmosclient#azure-cosmos-cosmos-client-cosmosclient-create-database-if-not-exists) method to create a new database if it doesn't already exist. This method will return a [`DatabaseProxy`](/python/api/azure-cosmos/azure.cosmos.databaseproxy) reference to the existing or newly created database.
131146

147+
#### [Sync](#tab/sync)
148+
132149
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="create_database":::
133150

134151
#### [Async](#tab/async)
135152

136-
Use the [`CosmosClient.create_database_if_not_exists`](/python/api/azure-cosmos/azure.cosmos.cosmos_client.cosmosclient#azure-cosmos-cosmos-client-cosmosclient-create-database-if-not-exists) method to create a new database if it doesn't already exist. This method will return a [`DatabaseProxy`](/python/api/azure-cosmos/azure.cosmos.databaseproxy) reference to the existing or newly created database.
137-
138153
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="28-29":::
139154

140155
---
141156

142157
### Create a container
143158

144-
#### [Sync](#tab/sync)
145-
146159
The [`PartitionKey`](/python/api/azure-cosmos/azure.cosmos.partitionkey) class defines a partition key path that you can use when creating a container.
147160

148-
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="create_partition_key":::
161+
#### [Sync](#tab/sync)
149162

150-
The [`Databaseproxy.create_container_if_not_exists`](/python/api/azure-cosmos/azure.cosmos.databaseproxy#azure-cosmos-databaseproxy-create-container-if-not-exists) method will create a new container if it doesn't already exist. This method will also return a [`ContainerProxy`](/python/api/azure-cosmos/azure.cosmos.containerproxy) reference to the container.
163+
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="create_partition_key":::
151164

152165
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="create_container":::
153166

154167
#### [Async](#tab/async)
155168

156-
The [`PartitionKey`](/python/api/azure-cosmos/azure.cosmos.partitionkey) class defines a partition key path that you can use when creating a container.
157-
158169
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="31":::
159170

160-
The [`Databaseproxy.create_container_if_not_exists`](/python/api/azure-cosmos/azure.cosmos.databaseproxy#azure-cosmos-databaseproxy-create-container-if-not-exists) method will create a new container if it doesn't already exist. This method will also return a [`ContainerProxy`](/python/api/azure-cosmos/azure.cosmos.containerproxy) reference to the container.
161-
162171
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="33-36":::
163172

164173
---
165174

175+
The [`Databaseproxy.create_container_if_not_exists`](/python/api/azure-cosmos/azure.cosmos.databaseproxy#azure-cosmos-databaseproxy-create-container-if-not-exists) method will create a new container if it doesn't already exist. This method will also return a [`ContainerProxy`](/python/api/azure-cosmos/azure.cosmos.containerproxy) reference to the container.
176+
166177
### Create an item
167178

168-
#### [Sync](#tab/sync)
179+
Create a new item in the container by first creating a new variable (`new_item`) with a sample item defined. In this example, the unique identifier of this item is `70b63682-b93a-4c77-aad2-65501347265f`. The partition key value is derived from the `/categoryId` path, so it would be `61dba35b-4f02-45c5-b648-c6badc0cbd79`.
169180

170-
Create a new item in the container by first creating a new variable (`newItem`) with a sample item defined. In this example, the unique identifier of this item is `70b63682-b93a-4c77-aad2-65501347265f`. The partition key value is derived from the `/categoryId` path, so it would be `61dba35b-4f02-45c5-b648-c6badc0cbd79`.
181+
#### [Sync](#tab/sync)
171182

172183
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="new_item":::
173184

174-
> [!TIP]
175-
> The remaining fields are flexible and you can define as many or as few as you want. You can even combine different item schemas in the same container.
176-
177-
Create an item in the container by using the [`ContainerProxy.create_item`](/python/api/azure-cosmos/azure.cosmos.containerproxy#azure-cosmos-containerproxy-create-item) method passing in the variable you already created.
178-
179-
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="create_item":::
180-
181185
#### [Async](#tab/async)
182186

183-
Create a new item in the container by first creating a new variable (`newItem`) with a sample item defined. In this example, the unique identifier of this item is `70b63682-b93a-4c77-aad2-65501347265f`. The partition key value is derived from the `/categoryId` path, so it would be `61dba35b-4f02-45c5-b648-c6badc0cbd79`.
184-
185187
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="38-45":::
186188

189+
---
190+
187191
> [!TIP]
188192
> The remaining fields are flexible and you can define as many or as few as you want. You can even combine different item schemas in the same container.
189193
190194
Create an item in the container by using the [`ContainerProxy.create_item`](/python/api/azure-cosmos/azure.cosmos.containerproxy#azure-cosmos-containerproxy-create-item) method passing in the variable you already created.
191195

196+
#### [Sync](#tab/sync)
197+
198+
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="create_item":::
199+
200+
#### [Async](#tab/async)
201+
192202
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="47":::
193203

194204
---
@@ -211,46 +221,46 @@ In this example, the dictionary result is saved to a variable named `existing_it
211221

212222
### Query items
213223

214-
#### [Sync](#tab/sync)
215-
216224
After you insert an item, you can run a query to get all items that match a specific filter. This example runs the SQL query: ``SELECT * FROM products p WHERE p.categoryId = "61dba35b-4f02-45c5-b648-c6badc0cbd79"``. This example uses query parameterization to construct the query. The query uses a string of the SQL query, and a dictionary of query parameters.
217225

226+
#### [Sync](#tab/sync)
227+
218228
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="build_query":::
219229

220-
This example dictionary included the `@categoryId` query parameter and the corresponding value `61dba35b-4f02-45c5-b648-c6badc0cbd79`.
230+
#### [Async](#tab/async)
221231

222-
Once the query is defined, call [`ContainerProxy.query_items`](/python/api/azure-cosmos/azure.cosmos.containerproxy#azure-cosmos-containerproxy-query-items) to run the query and return the results as a paged set of items (`ItemPage[Dict[str, Any]]`).
232+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="55-57":::
223233

224-
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="query_items":::
234+
---
225235

226-
Finally, use a for loop to iterate over the results in each page and perform various actions.
236+
This example dictionary included the `@categoryId` query parameter and the corresponding value `61dba35b-4f02-45c5-b648-c6badc0cbd79`.
227237

228-
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="iterate_query_results":::
238+
Once the query is defined, call [`ContainerProxy.query_items`](/python/api/azure-cosmos/azure.cosmos.containerproxy#azure-cosmos-containerproxy-query-items) to run the query and return the results as a paged set of items (`ItemPage[Dict[str, Any]]`).
229239

230-
In this example, `json.dumps` is used to print the item to the console in a human-readable way.
240+
#### [Sync](#tab/sync)
241+
242+
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="query_items":::
231243

232244
#### [Async](#tab/async)
233245

234-
After you insert an item, you can run a query to get all items that match a specific filter. This example runs the SQL query: ``SELECT * FROM products p WHERE p.categoryId = "61dba35b-4f02-45c5-b648-c6badc0cbd79"``. This example uses query parameterization to construct the query. The query uses a string of the SQL query, and a dictionary of query parameters.
246+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="59-61":::
235247

236-
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="55-57":::
248+
---
237249

238-
This example dictionary included the `@categoryId` query parameter and the corresponding value `61dba35b-4f02-45c5-b648-c6badc0cbd79`.
250+
Finally, use a for loop to iterate over the results in each page and perform various actions.
239251

240-
Once the query is defined, call [`ContainerProxy.query_items`](/python/api/azure-cosmos/azure.cosmos.containerproxy#azure-cosmos-containerproxy-query-items) to run the query and return the results as a paged set of items (`ItemPage[Dict[str, Any]]`).
252+
#### [Sync](#tab/sync)
241253

242-
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="59-61":::
254+
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="iterate_query_results":::
243255

244-
Finally, use a for loop to iterate over the results in each page and perform various actions.
256+
#### [Async](#tab/async)
245257

246258
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="62-64":::
247259

248-
Run the async function.
249-
250-
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="70":::
251-
252260
---
253261

262+
In this example, `json.dumps` is used to print the item to the console in a human-readable way.
263+
254264
## Run the code
255265

256266
This app creates an API for NoSQL database and container. The example then creates an item and then reads the exact same item back. Finally, the example issues a query that should only return that single item. At the final step, the example outputs the final item to the console.
@@ -264,19 +274,24 @@ python app.py
264274
The output of the app should be similar to this example:
265275

266276
```output
267-
{
277+
Database cosmicworks
278+
Container products
279+
Point read Yamba Surfboard
280+
Result list [
281+
{
268282
"id": "70b63682-b93a-4c77-aad2-65501347265f",
269283
"categoryId": "61dba35b-4f02-45c5-b648-c6badc0cbd79",
270284
"categoryName": "gear-surf-surfboards",
271285
"name": "Yamba Surfboard",
272286
"quantity": 12,
273287
"sale": false,
274-
"_rid": "yzN6AIfJxe0BAAAAAAAAAA==",
275-
"_self": "dbs/yzN6AA==/colls/yzN6AIfJxe0=/docs/yzN6AIfJxe0BAAAAAAAAAA==/",
276-
"_etag": "\"2a00ccd4-0000-0200-0000-63650e420000\"",
288+
"_rid": "KSsMAPI2fH0BAAAAAAAAAA==",
289+
"_self": "dbs/KSsMAA==/colls/KSsMAPI2fH0=/docs/KSsMAPI2fH0BAAAAAAAAAA==/",
290+
"_etag": "\"48002b76-0000-0200-0000-63c85f9d0000\"",
277291
"_attachments": "attachments/",
278-
"_ts": 16457527130
279-
}
292+
"_ts": 1674076061
293+
}
294+
]
280295
```
281296

282297
> [!NOTE]

0 commit comments

Comments
 (0)