You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/integrations/flask-celery-integration.txt
+86-54Lines changed: 86 additions & 54 deletions
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
.. original URL: https://www.mongodb.com/developer/products/mongodb/python-flask-celery-newsletter/
3
3
4
4
======================================
5
-
Tutorial: Celery and Flask Integration
5
+
Tutorial: Flask and Celery Integration
6
6
======================================
7
7
8
8
.. contents:: On this page
@@ -21,38 +21,29 @@ Tutorial: Celery and Flask Integration
21
21
Overview
22
22
--------
23
23
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
-
newsletters, and administrators to manage and send batch emails asynchronously.
27
-
28
-
Celery
29
-
~~~~~~
30
-
31
-
Celery is an open-source distributed task queue that handles large
32
-
volumes of messages efficiently. It supports asynchronous processing and task
33
-
scheduling. For more information, see the `Celery webpage
34
-
<https://docs.celeryq.dev/en/main/index.html>`__.
24
+
In this tutorial, you can learn how to use MongoDB, Flask, and Celery to build a newsletter platform. This application allows users to subscribe to newsletters, and administrators to manage and send batch emails asynchronously.
35
25
36
26
Flask
37
27
~~~~~
38
28
39
29
Flask is a lightweight web application framework with built-in configuration and
40
30
convention defaults that provide consistency to developers across projects. For
more information, see the `Flask webpage <https://flask.palletsprojects.com/en/stable/>`__.
32
+
33
+
Celery
34
+
~~~~~~
35
+
36
+
Celery is an open-source distributed task queue that handles large volumes of messages efficiently. It supports asynchronous processing and task scheduling. For more information, see the `Celery webpage <https://docs.celeryq.dev/en/main/index.html>`__.
43
37
44
38
Tutorial
45
39
--------
46
40
47
-
This tutorial creates a modified version of the sample application in the
48
-
:github:`Newsletter Platform with JavaScript, Flask, and MongoDB sample project
This tutorial creates a modified version of the sample application in the :github:`Newsletter Platform with JavaScript, Flask, and MongoDB sample project </mercybassey/newsletter-javascript-flask-mongodb>` GitHub repository.
50
42
51
43
Prerequisites
52
44
~~~~~~~~~~~~~
53
45
54
-
Ensure that you have the following components installed and set up before you start
55
-
this tutorial:
46
+
Ensure that you have the following components installed and set up before you start this tutorial:
56
47
57
48
- A MongoDB cluster. We recommend that you use Atlas. To learn how
58
49
to create an Atlas cluster, see the
@@ -65,7 +56,7 @@ this tutorial:
65
56
- `Gmail <www.gmail.com>`__ to use as an SMTP server. For more information about
66
57
SMTP servers, see the :wikipedia:`Simple Mail Transfer Protocol
67
58
<Simple_Mail_Transfer_Protocol>` Wikipedia page.
68
-
- `Python 3.8 or later <https://www.python.org/downloads/>`__
59
+
- `Python 3.9 or later <https://www.python.org/downloads/>`__
69
60
70
61
Setup
71
62
~~~~~
@@ -130,20 +121,18 @@ Setup
130
121
131
122
.. code-block:: bash
132
123
133
-
pip install Flask Flask-Mail pymongo celery
124
+
pip install flask-pymongo Flask-Mail celery
134
125
135
126
Configure Your Application
136
127
~~~~~~~~~~~~~~~~~~~~~~~~~~
137
128
138
-
The ``config.py`` file contains the settings and credentials to perform the
139
-
following actions:
129
+
The ``config.py`` file contains the settings and credentials to perform the following actions:
140
130
141
131
- Connect Celery to RabbitMQ as its message broker
142
132
- Configure Flask-Mail to use Gmail as its SMTP server
143
133
- Connect your application to your MongoDB server
144
134
145
-
Define the necessary configurations by adding the following code to your
146
-
``config.py`` file:
135
+
Define the necessary configurations by adding the following code to your ``config.py`` file:
147
136
148
137
.. code-block:: python
149
138
@@ -160,25 +149,22 @@ Define the necessary configurations by adding the following code to your
You must provide your Gmail credentials (``MAIL_USERNAME`` and ``MAIL_PASSWORD``) to
164
-
enable your application to send emails. For security purposes, we recommend that
165
-
you generate an app password to use, rather than using your primary password.
166
-
For more information, see the `App Password settings
167
-
<https://myaccount.google.com/apppasswords>`__ in your Google Account.
152
+
You must provide your Gmail credentials (``MAIL_USERNAME`` and ``MAIL_PASSWORD``) to enable your application to send emails. For security purposes, we recommend that you generate an app password to use, rather than using your primary password. For more information, see the `App Password settings <https://myaccount.google.com/apppasswords>`__ in your Google Account.
168
153
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.
154
+
You must also create a connection string to set into the ``MONGO_URI`` environment variable. For more information see the :ref:`Create a Connection String <pymongo-get-started-connection-string>` section of this guide.
172
155
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
The provided Celery broker URL (``CELERY_BROKER_URL``) specifies RabbitMQ as its broker, but you can customize this URL to support other implementations. For more information, see the `Broker Settings <https://docs.celeryq.dev/en/stable/userguide/configuration.html#broker-settings>`__ section of the Celery documentation.
178
157
179
158
Initialize Flask, MongoDB, and Celery
180
159
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
181
160
161
+
The ``app.py`` file initializes and configures the core components of the Flask application. It performs the following tasks:
162
+
163
+
- Creates a Flask application and loads configuration constants
164
+
- Initializes a Flask-Mail instance with the app's mail server settings
165
+
- Connects to the ``newsletter`` MongoDB database by using the {+driver-short+} driver
166
+
- Creates a Celery instance configured with the Flask app and your chosen broker
167
+
182
168
Initialize Flask, MongoDB, and Celery by adding the following code to your
183
169
``app.py`` file:
184
170
@@ -189,13 +175,18 @@ Initialize Flask, MongoDB, and Celery by adding the following code to your
@@ -205,13 +196,58 @@ Initialize Flask, MongoDB, and Celery by adding the following code to your
205
196
if __name__ == '__main__':
206
197
app.run(debug=True)
207
198
208
-
This opens a connection to the ``newsletter`` database in your MongoDB cluster
209
-
and configures your Celery task queue.
199
+
Create a Celery Task
200
+
~~~~~~~~~~~~~~~~~~~~
201
+
202
+
The Celery task uses the components instantiated in your ``app.py`` file to send a newsletter email to subscribers.
203
+
204
+
The ``@celery.task()`` decorator registers the function as a Celery task. Setting ``bind=True`` means the function receives the task instance as the ``self`` argument, which allows it to access Celery task methods and metadata. For more information about tasks, see the `celery.app.task <https://docs.celeryq.dev/en/stable/reference/celery.app.task.html>`__ API documentation.
205
+
206
+
Since this task runs outside of Flask's HTTP request cycle, you must manually provide application context by wrapping the email logic in a ``with app.app_context()`` block. This gives Flask access to other components like the Flask-Mail ``mail`` instance and the {+driver-short+} connection to your ``newsletter`` MongoDB database.
207
+
208
+
This method loops through the list of ``subscribers``, creates an email using the Flask-Mail ``Message`` class, and then sends it to each user by using the ``mail`` object. After each email is sent, it logs the delivery by inserting a document into your MongoDB ``deliveries`` collection to record that the message was sent. Each email operation is wrapped in a ``try`` block to ensure that in the case of an error, the failure is logged and the database is not updated with a false delivery record.
209
+
210
+
Define your ``send_emails()`` method by adding the following code to your ``tasks.py`` file:
print(f"Failed to send email to {subscriber['email']}: {str(e)}")
237
+
238
+
return {'result': 'All emails sent'}
239
+
210
240
211
241
Define Your Routes
212
242
~~~~~~~~~~~~~~~~~~
213
243
214
-
Define the ``root``, ``admin``, ``subscribe``, and ``send-newsletter`` routes by adding the following code to your ``routes.py`` file:
244
+
In Flask, the ``@app.route()`` decorator assigns a URL path to a specific function. In the following code, it is used to define the root (``/``), ``/admin``, ``/subscribe``, and ``/send-newsletter`` routes. The optional ``methods`` parameter is used in some cases to define a list of allowable HTTP methods.
245
+
246
+
The ``@app.before_request()`` method sets a function to run before every request. In this case, the function provides some basic security by limiting access to the ``admin`` page to IP addresses listed in the ``ALLOWED_IPS`` parameter defined in the ``config.py`` file. Specifically, access is only allowed for the ``localhost``.
247
+
248
+
The root and ``/admin`` routes render pages using the ``render_template()`` method. The ``/subscribe`` and ``/send-newsletter`` routes access request parameters in ``request.form[]`` to execute commands, and then return HTTP responses.
249
+
250
+
Define your routes by adding the following code to your ``routes.py`` file:
215
251
216
252
.. code-block:: python
217
253
@@ -275,14 +311,12 @@ application in this file.
275
311
Create Your Pages
276
312
~~~~~~~~~~~~~~~~~
277
313
278
-
You can build your user interface in the ``templates`` directory.
314
+
The HTML files in the ``templates`` directory define the user interface, and are written using standard HTML. Since this application uses asynchronous HTTP requests, the scripts in these files use :wikipedia:`Fetch API calls <XMLHttpRequest#Fetch_alternative>`. These scripts also handle timeouts and errors.
279
315
280
-
Because this application uses asynchronous messages, the scripts in the
281
-
following files use :wikipedia:`Fetch API calls <XMLHttpRequest#Fetch_alternative>`. They also
282
-
handle timeouts and errors.
316
+
Subscribe Pages
317
+
```````````````
283
318
284
-
Copy the following code into your ``subscribe.html`` file to create your
285
-
:guilabel:`Subscribe to Newsletter` page.
319
+
Copy the following code into your ``subscribe.html`` file to create your :guilabel:`Subscribe to Newsletter` page.
286
320
287
321
.. code-block:: html
288
322
@@ -340,6 +374,9 @@ Copy the following code into your ``subscribe.html`` file to create your
340
374
</body>
341
375
</html>
342
376
377
+
Admin Page
378
+
```````````
379
+
343
380
The script for the admin page displays an alert to the user that depends on the
344
381
success of the ``send_newsletter`` call.
345
382
@@ -399,8 +436,7 @@ Copy the following code into your ``admin.html`` file to create your
399
436
Format Your Pages
400
437
~~~~~~~~~~~~~~~~~
401
438
402
-
You can apply a style sheet to your templates by adding the following code to
403
-
the ``styles.css`` file:
439
+
You can apply a style sheet to your templates by adding the following code to the ``styles.css`` file:
404
440
405
441
.. code-block:: css
406
442
@@ -480,14 +516,10 @@ the ``styles.css`` file:
480
516
color: #666;
481
517
}
482
518
483
-
You can modify this style sheet or create your own to customize your
484
-
application.
485
-
486
519
Testing the Platform
487
520
~~~~~~~~~~~~~~~~~~~~
488
521
489
-
After you complete the previous steps, you have a working application that
490
-
uses MongoDB, Flask, and Celery to manage a newsletter platform.
522
+
After you complete the previous steps, you have a working application that uses MongoDB, Flask, and Celery to manage a newsletter platform.
491
523
492
524
You can use the following steps to test your application:
0 commit comments