@@ -16,19 +16,19 @@ Tutorial: Celery and Flask Integration
16
16
:values: tutorial
17
17
18
18
.. meta::
19
- :keywords: flask, celery, integration, code example
19
+ :keywords: code example, batch, framework
20
20
21
21
Overview
22
22
--------
23
23
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
26
26
newsletters, and administrators to manage and send batch emails asynchronously.
27
27
28
28
Celery
29
29
~~~~~~
30
30
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
32
32
volumes of messages efficiently. It supports asynchronous processing and task
33
33
scheduling. For more information, see the `Celery webpage
34
34
<https://docs.celeryq.dev/en/main/index.html>`__.
@@ -51,40 +51,45 @@ This tutorial creates a modified version of the sample application in the
51
51
Prerequisites
52
52
~~~~~~~~~~~~~
53
53
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
55
55
this tutorial:
56
56
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
58
59
:atlas:`Get Started with Atlas </getting-started?tck=docs_driver_python>` page
59
60
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
61
62
the :atlas:`Create a Database </atlas-ui/databases/#create-a-database>` page
62
63
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.
65
68
- `Python 3.8 or later <https://www.python.org/downloads/>`__
66
69
67
- Set-up
68
- ~~~~~~
70
+ Setup
71
+ ~~~~~
69
72
70
73
.. procedure::
71
74
:style: connected
72
75
73
- .. step:: Create your project directory and structure.
76
+ .. step:: Create your project directory and structure
74
77
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:
76
79
77
80
.. code-block:: bash
78
81
79
82
mkdir newsletter
80
83
cd newsletter
84
+
85
+ The following files will hold the code for your application:
81
86
82
- - ``app.py``: The main entry point for your Flask application.
87
+ - ``app.py``: The main entry point for your Flask application
83
88
- ``config.py``: Configuration settings for your application, including
84
89
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
88
93
89
94
We recommend structuring your application to separate concerns, which can
90
95
make the application modular and more maintainable.
@@ -104,7 +109,7 @@ Set-up
104
109
└── static/
105
110
└── styles.css
106
111
107
- .. step:: Install the required Python packages.
112
+ .. step:: Install the required Python packages
108
113
109
114
Your application depends on the following libraries:
110
115
@@ -114,12 +119,12 @@ Set-up
114
119
- `Celery <https://docs.celeryq.dev/en/stable/>`__ to manage tasks, such
115
120
as sending batch emails
116
121
117
- .. tip:: Use a Virtual environment
122
+ .. tip:: Use a Virtual Environment
118
123
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.
123
128
124
129
Run the following ``pip`` command in your terminal to install the dependencies:
125
130
@@ -134,7 +139,7 @@ The ``config.py`` file contains the settings and credentials to perform the
134
139
following actions:
135
140
136
141
- 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
138
143
- Connect your application to your MongoDB server
139
144
140
145
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
148
153
MAIL_SERVER = 'smtp.gmail.com'
149
154
MAIL_PORT = 587
150
155
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'
152
157
MAIL_PASSWORD = '<app password>'
153
158
ALLOWED_IPS = ['127.0.0.1']
154
- MONGO_URI = '<connection- string>'
159
+ MONGO_URI = '<mongodb connection string>'
155
160
CELERY_BROKER_URL = 'amqp://guest:guest@localhost//'
156
161
RESULT_BACKEND = MONGO_URI + '/celery_results'
157
162
@@ -161,12 +166,12 @@ you generate an app password to use, rather than using your primary password.
161
166
For more information, see the `App Password settings
162
167
<https://myaccount.google.com/apppasswords>`__ in your Google Account.
163
168
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.
167
172
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
170
175
information, see the `Broker Settings
171
176
<https://docs.celeryq.dev/en/stable/userguide/configuration.html#broker-settings>`__
172
177
section of the Celery documentation.
@@ -200,13 +205,13 @@ Initialize Flask, MongoDB, and Celery by adding the following code to your
200
205
if __name__ == '__main__':
201
206
app.run(debug=True)
202
207
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
204
209
and configures your Celery task queue.
205
210
206
211
Define Your Routes
207
212
~~~~~~~~~~~~~~~~~~
208
213
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:
210
215
211
216
.. code-block:: python
212
217
@@ -270,9 +275,15 @@ application in this file.
270
275
Create Your Pages
271
276
~~~~~~~~~~~~~~~~~
272
277
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.
274
284
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.
276
287
277
288
.. code-block:: html
278
289
@@ -330,10 +341,11 @@ Copy the following code into your ``subscribe.html`` file:
330
341
</body>
331
342
</html>
332
343
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.
335
346
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:
337
349
338
350
.. code-block:: html
339
351
@@ -385,10 +397,6 @@ Copy the following code into your ``admin.html`` file:
385
397
</body>
386
398
</html>
387
399
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
-
392
400
Format Your Pages
393
401
~~~~~~~~~~~~~~~~~
394
402
@@ -480,22 +488,20 @@ Testing the Platform
480
488
~~~~~~~~~~~~~~~~~~~~
481
489
482
490
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.
484
492
485
493
You can use the following steps to test your application:
486
494
487
495
.. procedure::
488
496
:style: connected
489
497
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
495
499
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.
497
503
498
- .. step:: Start your application.
504
+ .. step:: Start your application
499
505
500
506
Use the following code to start your application:
501
507
@@ -509,7 +515,7 @@ You can use the following steps to test your application:
509
515
510
516
celery -A app.celery worker --loglevel=info
511
517
512
- .. step:: Create a subscriber.
518
+ .. step:: Create a subscriber
513
519
514
520
Navigate to ``localhost:5000`` in your browser to open the
515
521
:guilabel:`Subscribe to our Newsletter` page.
@@ -520,14 +526,14 @@ You can use the following steps to test your application:
520
526
<https://account.mongodb.com/account/login>`__ and navigate to the
521
527
``users`` collection in your ``newletter`` database.
522
528
523
- .. step:: Dispatch a newsletter.
529
+ .. step:: Dispatch a newsletter
524
530
525
531
Navigate to ``localhost:5000/admin`` in your browser to open the
526
532
:guilabel:`Send Newsletter` page. Enter the newsletter details and click
527
533
:guilabel:`Send`.
528
534
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:
531
537
532
538
.. code-block:: bash
533
539
@@ -536,26 +542,26 @@ You can use the following steps to test your application:
536
542
[2025-05-27 10:04:52,046: WARNING/ForkPoolWorker-7] Sending email to <subscriber_email>
537
543
[2025-05-27 10:04:53,474: WARNING/ForkPoolWorker-7] Email sent
538
544
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.
541
547
542
548
Next Steps
543
549
~~~~~~~~~~
544
550
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
546
552
manage subscriber data, and send batch emails. You can further enhance this
547
553
platform by integrating analytics, customizing email templates, and implementing
548
554
automated responses.
549
555
550
556
More Resources
551
557
--------------
552
558
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
554
560
resources:
555
561
556
562
- `Flask <https://flask.palletsprojects.com>`__
557
563
- `Flask Mail <https://pypi.org/project/Flask-Mail/#files>`__
558
564
- `Celery <https://docs.celeryq.dev/en/stable/>`__
559
- - :mdb-shell:`MongoDB Shell <>`
565
+ - `RabbitMQ <https://www.rabbitmq.com/docs/download>`__
560
566
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