Skip to content

Commit d0ec742

Browse files
committed
MW feedback
1 parent 60c52fd commit d0ec742

File tree

1 file changed

+66
-60
lines changed

1 file changed

+66
-60
lines changed

source/integrations/flask-celery-integration.txt

Lines changed: 66 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ Tutorial: Celery and Flask Integration
1616
:values: tutorial
1717

1818
.. meta::
19-
:keywords: flask, celery, integration, code example
19+
:keywords: code example, batch, framework
2020

2121
Overview
2222
--------
2323

24-
In this tutorial, you can learn how to build a newsletter platform using
25-
MongoDB, Celery, and Flask. This application allows users to subscribe to
24+
In this tutorial, you can learn how to use MongoDB, Celery, and Flask
25+
to build a newsletter platform. This application allows users to subscribe to
2626
newsletters, and administrators to manage and send batch emails asynchronously.
2727

2828
Celery
2929
~~~~~~
3030

31-
Celery is an open-source distributed task queue that makes handling large
31+
Celery is an open-source distributed task queue that handles large
3232
volumes of messages efficiently. It supports asynchronous processing and task
3333
scheduling. For more information, see the `Celery webpage
3434
<https://docs.celeryq.dev/en/main/index.html>`__.
@@ -51,40 +51,45 @@ This tutorial creates a modified version of the sample application in the
5151
Prerequisites
5252
~~~~~~~~~~~~~
5353

54-
Ensure you have the following components installed and set up before you start
54+
Ensure that you have the following components installed and set up before you start
5555
this tutorial:
5656

57-
- A MongoDB Cluster. We recommend setting up a cluster using Atlas. See the
57+
- A MongoDB cluster. We recommend that you use Atlas. To learn how
58+
to create an Atlas cluster, see the
5859
:atlas:`Get Started with Atlas </getting-started?tck=docs_driver_python>` page
5960
in the Atlas documentation.
60-
- A database in your cluster called ``newsletter``. For more information, see
61+
- A database names ``newsletter`` in your cluster. For more information, see
6162
the :atlas:`Create a Database </atlas-ui/databases/#create-a-database>` page
6263
in the Atlas guide.
63-
- `RabbitMQ <https://www.rabbitmq.com/docs/download>`__ (message broker for Celery)
64-
- `Gmail <www.gmail.com>`__ (to use as an SMTP)
64+
- `RabbitMQ <https://www.rabbitmq.com/docs/download>`__ to use as a message broker for Celery.
65+
- `Gmail <www.gmail.com>`__ to use as an SMTP server. For more information about
66+
SMTP servers, see the `Simple Mail Transfer Protocol
67+
<https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol>`__ wikipedia page.
6568
- `Python 3.8 or later <https://www.python.org/downloads/>`__
6669

67-
Set-up
68-
~~~~~~
70+
Setup
71+
~~~~~
6972

7073
.. procedure::
7174
:style: connected
7275

73-
.. step:: Create your project directory and structure.
76+
.. step:: Create your project directory and structure
7477

75-
The ``newsletter`` directory your the project directory for this tutorial. You can open
78+
The name of your project directory is ``newsletter``. Create your directory and navigate to it by running the following commands in terminal:
7679

7780
.. code-block:: bash
7881

7982
mkdir newsletter
8083
cd newsletter
84+
85+
The following files will hold the code for your application:
8186

82-
- ``app.py``: The main entry point for your Flask application.
87+
- ``app.py``: The main entry point for your Flask application
8388
- ``config.py``: Configuration settings for your application, including
8489
MongoDB connection details, mail server configuration, Celery broker
85-
connection, and any other environment-specific variables.
86-
- ``tasks.py``: Defines background tasks to send emails asynchronously.
87-
- ``routes.py``: Define the routes (URLs) that your application responds to.
90+
connection, and any other environment-specific variables
91+
- ``tasks.py``: Defines background tasks to send emails asynchronously
92+
- ``routes.py``: Defines the routes (URLs) that your application responds to
8893

8994
We recommend structuring your application to separate concerns, which can
9095
make the application modular and more maintainable.
@@ -104,7 +109,7 @@ Set-up
104109
└── static/
105110
└── styles.css
106111

107-
.. step:: Install the required Python packages.
112+
.. step:: Install the required Python packages
108113

109114
Your application depends on the following libraries:
110115

@@ -114,12 +119,12 @@ Set-up
114119
- `Celery <https://docs.celeryq.dev/en/stable/>`__ to manage tasks, such
115120
as sending batch emails
116121

117-
.. tip:: Use a Virtual environment
122+
.. tip:: Use a Virtual Environment
118123

