1
- ========================================================================
2
- Building a Scalable Newsletter Platform with Flask, MongoDB, and Celery
3
- ========================================================================
1
+ ======================================
2
+ Tutorial: Flask and Celery Integration
3
+ ======================================
4
4
5
5
.. contents:: On this page
6
6
:local:
@@ -33,21 +33,28 @@ Prerequisites
33
33
Ensure you have the following components installed and set up before you start
34
34
this tutorial:
35
35
36
- - :ref:`MongoDB <pymongo-get-started-download-and-install>`: For data storage.
37
- - `RabbitMQ <https://www.rabbitmq.com/docs/download>`__: As the message broker for Celery.
38
- - `Gmail <www.gmail.com>`__: For sending emails via SMTP.
36
+ - :ref:`MongoDB <pymongo-get-started-create-deployment>`
37
+ - `RabbitMQ <https://www.rabbitmq.com/docs/download>`__ ( message broker for Celery)
38
+ - `Gmail <www.gmail.com>`__ (to use as an SMTP)
39
39
- `Python 3.8 or later <https://www.python.org/downloads/>`__
40
40
41
41
Set-up
42
42
~~~~~~
43
43
44
-
45
44
.. procedure::
46
45
:style: connected
47
46
48
47
.. step:: Install the required Python packages.
48
+
49
+ Your application depends on the following libraries:
50
+
51
+ - `Flask <https://flask.palletsprojects.com/en/stable/>`__ for handling the web server and routing
52
+ - `Flask Mail <https://pypi.org/project/Flask-Mail/>`__ for sending emails from your application
53
+ - :ref:`{+driver-long+} <pymongo-get-started-download-and-install>`
54
+ - `Celery <https://docs.celeryq.dev/en/stable/>`__ to manage tasks, such
55
+ as sending batch emails
49
56
50
- Run the following ``pip`` command in your terminal:
57
+ Run the following ``pip`` command in your terminal to install the dependencies :
51
58
52
59
.. code-block:: bash
53
60
58
65
We recommend structuring your application to separate concerns, which can
59
66
make the application modular and more maintainable.
60
67
61
- In your project directory, create the following directories and files :
68
+ In your project directory, create the following structure :
62
69
63
70
.. code-block:: none
64
71
76
83
Configure Your Application
77
84
~~~~~~~~~~~~~~~~~~~~~~~~~~
78
85
79
- In ``config.py``, define the necessary configurations by adding the following code:
86
+ Define the necessary configurations by adding the following code to your
87
+ ``config.py`` file:
80
88
81
89
.. code-block:: python
82
90
@@ -100,7 +108,8 @@ default sender email (``MAIL_DEFAULT_SENDER``) are set in your environment varia
100
108
Initialize Flask, MongoDB, and Celery
101
109
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102
110
103
- In ``app.py``, initialize Flask, MongoDB, and Celery by adding the following code:
111
+ Initialize Flask, MongoDB, and Celery by adding the following code to your
112
+ ``app.py`` file:
104
113
105
114
.. code-block:: python
106
115
@@ -123,10 +132,17 @@ In ``app.py``, initialize Flask, MongoDB, and Celery by adding the following cod
123
132
from routes import *
124
133
from tasks import *
125
134
135
+ Ensure that your connection string (``MONGOD_URI``) and broker url
136
+ (``CELERY_BROKER_URL``) are set in your environment variables. For more in
137
+ formation see the :ref:`Create a Connection String
138
+ <pymongo-get-started-connection-string>` section of this guide and the `Broker Settings
139
+ <https://docs.celeryq.dev/en/stable/userguide/configuration.html#broker-settings>`__
140
+ section of the Celery documentation.
141
+
126
142
Define Your Routes
127
143
~~~~~~~~~~~~~~~~~~
128
144
129
- In ``routes.py``, define the necessary routes by adding the following code:
145
+ Define the necessary routes by adding the following code to your ``routes.py`` file :
130
146
131
147
.. code-block:: python
132
148
@@ -157,15 +173,97 @@ In ``routes.py``, define the necessary routes by adding the following code:
157
173
email = request.form['email']
158
174
if db.users.find_one({'email': email}):
159
175
160
- After you complete these steps, you have a working application that
161
- uses MongoDB, Flask and Celery to manage a newsletter system.
176
+ After you complete these steps, you will have a working application that
177
+ uses MongoDB, Flask and Celery to manage a newsletter platform.
178
+
179
+ Testing the Platform
180
+ ~~~~~~~~~~~~~~~~~~~~
181
+
182
+ To test your application, run the following ``flask`` command in the terminal:
183
+
184
+ .. procedure::
185
+ :style: connected
186
+
187
+ .. step:: Start Your Application
188
+
189
+ .. code-block:: bash
190
+
191
+ flask --app app run
192
+
193
+ In another terminal, start the celery worker:
194
+
195
+ .. code-block:: bash
196
+
197
+ celery -A app.celery worker --loglevel=info
198
+
199
+ .. step:: Create a Subscriber
200
+
201
+ Navigate to ``localhost:5000`` in your browser to open the
202
+ :guilabel:`Subscribe to our Newsletter` page. The following image shows
203
+ the subscriber webpage:
204
+
205
+ .. image:: /includes/integrations/celery-subscriber-page.png
206
+ :alt: Screenshot of browser and subscriber page
207
+
208
+ Enter the subscriber information and click :guilabel:`Subscribe`.
209
+
210
+ To confirm that you created a new subscriber, run the following code in
211
+ your terminal to open a MongoDB Shell instance and view your collections:
212
+
213
+ .. code-block:: shell
214
+
215
+ mongosh
216
+ show dbs
217
+ use newsletter
218
+ show collections
219
+
220
+ .. step:: Dispatch a Newsletter
221
+
222
+ Navigate to ``localhost:5000/admin`` in your browser to open the
223
+ :guilabel:`Send Newsletter` page. The following image shows the admin
224
+ webpage:
225
+
226
+ .. image:: /includes/integrations/celery-admin-page.png
227
+ :alt: Screenshot of browser and admin
228
+
229
+ Enter the newsletter details and click :guilabel:`Send`.
230
+
231
+ Your Celery worker log should display an ``Email sent`` log entry, as
232
+ shown in the following image:
233
+
234
+ .. code-block:: bash
235
+
236
+ [2024-06-06 13:34:37,304: WARNING/ForkPoolWorker-4] Email sent
237
+ [2024-06-06 13:34:37,305: INFO/ForkPoolWorker-4] Task tasks.send_emails[b119bb9e-b2ef-4c85-b048-ca96e0e60ae1] succeeded in 17.155154566993588s: {'result': 'All emails sent'}
238
+
239
+ You can also see your newsletter deliverable by running the following
240
+ command in your MongoDB Shell to review your collections:
241
+
242
+ .. code-block:: shell
243
+
244
+ newsletter> show collections
245
+ deliveries
246
+ subscribers
247
+ newsletter>
248
+
249
+ .. step:: Review Your Sent Newsletter
250
+
251
+ Run the following commands in your MongoDB Shell to your previously sent
252
+ newsletters, also called ``deliveries``:
253
+
254
+ .. code-block:: shell
255
+
256
+ db.deliveries.find().pretty()
162
257
163
258
More Resources
164
259
--------------
165
260
166
- For more information about Flask and Celery integration , see the following
261
+ For more information about to components used in this tutorial , see the following
167
262
resources:
168
263
169
264
- `Flask <https://flask.palletsprojects.com>`__
170
265
- `Flask Mail <https://pypi.org/project/Flask-Mail/#files>`__
171
- - `Celery <https://docs.celeryq.dev/en/stable/>`__
266
+ - `Celery <https://docs.celeryq.dev/en/stable/>`__
267
+ - :mdb-shell:`MongoDB Shell <>`
268
+
269
+ For support or to contribute to the MongoDB Community, see the `MongoDB Developer Community <https://www.mongodb.com/community/>`__.
0 commit comments