Learning and using Python is fun. Thanks to its growing popularity, there are a plethora of ways it can be used to make the world of computing better than what it is today.
Imagine building and running python applications, whether it's a command-line tool developed to fetch your favorite curated articles from the Internet, or starting a web server that runs right in the palm of your hand, all with just an Android mobile device and open source tools. This would change how you view your mobile device entirely, changing it from a device that merely lets you consume content to a device that helps you be creative.
In this article, I'll demonstrate all of the tools, software packages, steps, and all the bells and whistles required to build, run, and test a simple Python application on any Android mobile device. I use the Flask framework to create a simple “Hello, World!” app running on a simple but powerful web server. And best of all, it all happens on the phone. No laptop or desktop required.
Install Termux on Android
First, install the Termux application. Termux is a powerful terminal emulator that offers all the most popular Linux commands, plus hundreds of additional packages for easy installation. It doesn't require any special permissions You can use either the default Google Play store or the open source app repository F-Droid to install.
Once you have installed Termux, launch it and perform a few requisite software installations using Termux's pkg command:
Subscribe to the additional repository “root-repo”:
$ pkg install root-repo
Perform an update to bring all the installed software up to date:
$ pkg update
Finally, install Python:
$ pkg install python
Once the installation and auto set-up of configuration is complete, it’s time to build your application.
Build an app for Android on Android
Now that you have a terminal installed, you can work on your Android phone largely as if it were just another Linux computer. This is a great demonstration of just how powerful a terminal really is.
Start by creating a project directory:
$ mkdir Source $ cd Source
Next, create a Python virtual environment. This is a common practice among Python developers, and it helps keep your Python project independent of your development system (in this case, your phone). Within your virtual environment, you'll be able to install Python modules specific to your app.
$ python -m venv venv
Activate your new virtual environment (note that the two dots at the start are separated by a space):
$ . ./venv/bin/activate (env)$
Notice that your shell prompt is now preceded by (env) to indicate that you're in a virtual environment.
Now install the Flask Python module using pip:
(env) $ pip install flask
Write Python code on Android
You're all set up. All you need now is to write the code for your app.
To do this, you should have experience with a classic text editor. I use vi. If you’re unfamiliar with vi, install and try the vimtutor application, which (as its name suggests) can teach you how to use this editor. If you have a different editor you prefer, such as jove, jed, joe, or emacs, you can install and use one of those instead.
For now, because this demonstration app is so simple, you can also just use the shell's heredoc function, which allows you to enter text directly at your prompt:
(env)$ cat << EOF >> hello_world.py > from flask import Flask > app = Flask(__name__) > > @app.route('/') > def hello_world(): > return 'Hello, World!' > EOF (env)$
That's just six lines of code, but with that you import Flask, create an app, and route incoming traffic to the function called hello_world.
Now you have the web-server code ready. It's time to set up some environment variables and start a web server on your phone.
(env) $ export FLASK_APP=hello_world.py (env) $ export FLASK_ENV=development (evn) $ python hello_world.py
After starting your app, you see this message:
serving Flask app… running on http://127.0.0.1:5000/
This indicates that you now have a tiny web server running on localhost (that is, your device). This server is listening for requests looking for port 5000.
Open your mobile browser and navigate to http://localhost:5000 to see your web app.
You haven't compromised your phone's security. You're only running a local server, meaning that your phone isn't accepting requests from the outside world. Only you can access your Flask server.
To make your server visible to others, you can disable Flask's debugging mode by adding --host=0.0.0.0 to the run command. This does open ports on your phone, so use this wisely.
(env) $ export FLASK_ENV=”” (env) $ flask run –host=0.0.0.0
Stop the server by pressing Ctrl+C (use the special Termux key for Control).
Decide what comes next
Your phone is probably not the ideal server platform for a serious web app, but this demonstrates that the possibilities are endless. You might program on your Android phone just because it’s a convenient way to stay in practice, or because you have an exciting new idea for localized web apps, or maybe you just happen to use a Flask app for your own daily tasks. As Einstein once said “Imagination is more important than knowledge”, and this is a fun little project for any new coder, or a seasoned Linux or Android enthusiast. It can be expanded to endless levels, so let your curiosity take over, and make something exciting!