Skip to content

Commit 1486035

Browse files
UbuntuUbuntu
authored andcommitted
.
1 parent e660d7a commit 1486035

3 files changed

Lines changed: 11 additions & 73 deletions

File tree

samples/web-app-cosmosdb-nosql-api/python/README.md

Lines changed: 9 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Azure Web App with Azure CosmosDB for MongoDB
1+
# Azure Web App with Azure CosmosDB for NoSQL API
22

3-
This sample demonstrates a Python Flask single-page web application called *Vacation Planner* hosted on an [Azure Web App](https://learn.microsoft.com/en-us/azure/app-service/overview). The app runs on an Azure App Service Plan and stores activity data in the `activities` collection of the `sampledb` MongoDB database on an [Azure CosmosDB for MongoDB](https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb/introduction) account.
3+
This sample demonstrates a Python Flask single-page web application called *Vacation Planner* hosted on an [Azure Web App](https://learn.microsoft.com/en-us/azure/app-service/overview). The app runs on an Azure App Service Plan and stores activity data in the `activities` container of the `sampledb` NoSQL database on an [Azure CosmosDB for NoSQL](https://learn.microsoft.com/en-us/azure/cosmos-db/distributed-nosql) account.
44

55
## Architecture
66

@@ -10,7 +10,7 @@ The following diagram illustrates the architecture of the solution:
1010

1111
- **Azure Web App**: Hosts the Python Flask application
1212
- **Azure App Service Plan**: Provides compute resources for the web app
13-
- **Azure CosmosDB for MongoDB**: Stores activity data in a MongoDB collection
13+
- **Azure CosmosDB for NoSQL API**: Stores activity data in a CosmosDB container
1414

1515
## Prerequisites
1616

@@ -19,8 +19,6 @@ The following diagram illustrates the architecture of the solution:
1919
- [Python 3.11+](https://www.python.org/downloads/)
2020
- [Flask](https://flask.palletsprojects.com/)
2121
- [pymongo](https://pymongo.readthedocs.io/en/stable/)
22-
- [Bicep extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-bicep), if you plan to install the sample via Bicep.
23-
- [Terraform](https://developer.hashicorp.com/terraform/downloads), if you plan to install the sample via Terraform.
2422

2523
## Deployment
2624

@@ -37,13 +35,9 @@ export LOCALSTACK_AUTH_TOKEN=<your_auth_token>
3735
IMAGE_NAME=localstack/localstack-azure-alpha localstack start
3836
```
3937

40-
Deploy the application to LocalStack for Azure using one of these methods:
38+
Deploy the application to LocalStack for Azure using:
4139

4240
- [Azure CLI Deployment](./scripts/README.md)
43-
- [Bicep Deployment](./bicep/README.md)
44-
- [Terraform Deployment](./terraform/README.md)
45-
46-
All deployment methods have been fully tested against Azure and the LocalStack for Azure local emulator.
4741

4842
> **Note**
4943
> When you deploy the application to LocalStack for Azure for the first time, the initialization process involves downloading and building Docker images. This is a one-time operation—subsequent deployments will be significantly faster. Depending on your internet connection and system resources, this initial setup may take several minutes.
@@ -54,7 +48,7 @@ All deployment methods have been fully tested against Azure and the LocalStack f
5448
2. Open a web browser and navigate to `http://localhost:<published-port>`.
5549
3. If the deployment was successful, you will see the following user interface for adding and removing activities:
5650

57-
![Architecture Diagram](./images/vacation-planner.png)
51+
![Architecture Diagram](./images/architecture.png)
5852

5953
You can use the `call-web-app.sh` Bash script below to call the web app. The script demonstrates three methods for calling web apps:
6054

@@ -267,70 +261,16 @@ call_web_app() {
267261
call_web_app
268262
```
269263

270-
## MongoDB Tooling
271-
272-
You can utilize [MongoDB Compass](https://www.mongodb.com/try/download/compass) to explore and manage your MongoDB databases and collections. Ensure you connect using `mongodb://localhost:port` connection string, where `port` corresponds to the port published by the MongoDB container on the host and mapped to the internal MongoDB port `27017`.
264+
## CosmosDB Tooling
273265

274-
![MongoDB Compass](./images/mongodb-compass.png)
266+
You can utilize [CosmosDB Data Explorer] to explore and manage your CosmosDB databases and collections. Ensure you connect using `http://localhost:port` connection string, where `port` corresponds to the port published by the CosmosDB container on the host and mapped to the internal CosmosDB port `1234`.
275267

276-
Alternatively, you can use the [MongoDB Shell](https://www.mongodb.com/docs/mongodb-shell/) to interact with and administer your MongoDB instance, as shown in the following table:
277-
278-
```bash
279-
~$ mongosh mongodb://localhost:32770
280-
Current Mongosh Log ID: 6914588406320f60899dc29c
281-
Connecting to: mongodb://localhost:32770/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.5.9
282-
Using MongoDB: 8.0.15
283-
Using Mongosh: 2.5.9
284-
285-
For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/
286-
287-
------
288-
The server generated these startup warnings when booting
289-
2025-11-12T09:28:07.726+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
290-
2025-11-12T09:28:07.892+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
291-
2025-11-12T09:28:07.892+00:00: For customers running the current memory allocator, we suggest changing the contents of the following sysfsFile
292-
2025-11-12T09:28:07.892+00:00: We suggest setting the contents of sysfsFile to 0.
293-
2025-11-12T09:28:07.892+00:00: vm.max_map_count is too low
294-
2025-11-12T09:28:07.892+00:00: We suggest setting swappiness to 0 or 1, as swapping can cause performance problems.
295-
------
296-
297-
test> show dbs
298-
admin 100.00 KiB
299-
config 108.00 KiB
300-
local 40.00 KiB
301-
sampledb 180.00 KiB
302-
test> use sampledb
303-
switched to db sampledb
304-
sampledb> show collections
305-
activities
306-
sampledb> db.activities.find().pretty()
307-
[
308-
{
309-
_id: '39ab62c2aaa0015ed5309876053e4146',
310-
username: 'Paolo',
311-
activity: 'Go to Paris',
312-
timestamp: '2025-11-12T09:31:43.338268'
313-
},
314-
{
315-
_id: '4fb8f53442d3ebe9167245f9555bac51',
316-
username: 'Paolo',
317-
activity: 'Go to Madrid',
318-
timestamp: '2025-11-12T09:31:50.109456'
319-
},
320-
{
321-
_id: '84646160cb1db21a7083b4c5b6e2d9d0',
322-
username: 'Paolo',
323-
activity: 'Go to Rome',
324-
timestamp: '2025-11-12T09:32:21.781936'
325-
}
326-
]
327-
```
268+
![CosmosDB Data Explorer](./images/nosql-api-data-explorer.png)
328269

329270
## References
330271

331272
- [Azure Web Apps Documentation](https://learn.microsoft.com/en-us/azure/app-service/)
332-
- [Azure CosmosDB for MongoDB API](https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb/introduction)
273+
- [Azure CosmosDB for MongoDB API](https://learn.microsoft.com/en-us/azure/cosmos-db/)
333274
- [Quickstart: Python Flask on Azure](https://learn.microsoft.com/en-us/azure/app-service/quickstart-python?tabs=flask%2Cbrowser)
334-
- [Quickstart: CosmosDB for MongoDB](https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb/quickstart?tabs=azure-portal)
335275
- [Azure Identity Client Library for Python](https://learn.microsoft.com/en-us/python/api/overview/azure/identity-readme?view=azure-python)
336276
- [LocalStack for Azure](https://azure.localstack.cloud/)

samples/web-app-cosmosdb-nosql-api/python/images/nosql-api-compass.png renamed to samples/web-app-cosmosdb-nosql-api/python/images/nosql-api-data-explorer.png

File renamed without changes.

samples/web-app-cosmosdb-nosql-api/python/src/app.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import datetime
33
import logging
44
import hashlib
5-
from typing import List, Tuple
65
from flask import Flask, render_template, request, redirect, url_for
76
from cosmosdb_client import CosmosDbClient
87

@@ -42,12 +41,12 @@ def get_cosmos():
4241
return cosmos_client
4342

4443
def create_document(activity: str) -> dict:
44+
get_cosmos().ensure_initialized()
45+
4546
timestamp = datetime.datetime.now().isoformat()
4647
id_string = f"{username}_{activity}_{timestamp}"
4748
document_id = hashlib.md5(id_string.encode()).hexdigest()
4849

49-
get_cosmos().ensure_initialized()
50-
5150
return {
5251
"id": document_id,
5352
"username": username,
@@ -57,7 +56,6 @@ def create_document(activity: str) -> dict:
5756

5857
def read_documents(username: str):
5958
get_cosmos().ensure_initialized()
60-
6159
get_activities().clear()
6260

6361
try:

0 commit comments

Comments
 (0)