Skip to content

Commit a877430

Browse files
committed
format
1 parent ca29f64 commit a877430

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

source/integrations/flask-celery-integration.txt

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -139,22 +139,25 @@ Define the necessary configurations by adding the following code to your ``confi
139139
import os
140140

141141
class Config:
142-
MAIL_SERVER = 'smtp.gmail.com'
143-
MAIL_PORT = 587
144-
MAIL_USE_TLS = True
145142
MAIL_USERNAME = '<username>' # Your email address without '@gmail.com'
146143
MAIL_PASSWORD = '<app password>'
147-
ALLOWED_IPS = ['127.0.0.1']
144+
MAIL_DEFAULT_SENDER = '<email address>'
148145
MONGO_URI = '<mongodb connection string>'
146+
ALLOWED_IPS = ['127.0.0.1']
147+
MAIL_SERVER = 'smtp.gmail.com'
148+
MAIL_PORT = 587
149+
MAIL_USE_TLS = True
149150
CELERY_BROKER_URL = 'amqp://guest:guest@localhost//'
150151
RESULT_BACKEND = MONGO_URI + '/celery_results'
151152

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.
153+
You must provide your Gmail credentials and email (``MAIL_USERNAME``, ``MAIL_PASSWORD``, and ``MAIL_DEFAULT_SENDER``) 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.
153154

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.
155+
You must also provide 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.
155156

156157
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.
157158

159+
The ``ALLOWED_IPS`` list is used to control access to the :guilabel:`Send Newsletter` page. The rest of the variables configure the Flask and Celery components.
160+
158161
Initialize Flask, MongoDB, and Celery
159162
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
160163

@@ -241,11 +244,11 @@ Define your ``send_emails()`` method by adding the following code to your ``task
241244
Define Your Routes
242245
~~~~~~~~~~~~~~~~~~
243246

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.
247+
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-newsletters`` routes. The optional ``methods`` parameter is used in some cases to define a list of allowable HTTP methods.
245248

246249
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``.
247250

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.
251+
The root and ``/admin`` routes render pages using the ``render_template()`` method. The ``/subscribe`` and ``/send-newsletters`` routes access request parameters in ``request.form[]`` to execute commands, and then return HTTP responses.
249252

250253
Define your routes by adding the following code to your ``routes.py`` file:
251254

@@ -313,7 +316,7 @@ Create Your Pages
313316

314317
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.
315318

316-
Subscribe Pages
319+
Subscribe Page
317320
```````````````
318321

319322
Copy the following code into your ``subscribe.html`` file to create your :guilabel:`Subscribe to Newsletter` page.
@@ -516,7 +519,7 @@ You can apply a style sheet to your templates by adding the following code to th
516519
color: #666;
517520
}
518521

519-
Testing the Platform
522+
Test the Application
520523
~~~~~~~~~~~~~~~~~~~~
521524

522525
After you complete the previous steps, you have a working application that uses MongoDB, Flask, and Celery to manage a newsletter platform.
@@ -532,6 +535,24 @@ You can use the following steps to test your application:
532535
documentation <https://www.rabbitmq.com/docs/platforms>`__ for your
533536
operating system.
534537

538+
On MacOS:
539+
540+
.. code-block:: bash
541+
542+
brew services start rabbitmq
543+
544+
On Windows:
545+
546+
.. code-block:: bash
547+
548+
rabbitmq-service start
549+
550+
On Linux/Unix:
551+
552+
.. code-block:: bash
553+
554+
sudo systemctl start rabbitmq-server
555+
535556
.. step:: Start your application
536557

537558
Use the following code to start your application:
@@ -548,7 +569,7 @@ You can use the following steps to test your application:
548569

549570
.. step:: Create a subscriber
550571

551-
Navigate to ``localhost:5000`` in your browser to open the
572+
Navigate to `<localhost:5000>`__ in your browser to open the
552573
:guilabel:`Subscribe to our Newsletter` page.
553574

554575
Enter the subscriber information and click :guilabel:`Subscribe`.
@@ -559,7 +580,7 @@ You can use the following steps to test your application:
559580

560581
.. step:: Dispatch a newsletter
561582

562-
Navigate to ``localhost:5000/admin`` in your browser to open the
583+
Navigate to `<localhost:5000/admin>`__ in your browser to open the
563584
:guilabel:`Send Newsletter` page. Enter the newsletter details and click
564585
:guilabel:`Send`.
565586

@@ -579,16 +600,12 @@ You can use the following steps to test your application:
579600
Next Steps
580601
~~~~~~~~~~
581602

582-
This application demonstrates how to integrate with the Celery task queue to
583-
manage subscriber data, and send batch emails. You can further enhance this
584-
platform by integrating analytics, customizing email templates, and implementing
585-
automated responses.
603+
This application demonstrates how to integrate a Flask application with the Celery task queue to manage subscriber data, and send batch emails. You can further enhance this platform by integrating analytics, customizing email templates, and implementing automated responses.
586604

587605
More Resources
588606
--------------
589607

590-
For more information about the components used in this tutorial, see the following
591-
resources:
608+
For more information about the components used in this tutorial, see the following resources:
592609

593610
- `Flask <https://flask.palletsprojects.com>`__
594611
- `Flask Mail <https://pypi.org/project/Flask-Mail/#files>`__

0 commit comments

Comments
 (0)