1- ========================================================================
2- Building a Scalable Newsletter Platform with Flask, MongoDB, and Celery
3- ========================================================================
1+ ======================================
2+ Tutorial: Flask and Celery Integration
3+ ======================================
44
55.. contents:: On this page
66 :local:
@@ -33,21 +33,28 @@ Prerequisites
3333Ensure you have the following components installed and set up before you start
3434this tutorial:
3535
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)
3939- `Python 3.8 or later <https://www.python.org/downloads/>`__
4040
4141Set-up
4242~~~~~~
4343
44-
4544.. procedure::
4645 :style: connected
4746
4847 .. 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
4956
50- Run the following ``pip`` command in your terminal:
57+ Run the following ``pip`` command in your terminal to install the dependencies :
5158
5259 .. code-block:: bash
5360
5865 We recommend structuring your application to separate concerns, which can
5966 make the application modular and more maintainable.
6067
61- In your project directory, create the following directories and files :
68+ In your project directory, create the following structure :
6269
6370 .. code-block:: none
6471
7683Configure Your Application
7784~~~~~~~~~~~~~~~~~~~~~~~~~~
7885
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:
8088
8189.. code-block:: python
8290
@@ -100,7 +108,8 @@ default sender email (``MAIL_DEFAULT_SENDER``) are set in your environment varia
100108Initialize Flask, MongoDB, and Celery
101109~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102110
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:
104113
105114.. code-block:: python
106115
@@ -123,10 +132,17 @@ In ``app.py``, initialize Flask, MongoDB, and Celery by adding the following cod
123132 from routes import *
124133 from tasks import *
125134
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+
126142Define Your Routes
127143~~~~~~~~~~~~~~~~~~
128144
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 :
130146
131147.. code-block:: python
132148
@@ -157,15 +173,97 @@ In ``routes.py``, define the necessary routes by adding the following code:
157173 email = request.form['email']
158174 if db.users.find_one({'email': email}):
159175
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()
162257
163258More Resources
164259--------------
165260
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
167262resources:
168263
169264- `Flask <https://flask.palletsprojects.com>`__
170265- `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