<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Gidraf Orenja on Medium]]></title>
        <description><![CDATA[Stories by Gidraf Orenja on Medium]]></description>
        <link>https://medium.com/@gidraf-orenja?source=rss-175ac4d02a00------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*9Sx4zDJKD_JxCqpGKKpJ2g.jpeg</url>
            <title>Stories by Gidraf Orenja on Medium</title>
            <link>https://medium.com/@gidraf-orenja?source=rss-175ac4d02a00------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 00:40:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gidraf-orenja/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to create a Microservice to send Email with Celery, Python, Flask & Redis using Gmail API.]]></title>
            <link>https://medium.com/the-andela-way/create-a-simple-microservice-with-celery-python-flask-redis-to-send-emails-with-gmail-api-224cc74ac7b3?source=rss-175ac4d02a00------2</link>
            <guid isPermaLink="false">https://medium.com/p/224cc74ac7b3</guid>
            <category><![CDATA[flask]]></category>
            <category><![CDATA[redis]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[the-andela-way]]></category>
            <dc:creator><![CDATA[Gidraf Orenja]]></dc:creator>
            <pubDate>Sat, 07 Jan 2023 14:38:47 GMT</pubDate>
            <atom:updated>2023-01-24T17:33:35.241Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*StR1O_kvctKLdMIp" /><figcaption>Photo by <a href="https://unsplash.com/@hostreviews?utm_source=medium&amp;utm_medium=referral">Stephen Phillips - Hostreviews.co.uk</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>This tutorial will show you how to use Celery, a task queue for Python, in combination with Flask and Redis to create a microservice for sending emails with the Gmail API. You’ll learn how to set up a Google Cloud project, create a service account, and configure Celery to use Redis as the message broker. By the end of this tutorial, you’ll have a working microservice that can send emails in the background, allowing you to improve the performance and scalability of your application.</p><p>This tutorial assumes that you have a good knowledge of programming with python and Flask and how to build it locally since that is beyond the scope of this tutorial.</p><p><strong>What we will build</strong></p><p>The app we will create is a simple microservice built using Flask and Celery that allows you to send emails using the Gmail API. When a certain event occurs in another microservice or app, a webhook sends a message to the /send-email route of the Flask app. The app then calls the send_email task and passes it the necessary parameters (recipient, subject, and body). The send_email task is then added to a message queue managed by Redis. A separate Celery worker process monitors this queue and retrieves tasks as they become available. When the worker process retrieves the send_email task, it executes it by using the Gmail API to send the email. The Flask app returns a response to the webhook provider indicating that the task has been queued successfully.</p><p><strong>Prerequisites</strong></p><p><strong>A Google Cloud project with the Gmail API enabled</strong></p><p>You’ll need to set up a Google Cloud project and enable the Gmail API to use the Gmail API to send emails. You can do this by going to the Google Cloud Console (https://console.cloud.google.com/) and following the prompts to create a new project and enable the API. Refer to the video below</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FahqO6hWPoz0&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DahqO6hWPoz0&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=google" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/d84ff2d9bb0a8a585ddf089da299c050/href">https://medium.com/media/d84ff2d9bb0a8a585ddf089da299c050/href</a></iframe><p><strong>A service account and private key file</strong></p><p>You’ll need to create a service account and download the private key file to authenticate your app to use the Gmail API. Please refer to the video above.</p><p><strong>Python 3 and pip</strong></p><p>You’ll need to have Python 3 and pip (the Python package manager) installed on your machine to run the app. You can check if you already have Python installed by running the python — version command in your terminal. If you don’t have Python installed, you can download it from the Python website (https://www.python.org/).</p><p><strong>Flask, Celery, Redis and Google python packages</strong></p><p>You’ll need to install these Python packages to use them in your app. You can install them by running the following commands:</p><pre>pip install flask<br>pip install celery<br>pip install Redis<br>pip install google-auth<br>pip install google-auth-oauth</pre><p><strong>A message broker</strong></p><p>You’ll need to set up a message broker (such as RabbitMQ or Redis) to manage the queue of tasks that Celery will process. In this case, you’ll be using Redis as the message broker. You’ll need to install and set up Redis on your machine to use it.</p><p>Once you have set up the prerequisites, let us start building.</p><p>First, we need to authenticate that app so that we may get the token.json file that we will to build the Gmail service instance. We are doing this because we will be testing our application on a desktop.</p><p><strong>Authentication Process.</strong></p><p>To authenticate a desktop app using OAuth 2.0, you’ll need to create an OAuth 2.0 client ID in the Google Cloud Console and configure it as a “Desktop app”. This will allow your app to run on the user’s desktop and access Google APIs. You’ll also need to download the client secrets file, which contains the client ID and client secret that you’ll need to authenticate your app. Once you have these prerequisites in place, you can use the google-auth and google-auth-OAuth lib Python packages to authenticate your app and obtain an access token. This process involves opening the authorization URL in the default web browser and prompting the user to enter the authorization code. Once you have the authorization code, you can exchange it for an access token using the fetch_token method of the InstalledAppFlow object. Finally, you can use the googleapiclient package to access the Google API and perform operations on behalf of the user.</p><ul><li>Save the below code in a file named token_gen.py - this code is responsible for the authentication.</li></ul><pre>rom __future__ import print_function<br>import os.path<br>from googleapiclient.discovery import build<br>from google_auth_oauthlib.flow import InstalledAppFlow<br>from google.auth.transport.requests import Request<br>from google.oauth2.credentials import Credentials<br> <br># If modifying these scopes, delete the file token.json.<br>SCOPES = [&#39;https://www.googleapis.com/auth/gmail.send&#39;]<br> <br>def main():<br>   &quot;&quot;&quot;Shows basic usage of the Gmail API.<br>   Lists the user&#39;s Gmail labels.<br>   &quot;&quot;&quot;<br>   creds = None<br>   # The file token.json stores the user&#39;s access and refresh tokens, and is<br>   # created automatically when the authorization flow completes for the first<br>   # time.<br>   if os.path.exists(&#39;token.json&#39;):<br>       creds = Credentials.from_authorized_user_file(&#39;token.json&#39;, SCOPES)<br>   # If there are no (valid) credentials available, let the user log in.<br>   if not creds or not creds.valid:<br>       if creds and creds.expired and creds.refresh_token:<br>           creds.refresh(Request())<br>       else:<br>           flow = InstalledAppFlow.from_client_secrets_file(<br>               &#39;credentials.json&#39;, SCOPES)<br>           creds = flow.run_local_server(port=0)<br>       # Save the credentials for the next run<br>       with open(&#39;token.json&#39;, &#39;w&#39;) as token:<br>           token.write(creds.to_json())<br> <br> <br>if __name__ == &#39;__main__&#39;:<br>   main()</pre><ul><li>Open your terminal in the same path as token_gen.py and run python token_gen.py, I recommend you save this file inside the root directory of your project, this process will launch the default browser, log in and return to the app. Refer to the image below to see how to achieve this.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*H3EUVJ4-OpuhyGq_Bz-ldw.png" /></figure><p>We have done the authentication, now we need to set up the cereals services and the app&#39;s routes i.e. “ /send-email”.</p><p>First, we will have to create the send_email functions and link it to celery, to do that follow the steps below:</p><ul><li>First, make sure your project struct is like this:</li></ul><pre>└── app<br>    ├── services<br>    │   └─ __init__.py<br>    └── templates<br>    └─ __init__.py<br>└──token_gen.py<br>└──credentials.json<br>└──toke.json<br></pre><ul><li>open app/services/__init__.py and paste the below code, this code is to initialize celery:</li></ul><pre>import celery<br>celery_service = celery.Celery(&#39;mailing tasks&#39;, broker=&#39;redis://localhost:6379&#39;, backend=&#39;redis://localhost:6379&#39;)</pre><ul><li>Define the send_email function. This function should take in the necessary parameters (e.g., recipient, subject, body) and use the googleapiclient library to send an email using the Gmail API.</li><li>Create a file named email_service.py inside the services package, and copy and paste the below code to the file;</li></ul><pre>from datetime import datetime<br>import os<br>from datetime import datetime<br>from . import celery_service<br>import base64<br>from googleapiclient.discovery import build<br>from google_auth_oauthlib.flow import InstalledAppFlow<br>from google.auth.transport.requests import Request<br>from google.oauth2.credentials import Credentials<br>import base64<br>from email.mime.text import MIMEText<br>import httplib2shim<br> <br> <br>@celery_service.task<br>def send_html_email(body, email, subject, sender):<br>   # app = create_celery_app()<br>   # with app.app_context():<br>      <br>       httplib2shim.patch()<br>       creds = None<br>       # The file token.json stores the user&#39;s access and refresh tokens, and is<br>       # created automatically when the authorization flow completes for the first<br>       # time.<br>       if os.path.exists(&#39;token.json&#39;):<br>           creds = Credentials.from_authorized_user_file(&#39;token.json&#39;, SCOPES)<br>       # If there are no (valid) credentials available, let the user log in.<br>       if not creds or not creds.valid:<br>           if creds and creds.expired and creds.refresh_token:<br>               creds.refresh(Request())<br>           else:<br>               flow = InstalledAppFlow.from_client_secrets_file(<br>                   &#39;credentials.json&#39;, SCOPES)<br>               creds = flow.run_local_server(port=0)<br>           # Save the credentials for the next run<br>           with open(&#39;token.json&#39;, &#39;w&#39;) as token:<br>               token.write(creds.to_json())<br>       # http_auth = creds.authorize(httplib2shim.Http())<br>       service = build(&#39;gmail&#39;, &#39;v1&#39;, credentials=creds)<br> <br>       sender = sender<br>       to = customer_email<br>       message_Text = message_text<br>       msg = create_message(sender,to,subject,message_Text)<br>       send_message(service,&quot;me&quot;,msg)<br> <br>def send_message(service, user_id, message):<br>   &quot;&quot;&quot;Send an email message.<br>   Args:<br>   service: Authorized Gmail API service instance.<br>   user_id: User&#39;s email address. The special value &quot;me&quot;<br>   can be used to indicate the authenticated user.<br>   message: Message to be sent.<br>   Returns:<br>   Sent Message.<br>   &quot;&quot;&quot;<br>   try:<br>       message = (service.users().messages().send(userId=user_id, body=message)<br>               .execute())<br>       print (&#39;Message Id: %s&#39; % message[&#39;id&#39;])<br>       return message<br>   except Exception as error:<br>       print (&#39;An error occurred: %s&#39; % error)<br> <br> <br>def create_message(sender, to, subject, message_text):<br> &quot;&quot;&quot;Create a message for an email.<br> Args:<br>   sender: Email address of the sender.<br>   to: Email address of the receiver.<br>   subject: The subject of the email message.<br>   message_text: The text of the email message.<br> Returns:<br>   An object containing a base64url encoded email object.<br> &quot;&quot;&quot;<br> message = MIMEText(message_text,&quot;html&quot;)<br> message[&#39;to&#39;] = to<br> message[&#39;from&#39;] = sender<br> message[&#39;subject&#39;] = subject<br> b64_bytes = base64.urlsafe_b64encode(message.as_bytes())<br> b64_string = b64_bytes.decode()<br> return {&#39;raw&#39;: b64_string}</pre><p>To understand the code above and also to get more details of what is going on here kindly refer to this Gmail API documentation <a href="https://developers.google.com/gmail/api/quickstart/python">https://developers.google.com/gmail/api/quickstart/python</a></p><p>Below is how you should arrange your files:</p><pre>├── app<br>│   ├── __init__.py<br>│   └── services<br>│       ├── __init__.py<br>│       └── email_service.py<br>├── credentials.json<br>├── token.json<br>└── token_gen.py</pre><p>After this next is to create a flask server and add a send-email route that will accept the webhooks payload that contains email data.</p><p>Open app/_init__.py and add the following code:</p><pre>from app.services.email_service import send_html_email<br>from flask import Flask, jsonify, request, render_template<br> <br>app = Flask(__name__)<br> <br>@app.route(&quot;/send-email&quot;, methods=[&quot;POST&quot;])<br>def send_email():<br>   subject = request.json.get(&quot;subject&quot;)<br>   body = request.json.get(&quot;body&quot;)<br>   address = request.json.get(&quot;address&quot;)<br>   msg_email = render_template(&quot;registration.html&quot;)<br>   send_html_email.delay(msg_email,address, subject,&quot;orenjagidraf@gmail.com&quot;)</pre><p><strong>Let me explain what the code above does:</strong></p><ul><li>This is a Flask app that defines a route called /send-email that can be used to send an email using the send_html_email function from the app.services.email_service module.</li><li>The send_email route is a POST endpoint that expects a JSON payload with the following fields:</li></ul><pre>{<br>&quot;subject&quot;: &quot;the subject of the email&quot;<br>&quot;body&quot;: &quot;the body of the email&quot;<br>&quot;address&quot;: &quot;the email address of the recipient&quot;<br>}</pre><ul><li>When a request is made to the /send-email route, the app extracts the subject, body, and address from the request payload using the request.json.get() method. It then renders an HTML email template using the render_template function and passes it to the send_html_email function along with the address, subject, and sender’s email address. <a href="https://flask.palletsprojects.com/en/2.2.x/tutorial/templates/#:~:text=Templates%20are%20files%20that%20contain,display%20in%20the%20user&#39;s%20browser.">Refer here for me details about flask templates</a></li><li>The send_html_email function is decorated with the @celery.task decorator, so it will be executed asynchronously by the Celery worker.</li><li>Finally, the app returns a JSON response with a msg field set to “success”. This indicates that the email has been sent successfully and the task has been added to the Celery queue.</li><li>Create another file on the root folder of the project name run.py. The files tree should be like this:</li></ul><pre>├── app<br>│   ├── __init__.py<br>│   ├── services<br>│   │   ├── __init__.py<br>│   │   └── email_service.py<br>│   └── templates<br>│       └── registration.html<br>├── credentials.json<br>├── run.py<br>├── token.json<br>└── token_gen.py</pre><ul><li>Paste the following code to the file:</li></ul><pre>from app import app<br> <br>if __name__ == &quot;__main__&quot;:<br>   app.run(port=5000, host=&quot;0.0.0.0&quot;, debug=True)</pre><p>Once everything is done, we need to spin up our development server and celery workers to send the email. Open your terminal and cd to where you have saved this project, run the following command.</p><pre>Python run.py,</pre><p>If everything is fine you will see logs like the image below on your terminal</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*okVWifpN0JstZnT2" /></figure><p>Open another window on your terminal and cd to where you have saved this project, run the following command</p><pre>celery -A app.services.email_service worker -l info --pool gevent<br><br># This is spin up clery worker but make sure redis server is running first else celery worker will fail.</pre><p>If everything is well you should see the logs like the images below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7C5w8uSC5yanX-eR" /></figure><p>Open postman and send a post request to localhost:5000/send-email, the body should be JSON and should have the following parameters:</p><pre>{<br>&quot;subject&quot;: &quot;the subject of the email&quot;<br>&quot;body&quot;: &quot;the body of the email&quot;<br>&quot;address&quot;: &quot;the email address of the recipient&quot;<br>}</pre><p><strong>Conclusion</strong></p><p>In this article, we covered how to create a simple microservice using Celery, Python-Flask, and Redis to send emails with the Gmail API. We started by setting up the development environment and authenticating the app with the Gmail API. We then implemented a Flask app and defined a Celery task to send an email asynchronously. Finally, we ran the Celery worker to execute the task and tested the microservice using a sample email.</p><p>By following the steps outlined in this article, you should now have a working microservice that can send emails using the Gmail API. You can use this microservice as a foundation for building more complex applications, or you can customize it to meet your specific needs. Remember to consider the benefits of using a microservice architecture, such as the ability to scale and update individual components independently.</p><p>I hope you found this article helpful, and that you feel confident building your own microservice with Celery, Flask, and the Gmail API.</p><p>You can clone this project <a href="https://github.com/Gidraf/Email_Service">here</a></p><p>Thank you for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=224cc74ac7b3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-andela-way/create-a-simple-microservice-with-celery-python-flask-redis-to-send-emails-with-gmail-api-224cc74ac7b3">How to create a Microservice to send Email with Celery, Python, Flask &amp; Redis using Gmail API.</a> was originally published in <a href="https://medium.com/the-andela-way">The Andela Way</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build a simple text-to-speech web app with Python, Flask, and Termux.]]></title>
            <link>https://medium.com/the-andela-way/build-a-simple-text-to-speech-web-app-with-python-flask-and-termux-a51701f1fea0?source=rss-175ac4d02a00------2</link>
            <guid isPermaLink="false">https://medium.com/p/a51701f1fea0</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[flask]]></category>
            <category><![CDATA[web-app-development]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Gidraf Orenja]]></dc:creator>
            <pubDate>Sat, 25 Jan 2020 16:03:37 GMT</pubDate>
            <atom:updated>2020-02-17T10:16:14.946Z</atom:updated>
            <content:encoded><![CDATA[<h3>Build a simple web app with Python, Flask, and Termux.</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gJPv8J0L2KWJuWhy" /><figcaption>Photo by <a href="https://unsplash.com/@__matthoffman__?utm_source=medium&amp;utm_medium=referral">Matt Hoffman</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FwQtpPkNrWPc%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DwQtpPkNrWPc&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FwQtpPkNrWPc%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/438df3cd1ebb63ea5a64955f9d0e4250/href">https://medium.com/media/438df3cd1ebb63ea5a64955f9d0e4250/href</a></iframe><p>In my <a href="https://medium.com/the-andela-way/create-the-simplest-django-and-react-server-on-your-phone-fd53ea30c5e8">first article</a>, I wrote about how we can create the simplest structure of the web app server on our android devices. In this article, I want to take you even further by building a web-based app that shall run on your android phone. The app will be a simple Flask server that accepts a text and converts that text to speech. The UI is a simple form with an input and a button, we will use this form to send the text to the server.</p><p>This article assumes you have some basic knowledge of Termux and Python because I won’t cover that part in this article.</p><ul><li>First Make sure Termux and Python are installed on your phone. If you have no idea how to get it done, kindly check my <a href="https://medium.com/the-andela-way/create-the-simplest-django-and-react-server-on-your-phone-fd53ea30c5e8">previous tutorial.</a></li><li>Open Termux and run pkg install vim on your Termux terminal to install vim. We will use it as the text editor to edit our python file.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KbkqR1_j36ErTKy5S8ilyA.png" /><figcaption>installation of vim</figcaption></figure><ul><li>After vim has finished installing run pkg install termux-api to install the Termux API package. We will use one of this package API in our project, the package is called <a href="https://wiki.termux.com/wiki/Termux-tts-speak">termux-tts-speak</a> read more about it <a href="https://wiki.termux.com/wiki/Termux-tts-speak">here</a>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bH37UHDD9a3nwl4U9uReLg.png" /><figcaption>installation of termux-API</figcaption></figure><ul><li>Create a virtual environment by running python3 -m venv text-to-speech</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f-dB3Zp_JyjWjAGGSYj24g.png" /><figcaption>the creation of a virtual environment</figcaption></figure><ul><li>Run source text-to-speech/bin/activate to activate the virtual environment we just created.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JtGn9-CWqILZXTrh-8KzbQ.png" /><figcaption>Activation of the virtual environment</figcaption></figure><ul><li>Run pip install flask to install/add Flask virtual environment.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xLACMhCf7eSDrCxKvYl5hw.png" /><figcaption>Installation of flask</figcaption></figure><ul><li>After Flask has finished installing, run touch run.py to create a file called run.py. You canls and notice there is a new file called run.py created.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-gBCjQbhA1TA9pqePL3zFg.png" /><figcaption>Creating a run.py file.</figcaption></figure><blockquote><strong><em>You can just clone the repo using git. Git is installed the same way as vim, just run pkg install git.</em></strong></blockquote><blockquote><strong><em>After git has installed clone the repo by running git clone </em></strong><a href="https://github.com/Gidraf/Text-to-speech.git"><strong><em>https://github.com/Gidraf/Text-to-speech.git</em></strong></a></blockquote><ul><li>Run vim run.py to open the file we have just created. Here is where we will write all the code to convert text to speech using an android tts engine with the help of the Termux API.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1PMi89mcdsfgHeRDnhkMSQ.png" /><figcaption>vim interface after running <strong>vim run.py</strong></figcaption></figure><ul><li>Tap letter i on your keyboard to change the mode of the vim editor to edit/insert and copy and paste the code below to your vim editor.</li></ul><pre>import subprocess<br>from flask import Flask,request, render_template</pre><pre>app = Flask(__name__)</pre><pre>@app.route(&#39;/&#39;,methods=[&quot;GET&quot;,&quot;POST&quot;])<br>def playsound():<br>    if request.method == &#39;GET&#39;:<br>        return &#39;&#39;&#39;&lt;!DOCTYPE html&gt;                                       &lt;html&gt;                                                        &lt;link rel=&quot;stylesheet&quot; href=&quot;static/style.css&quot;&gt;                                                     &lt;body&gt;                                                                                                                                                            &lt;form action=&quot;/&quot; method=&quot;POST&quot; style=&quot;border:1px<br>solid #ccc&quot;&gt;                                            &lt;div class=&quot;container&quot;&gt;                                 &lt;h1&gt;Play sound&lt;/h1&gt;                                        &lt;hr&gt;                                                                                                        &lt;input id=&quot;text&quot; type=&quot;text&quot; placeholder=&quot;Text to play&quot; name=&quot;text&quot; required&gt;                                                                                     &lt;button type=&quot;submit&quot; class=&quot;signupbtn&quot;&gt;Play&lt;/button&gt;                                                       &lt;/div&gt;                                              &lt;/div&gt;                                              &lt;/form&gt;                                               <br>&lt;/body&gt;&#39;&#39;&#39;<br>    text = request.values.get(&quot;text&quot;)<br>    MyOut = subprocess.call(f&#39;&#39;&#39;termux-tts-speak {text}&#39;&#39;&#39;, shell=True)<br>    return &#39;&#39;&#39;&lt;!DOCTYPE html&gt;                                       &lt;html&gt;                                                        &lt;link rel=&quot;stylesheet&quot; href=&quot;static/style.css&quot;&gt;                                                     &lt;body&gt;                                                                                                                                                            &lt;form action=&quot;/&quot; method=&quot;POST&quot; style=&quot;border:1px<br>solid #ccc&quot;&gt;                                            &lt;div class=&quot;container&quot;&gt;                                 &lt;h1&gt;Play sound&lt;/h1&gt;                                        &lt;hr&gt;                                                                                                        &lt;input id=&quot;text&quot; type=&quot;text&quot; placeholder=&quot;Text to play&quot; name=&quot;text&quot; required&gt;                                                                                     &lt;button type=&quot;submit&quot; class=&quot;signupbtn&quot;&gt;Play&lt;/button&gt;                                                       &lt;/div&gt;                                              &lt;/div&gt;                                              &lt;/form&gt;                                               <br>&lt;/body&gt;&#39;&#39;&#39;</pre><pre>if __name__ ==&#39;__main__&#39;:<br>    app.run(debug=True)</pre><blockquote><em>Let me explain the code above:-</em></blockquote><blockquote><em>First, we are importing the modules which are Flask and subprocess. The Flask package is to build the server to communicate with the browser, while the subprocess is used to run the terminal command on the Termux terminal. We are also initializing Flask class and route to home </em><em>‘/’. The route accepts two methods which are </em><em>GET and </em><em>POST it also has got a function that checks if the request method is </em><em>GET or not if the method is GET, we are returning the HTML string while if it’s not we are processing the text to speech logic and returning the same HTML string. This is a simple implementation of Flask server app. If you are new to Flask I would advise you to read more about it </em><a href="http://flask.palletsprojects.com/en/1.1.x/"><em>here</em></a><em>.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m_xUUjdZGn48O6uYnVVt3w.png" /><figcaption>Paste the code above the vim text editor</figcaption></figure><ul><li>Once done tap ESC button on top of your keyboard</li><li>Run python run.py to start the flask server.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M5f57ZAY-WFXBQeKVjoUpw.png" /></figure><ul><li>Now navigate to your browser on URL localhost:5000 and notice the edit text field and a button type anything and press play button. Wait for the server to respond it may take a while since android tts engine needs to be started first</li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FwQtpPkNrWPc%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DwQtpPkNrWPc&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FwQtpPkNrWPc%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/438df3cd1ebb63ea5a64955f9d0e4250/href">https://medium.com/media/438df3cd1ebb63ea5a64955f9d0e4250/href</a></iframe><p>Congratulation you have just build a functional web app on your phone. If you would like to receive more of this kindly follow me or subscribe to my youtube channel. Thanks for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a51701f1fea0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-andela-way/build-a-simple-text-to-speech-web-app-with-python-flask-and-termux-a51701f1fea0">Build a simple text-to-speech web app with Python, Flask, and Termux.</a> was originally published in <a href="https://medium.com/the-andela-way">The Andela Way</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create the simplest Django and React server on your phone]]></title>
            <link>https://medium.com/the-andela-way/create-the-simplest-django-and-react-server-on-your-phone-fd53ea30c5e8?source=rss-175ac4d02a00------2</link>
            <guid isPermaLink="false">https://medium.com/p/fd53ea30c5e8</guid>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[the-andela-way]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Gidraf Orenja]]></dc:creator>
            <pubDate>Sun, 19 May 2019 12:44:11 GMT</pubDate>
            <atom:updated>2019-05-19T12:44:11.063Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*MNwf-R8sCwd1DEA7" /><figcaption>Photo by <a href="https://unsplash.com/@yoal_des?utm_source=medium&amp;utm_medium=referral">Yoal Desurmont</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>We will be creating the simplest app we can build with Django and ReactJs frameworks. This tutorial aims to give aspiring coders who don’t have access to a computer starting point if they want to code on their phone. Steps and procedure explained here should be practised on Android phone.</p><p>1.<strong> What is Termux and Termux API?<br></strong><em>Termux is an Android terminal emulator and Linux environment app that works directly with no rooting or setup required. Termux API work with Termux to provides an interface to access Android and Chrome hardware features. We will be using termux, to run the terminal commands on the device. learn more on how to use termux </em><a href="https://wiki.termux.com/wiki/Main_Page"><em>here</em></a></p><p><strong>2. Installation of Termux and Termux API</strong>.<br>We will need to install both Termux and Termux API they can both be downloaded on <a href="https://play.google.com/store/apps/details?id=com.termux">google play</a></p><blockquote><strong>3</strong>.<strong> Installation of Python<br></strong>In order to install and run the Django server, we will need to have Python programming language installed first on our device. Read more about python <a href="https://docs.python.org/3/tutorial/index.html">here</a>.</blockquote><p>When you have finished installing the Termux and Termux API application,</p><ul><li>open Termux.</li><li>Run pkg install python to install python.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/322/1*sWLasehRrqwYoAwezyT0ag.png" /></figure><ul><li>When python has finished installing you can run python on Termux to confirm the version of python installed.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/636/0*ID7QLQYfmuA4drWh" /></figure><p><em>Congratulations!! you have just installed python on your phone.</em></p><p><strong>Creating a virtual environment<br></strong><em>We will use the virtual environment to manage our project packages. If you are not familiar with the virtual environment in python, this great </em><a href="https://realpython.com/python-virtual-environments-a-primer/"><em>tutorial</em></a><em> is for you.</em></p><ul><li>Exit the python CLI by running exit() on your Termux terminal.</li><li>Run python -m venv my_venvon your Termux terminal to create a virtual environment named venv that we will be using for this tutorial.</li><li>Run ls on your termux terminal and notice that a new folder has been created named my_venv — that folder will hold our environment packages i.e Django.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/0*BR1ququyezA_e0vM" /></figure><ul><li>Run source my_env/bin/activate to activate the virtual environment.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/634/0*flf09DAQf_YQxqrr" /></figure><p><strong>Installing Django<br></strong>Now let us install the Django framework</p><ul><li>Install Django by running pip install django on termux terminal.<br>When Django has finished installation the below message will appear.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/634/0*uJw5HZ3ZPVz5Dair" /></figure><blockquote><strong>Run Django Server<br></strong>It’s time to run our Django application and test it but in order to run the Django server, we need to start a Django project. I will recommend you read more about Django and how to make a more comprehensive application with it <a href="https://docs.djangoproject.com/en/2.2/">here</a> if you are unfamiliar with it.</blockquote><ul><li>Run django-admin startproject myapp to create a Django project called myapp.</li><li>Run lsand notice there is an additional folder created named myapp.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/0*t66dPCEW0YGt7_JQ" /></figure><ul><li>Run cd myappto switch to our project folder.</li><li>Run lsand notice there is a file named manage.py we will be using it to run the server.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/0*qLdnTMrOIVubmPrg" /></figure><ul><li>Run python manage.py runserverto run the Django server. The following screen should appear.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/636/0*t_o0my16_K7MoiRZ" /></figure><blockquote>Congratulations!! you have just created mobile Django app.<br>Open your mobile browser and use <em>localhost:8000</em> as the url, you should see content similar to the image below.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/648/0*8rrDfxVUOuJG_aEk" /></figure><p><strong>4. Installation of Nodejs and React.<br></strong>The Nodejs and React installation procedure isn’t that much different from python and Django installation, but we will need a different session to create the react server app.<em> I assume by now you are already familiar with termux so I won’t be posting a lot of screenshots please observe it from your terminal.</em></p><ul><li>Slide the screen from left to right to open Termux navigation drawer.</li><li>Select new session option a new window of Termux session will be opened.</li></ul><blockquote>We will create the ReactJs server in this session. Just like Django and python in order to run ReactJs server we need nodeJs to be installed first. If you are not familiar with NodeJs please read more about it <a href="https://nodejs.org/en/docs/">here</a></blockquote><ul><li>Run pkg install nodejsto install nodejs on your phone.</li><li>Run node -v to check if nodejs has been properly installed and also to know the version of nodejsinstalled.</li><li>Run npm install create-react-app after Nodejs has finished installing. We will use this library to create react app. You can learn more about Reactjs <a href="https://reactjs.org/docs/getting-started.html">here</a></li><li>Run mkdir reactapp after create-react-app has finished installing. This command will create a new folder project that we will use to build the react server app.</li><li>Run cd reactapp to switch to the newly created folder named reactapp</li><li>Run npm init react-app myapp this command will set up and install everything you need to run a react app on your phone.</li><li>Run cd myapp to switch to your react project</li><li>Run npm start to start the react server, wait for the server to build and open your phone browser enter localhost:3000 on your URL address field.</li></ul><p><em>You will see a beautiful react log similar to the screenshot below rotating</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*uFrPqvNK7hMCtdxGXQ5CCA.png" /></figure><p>Congratulations you have just created a Reactjs app.</p><p><strong>5. Conclusion and Recommendations<br></strong>Termux is a cool terminal emulator, you can use <a href="https://git-scm.com/">git</a> and clone projects from <a href="https://github.com/">GitHub</a> and run them on your phone. Termux can also be used with Vim as the text editor. Now the power in your hand has been unlocked I want to see what you will build with your phone. 😀 😀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fd53ea30c5e8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-andela-way/create-the-simplest-django-and-react-server-on-your-phone-fd53ea30c5e8">Create the simplest Django and React server on your phone</a> was originally published in <a href="https://medium.com/the-andela-way">The Andela Way</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>