119-
Installing your Python dependencies in a `virtualenv
120-
<https://docs.python.org/3/tutorial/venv.html>`__ allows you to install
121-
versions of your libraries for individual projects. Before running any
122-
``pip`` commands, ensure your ``virtualenv`` is active.
124+
Python `virtual environments
125+
<https://docs.python.org/3/tutorial/venv.html>`__ allow you to install
126+
different versions of libraries for different projects. Before running
127+
any ``pip`` commands, ensure that your ``virtualenv`` is active.
123128

124129
Run the following ``pip`` command in your terminal to install the dependencies:
125130

@@ -134,7 +139,7 @@ The ``config.py`` file contains the settings and credentials to perform the
134139
following actions:
135140

136141
- Connect Celery to RabbitMQ as its message broker
137-
- Configure Flask-Mail to use Gmail as its SMPT server
142+
- Configure Flask-Mail to use Gmail as its SMTP server
138143
- Connect your application to your MongoDB server
139144

140145
Define the necessary configurations by adding the following code to your
@@ -148,10 +153,10 @@ Define the necessary configurations by adding the following code to your
148153
MAIL_SERVER = 'smtp.gmail.com'
149154
MAIL_PORT = 587
150155
MAIL_USE_TLS = True
151-
MAIL_USERNAME = '<username>' # Your email address without the domain, that is without '@gmail.com'
156+
MAIL_USERNAME = '<username>' # Your email address without '@gmail.com'
152157
MAIL_PASSWORD = '<app password>'
153158
ALLOWED_IPS = ['127.0.0.1']
154-
MONGO_URI = '<connection-string>'
159+
MONGO_URI = '<mongodb connection string>'
155160
CELERY_BROKER_URL = 'amqp://guest:guest@localhost//'
156161
RESULT_BACKEND = MONGO_URI + '/celery_results'
157162

@@ -161,12 +166,12 @@ you generate an app password to use, rather than using your primary password.
161166
For more information, see the `App Password settings
162167
<https://myaccount.google.com/apppasswords>`__ in your Google Account.
163168

164-
You must also create a connection string (``MONGO_URI``) are set in your
165-
environment variables. For more information see the :ref:`Create a Connection
166-
String <pymongo-get-started-connection-string>` section of this guide
169+
You must also create a connection string to set into the ``MONGO_URI``
170+
environment variable. For more information see the :ref:`Create a Connection
171+
String <pymongo-get-started-connection-string>` section of this guide.
167172

168-
The provided broker url (``CELERY_BROKER_URL``) using RabbitMQ as the broker,
169-
but you can customize this url to support other implementations. For more
173+
The provided Celery broker URL (``CELERY_BROKER_URL``) specifies RabbitMQ as its broker,
174+
but you can customize this URL to support other implementations. For more
170175
information, see the `Broker Settings
171176
<https://docs.celeryq.dev/en/stable/userguide/configuration.html#broker-settings>`__
172177
section of the Celery documentation.
@@ -200,13 +205,13 @@ Initialize Flask, MongoDB, and Celery by adding the following code to your
200205
if __name__ == '__main__':
201206
app.run(debug=True)
202207

203-
This opens a connection to the ``newsletter`` database in your MongoDB cluster,
208+
This opens a connection to the ``newsletter`` database in your MongoDB cluster
204209
and configures your Celery task queue.
205210

206211
Define Your Routes
207212
~~~~~~~~~~~~~~~~~~
208213

209-
Define the root, admin, subscribe, and send-newsletter routes by adding the following code to your ``routes.py`` file:
214+
Define the ``root``, ``admin``, ``subscribe``, and ``send-newsletter`` routes by adding the following code to your ``routes.py`` file:
210215

211216
.. code-block:: python
212217

@@ -270,9 +275,15 @@ application in this file.
270275
Create Your Pages
271276
~~~~~~~~~~~~~~~~~
272277

273-
In the ``templates`` directory you can build your user interface.
278+
You can build your user interface in the ``templates`` directory.
279+
280+
Because this application uses asynchronous messages, the scripts in the
281+
following files use `Fetch API calls
282+
<https://en.wikipedia.org/wiki/XMLHttpRequest#Fetch_alternative>`__. They also
283+
handle timeouts and errors.
274284

275-
Copy the following code into your ``subscribe.html`` file:
285+
Copy the following code into your ``subscribe.html`` file to create your
286+
:guilabel:`Subscribe to Newsletter` page.
276287

277288
.. code-block:: html
278289

@@ -330,10 +341,11 @@ Copy the following code into your ``subscribe.html`` file:
330341
</body>
331342
</html>
332343

