Tuesday, August 15, 2017

How to install and run a python flask web app on android mobile phone

We are going to test run python Flask on mobile phone as nowadays there are mobile phones available with more CPU and RAM than it used to be generally available on laptops.

I know most of the time people need their phone to do the stuff for these phones are made. But, what about when you are working and your android with gigabytes of RAM and multi-core CPU is just sleeping and waiting for incoming text/call? what about using it as portable webserver for development environment?

So I'm going to demonstrate how to install and run python Flask micro framework on mobile phone.

Prerequisite:
  • Terminal access( no need of root access): I'm using Termux
  • Python 2 or 3
  • pip 
  • Flask
Let's begin with installing Termux app from Google play store. Once app is installed open it  up update packages repository and install python.

apt-get install python





It's always to recommended to install virtual environment for the experiments and keeping application specific package versions separate.So let's install virtualenv package and create and activate a virtual environment called flask(you can give any name you like). Once the virtual environment activated. it will change the terminal prompt and add activated environment name, in my case as the environment name is 'flask' it has changed prompt to (flask)$.

Now as the virtual environment is activated, we can install required python packages and these packages will be restricted to 'flask' virtual environment. In our case we just need Flask package so will install only 'flask' using pip utility.

pip install virtualenv
virtualenv flask
source .flask/bin/activate
pip install Flask





cat app.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
  return "My mobile web server!"

if __name__ == "__main__":
  app.run(host='0.0.0.0')

Next will write a quick test Flask application and run to test if it's working. below command will start Flask inbuilt webserver on port 5000.

python app.py

Test it on you mobile web browser using : http://localhost:5000




To access it on laptop we have to connect laptop to mobile using mobile hotspot and using mobile ip. In my case the IP address is : 192.168.43.1

To check mobile IP address, activate mobile hot-spot on android and check IP access using ifconfig command.

ifconfig wlan0



What next? install git and pull your your python Flask app code and run it on mobile.👀



9 comments:

  1. Awesome post dude...got everything working in minutes... only snag was.... running this command...

    termux-setup-storage

    to get access to the storage location..

    as per this post...

    https://android.stackexchange.com/questions/166538/where-is-the-folder-that-termux-defaults-to

    ReplyDelete
  2. I got this when im trying to open cat app.py

    'app.py': No such file or directory

    ReplyDelete
  3. I got this when im trying to open cat app.py

    'app.py': No such file or directory

    ReplyDelete
  4. Offshore Development Team in Poland is a full stack solution for professionals. They provide their services in mobile app development and UI/UX designing. After development they provide the facility of maintenance and app marketing.

    ReplyDelete

How to install and run a python flask web app on android mobile phone

We are going to test run python Flask on mobile phone as nowadays there are mobile phones available with more CPU and RAM than it used to b...