A Quick Way to Build Applications in Python | by Naser Tamimi | Towards Data Science

Building Python applications that have graphical user interfaces and are doing sophisticated tasks might look difficult. In a recently published article (see the link below), I mentioned how only 7 Python libraries are needed to start building applications.
towardsdatascience.com
This article will show you how to build a simple translation application in a few lines. I only use two Python libraries: requests and ipywidgets.
I chose to write a translator application as an example to show you how it is easy to build applications in Python. This application gets an English text and shows its Spanish translation. Very easy and straightforward.
To do so, we need two parts inside the application. The first part is a backend that does the translation, and the second part is a frontend or a graphical user interface (GUI) that interacts with the user (taking the input text and showing the output text).
For the backend, I use Azure Translation API. If you are not familiar with the concept of API (Application Programming Interface), I try to explain it in an effortless way. An API is a way (i.e., protocol) to ask a service (for example, translation) from a service provider (usually a server). For example, you can use Google Maps API to get the driving distance between two locations. Imagine, if Google Maps API did not exist, then you should write your own code to calculate this driving distance based on roads databases. Simply, APIs prevent you from reinventing the wheel. Sometimes third party APIs are free, but most of the time, you must pay a little bit of cost (especially if you are using them frequently). For this article's purpose, we use Microsoft Azure Translator API, which is free for limited use. If you don’t have an Azure account, please read the next section; otherwise, you can skip it.
If you don’t have an Azure account, you can sign up for a free one. Follow the instructions in the following link. It is easy and gives you free access to tens of awesome APIs and services.
docs.microsoft.com
After creating a free Azure account, you need to add Azure Translator API to your account. Again, it is free and easy. You need to follow the steps in the following link.
docs.microsoft.com
If you do everything correctly, you must have an API key (i.e., Authentication key). The key is simply a string of characters that lets you work with the Azure Translator API. You need the key to put it in your Python program (the next section). REMEMBER, never share your API keys with anyone (including your users).
Now that you have your API key for Azure Translator (congratulations!), let’s see how we can talk to the API through Python.
One of the most popular Python libraries to work with APIs is “requests.” You may install it using
pip install requests
or
conda install -c anaconda requests
The following Python function gets a text and sends a request to Azure Translator API.
Before going further, I must tell you an important point (especially to beginners). The truth is:
Each API has a set of rules for requesting a service. Working with Google Maps API is different from Azure Translator API. To work with an API, you must read the documentation.
Let’s go back to our code ...
In the code, you must put your API key (see API_KEY) and specify your API region (see API_REGION). requests.post sends a POST request to Azure Translator API. To send a request, you need to pass an URL address, parameters, headers, and a body. The URL address is a web address that Azure servers are constantly monitoring for any upcoming requests. As soon as the server sees your request, first, it checks your credentials in the header. If your credentials are valid, it looks at your parameters to understand what you are requesting. For example, in this case, I am requesting version 3.0 of the translator for translating from English (i.e. en) to Spanish (i.e. es). If the parameters are valid, then the API server takes the body (i.e., a JSON object in this example) and processes my requests. Finally, the requests.post send me a JSON object back that I named as response.
You can check the function with a simple example. Add the following lines at the end of the code and run it.
if __name__ == '__main__':    print(azure_translate('Hello world.'))
You must see an output like this.
[{'translations': [{'text': 'Hola mundo.', 'to': 'es'}]}]
This is a JSON (or Python dictionary) object in response to your request. If you look closely, you see the translation as well as some additional information such as the translated language (i.e., Spanish or es).
Again, everything that is said here is based on the documentation, and you must be able to work with any API after reading the documentation. If you don’t read it, you never know how to communicate with an API and what parameters with what data structure should be passed.
Now that you have your backend ready, it is time to build your frontend. As I mentioned previously (link), ipywidgets and Jupyter Notebooks are good combinations to build simple user interfaces for browsers.
For my simple application, I only need a few elements in my user interface. Those elements are:
A header to show the name of the application (a cosmetic feature).A text area widgets to input an English text.A button to request the translation after clicking.An empty area to print the Spanish translation.
A header to show the name of the application (a cosmetic feature).
A text area widgets to input an English text.
A button to request the translation after clicking.
An empty area to print the Spanish translation.
Ipywidgets has a set of widgets to create different sorts of user interfaces and dashboards with them. For this application, I’ll use an HTML widget for the header, a Textarea widget for input text, a Button widget for requesting the translation, and an Output widget for printing out the results. Then I put them vertically aligned in a container using a VBox widget.
The only trick is how to request a translation service when the user clicks on the translation button. It is simple to define this action upon the clicking event. To do so, I define a function and pass it to on_click method of the button. When a user clicks on the translation button, the specified function will run and send a request to Azure Translator API. Simple, right?
The following codes show the front end part of the application.
Now, it is time to put everything together in a Jupyter Notebook file. Put both parts in a Jupyter Notebook file and save it (e.g., Translator_API.ipynb).
Since Ipywidgets work very well with Chrome, check if your default browser is Chrome. After setting your default browser to Chrome, open a terminal and run your Jupyter Notebook code via Voila.
voila Translator_API.ipynb
After a few seconds, you must see your application automatically opened in the browser. Give it a test like what I did here.
Voila!!!!! you made your simple Translator application in a few steps. You can improve your GUI look by using ipyvuetify, a library full of modern-looking widgets. You can also share your MVP (minimum viable product) with other people using ngrok (read the following article for details).
towardsdatascience.com
Building applications in Python is easy and fast. You can use APIs to avoid reinventing the wheel and build your backend. You can also use simple libraries like ipywidgets or ipyvuetify to build a simple GUI for your application. In this article, I showed you how to build a simple translator application with a few lines of code. The same approach can be applied to more sophisticated MVP applications.
Follow me on Medium and Twitter for the latest stories.