333-
Because this application uses asynchronous messages, the script in this file
334-
uses Fetch API calls. This script also handles timeouts and errors.
344+
The script for the admin page displays an alert to the user which depends on the
345+
success of the ``send_newsletter`` call.
335346

336-
Copy the following code into your ``admin.html`` file:
347+
Copy the following code into your ``admin.html`` file to create your
348+
:guilabel:`Send Newsletter` page:
337349

338350
.. code-block:: html
339351

@@ -385,10 +397,6 @@ Copy the following code into your ``admin.html`` file:
385397
</body>
386398
</html>
387399

388-
The script in this file also used Fetch for asynchronous calls. It also displays
389-
an alert to the user which depends on the success of the ``send_newsletter``
390-
call.
391-
392400
Format Your Pages
393401
~~~~~~~~~~~~~~~~~
394402

@@ -480,22 +488,20 @@ Testing the Platform
480488
~~~~~~~~~~~~~~~~~~~~
481489

482490
After you complete the previous steps, you have a working application that
483-
uses MongoDB, Flask and Celery to manage a newsletter platform.
491+
uses MongoDB, Flask, and Celery to manage a newsletter platform.
484492

485493
You can use the following steps to test your application:
486494

487495
.. procedure::
488496
:style: connected
489497

490-
.. step:: Start your background services.
491-
492-
Start your RabbitMQ node by running the following code:
493-
494-
.. code-block:: bash
498+
.. step:: Start your background services
495499

496-
brew services start rabbitmq
500+
Start your RabbitMQ node. For instructions, see the `RabbitMQ
501+
documentation <https://www.rabbitmq.com/docs/platforms>`__ for your
502+
operating system.
497503

498-
.. step:: Start your application.
504+
.. step:: Start your application
499505

500506
Use the following code to start your application:
501507

@@ -509,7 +515,7 @@ You can use the following steps to test your application:
509515

510516
celery -A app.celery worker --loglevel=info
511517

512-
.. step:: Create a subscriber.
518+
.. step:: Create a subscriber
513519

514520
Navigate to ``localhost:5000`` in your browser to open the
515521
:guilabel:`Subscribe to our Newsletter` page.
@@ -520,14 +526,14 @@ You can use the following steps to test your application:
520526
<https://account.mongodb.com/account/login>`__ and navigate to the
521527
``users`` collection in your ``newletter`` database.
522528

523-
.. step:: Dispatch a newsletter.
529+
.. step:: Dispatch a newsletter
524530

525531
Navigate to ``localhost:5000/admin`` in your browser to open the
526532
:guilabel:`Send Newsletter` page. Enter the newsletter details and click
527533
:guilabel:`Send`.
528534

529-
Your Celery worker log will display an ``Email sent`` log entry, as
530-
shown in the following image:
535+
Your Celery worker log will display an ``Email sent`` log entry similar to
536+
the following image:
531537

532538
.. code-block:: bash
533539

@@ -536,26 +542,26 @@ You can use the following steps to test your application:
536542
[2025-05-27 10:04:52,046: WARNING/ForkPoolWorker-7] Sending email to <subscriber_email>
537543
[2025-05-27 10:04:53,474: WARNING/ForkPoolWorker-7] Email sent
538544

539-
You can also confirm that you sent an email navigating to the
540-
``deliveries`` collection in your ``newletter`` database.
545+
You can also confirm that you sent an email by navigating to the
546+
``deliveries`` collection in your ``newsletter`` database.
541547

542548
Next Steps
543549
~~~~~~~~~~
544550

545-
This application demonstrates how to integrate with the Celery tasks task queue to
551+
This application demonstrates how to integrate with the Celery task queue to
546552
manage subscriber data, and send batch emails. You can further enhance this
547553
platform by integrating analytics, customizing email templates, and implementing
548554
automated responses.
549555

550556
More Resources
551557
--------------
552558

553-
For more information about to components used in this tutorial, see the following
559+
For more information about the components used in this tutorial, see the following
554560
resources:
555561

556562
- `Flask <https://flask.palletsprojects.com>`__
557563
- `Flask Mail <https://pypi.org/project/Flask-Mail/#files>`__
558564
- `Celery <https://docs.celeryq.dev/en/stable/>`__
559-
- :mdb-shell:`MongoDB Shell <>`
565+
- `RabbitMQ <https://www.rabbitmq.com/docs/download>`__
560566

561-
For support or to contribute to the MongoDB Community, see the `MongoDB Developer Community <https://www.mongodb.com/community/>`__.
567+
To find support or to contribute to the MongoDB community, see the `MongoDB Developer Community <https://www.mongodb.com/community/>`__ page.

0 commit comments

Comments
 (0)