Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create and run Python applications in Android

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to create and run Python applications in Android". In daily operation, I believe many people have doubts about how to create and run Python applications in Android. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to create and run Python applications in Android". Next, please follow the editor to study!

Install Termux on Android

First, install the Termux application. Termux is a powerful terminal emulator that provides all the most popular Linux commands, plus hundreds of additional packages for easy installation. It doesn't require any special permissions, and you can install it using the default Google Play Store or the open source application repository F-Droid.

After installing Termux, start it and use Termux's pkg command to perform some necessary software installation.

Subscribe to additional repository root-repo:

$pkg install root-repo

Perform an update to keep all installed software up to date.

$pkg update

Finally, install Python:

$pkg install python

After the installation and automatic configuration are complete, you can build your application.

Build an Android application on Android

Now that you have installed a terminal, you can use your Android phone much like another Linux computer. This is a good demonstration of how powerful the terminal is.

First create a project directory:

$mkdir Source$ cd Source

Next, create a Python virtual environment. This is a common practice for Python developers, and it helps to make your Python project independent of your development system (in this case, your phone). In your virtual environment, you will be able to install Python modules specific to your application.

$python-m venv venv

Activate your new virtual environment (note that the first two dots are separated by spaces)

$. . / venv/bin/activate (env) $

Please note that your shell prompt now starts with (env), indicating that you are in a virtual environment.

Now use pip to install the Flask Python module.

(env) $pip install flask write Python code on Android

You're ready. Now you need to write code for your application.

To do this, you need to have experience in a classic text editor. I am using vi. If you are not familiar with vi, install and try vimtutor, which (as its name implies) will teach you how to use this editor. If you have other editors you like, such as jove, jed, joe or emacs, you can install and use one of them.

Now, because the demo is very simple, you can also use shell's heredoc function directly, which allows you to type text directly into the prompt.

(env) $cat > hello_world.py > from flask import Flask > app = Flask (_ _ name__) > > @ app.route ('/') > def hello_world (): > return 'Hello, Worldwide' > EOF (env) $

There are only six lines of code, but with it, you can import Flask, create an application, and route incoming traffic to a function called hello_world.

Now you have prepared the code for the web server. It's time to set 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 launching the app, you will see this message:

Serving Flask app... Running on http://127.0.0.1:5000/

This shows that you are now running a mini web server on localhost (that is, your device). The server is listening for requests from port 5000.

Open your mobile browser and go to http://localhost:5000 to view your web app.

You didn't compromise the security of your phone. You only run a local server, which means your phone does not accept requests from the outside world. Only you can access your Flask server.

To make your server visible to others, you can disable the debug mode of Flask by adding-host=0.0.0.0 to the run command. This will open the port on your phone, so use it with caution.

(env) $export FLASK_ENV= "" (env) $flask run-host=0.0.0.0

Press Ctrl+C to stop the server (use the special Termux key as the Ctrl key).

At this point, the study on "how to create and run Python applications in Android" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report