mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-22 23:00:57 +08:00
translated
This commit is contained in:
parent
fbf76989ae
commit
0944d6f751
@ -1,179 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Create and run Python apps on your Android phone)
|
||||
[#]: via: (https://opensource.com/article/20/8/python-android-mobile)
|
||||
[#]: author: (Phani Adabala https://opensource.com/users/adabala)
|
||||
|
||||
Create and run Python apps on your Android phone
|
||||
======
|
||||
Use Termux and Flask to create, develop, and run a web app on your
|
||||
mobile device.
|
||||
![Tux and Android stuffed animals on shelf][1]
|
||||
|
||||
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][2] 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][3]. 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][4] store or the open source app repository [F-Droid][5] to install.
|
||||
|
||||
![Welcome to Termux][6]
|
||||
|
||||
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`
|
||||
```
|
||||
|
||||
![Install Python][7]
|
||||
|
||||
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**.
|
||||
|
||||
![Vim on Android][8]
|
||||
|
||||
Now you have the web-server code ready. It's time to set up some [environment variables][9] 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
|
||||
```
|
||||
|
||||
![Running a Flask app on your phone][10]
|
||||
|
||||
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.
|
||||
|
||||
![Your web app][11]
|
||||
|
||||
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!
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/20/8/python-android-mobile
|
||||
|
||||
作者:[Phani Adabala][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/adabala
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/tux_penguin_linux_android.jpg?itok=ctgANLI7 (Tux and Android stuffed animals on shelf)
|
||||
[2]: https://opensource.com/article/18/4/flask
|
||||
[3]: https://opensource.com/article/20/8/termux
|
||||
[4]: https://play.google.com/store/apps/details?id=com.termux
|
||||
[5]: https://f-droid.org/repository/browse/?fdid=com.termux
|
||||
[6]: https://opensource.com/sites/default/files/termux-flask-1_0.webp (Welcome to Termux)
|
||||
[7]: https://opensource.com/sites/default/files/termux-install-python.webp (Install Python)
|
||||
[8]: https://opensource.com/sites/default/files/termux-python-vim.webp (Vim on Android)
|
||||
[9]: https://opensource.com/article/19/8/what-are-environment-variables
|
||||
[10]: https://opensource.com/sites/default/files/termux-flask-run.webp (Running a Flask app on your phone)
|
||||
[11]: https://opensource.com/sites/default/files/flask-app-android.webp (Your web app)
|
@ -0,0 +1,178 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Create and run Python apps on your Android phone)
|
||||
[#]: via: (https://opensource.com/article/20/8/python-android-mobile)
|
||||
[#]: author: (Phani Adabala https://opensource.com/users/adabala)
|
||||
|
||||
在你的 Android 手机上创建和运行 Python 应用
|
||||
======
|
||||
使用 Termux 和 Flask 在你的移动设备上创建、开发和运行一个网络应用。
|
||||
![Tux and Android stuffed animals on shelf][1]
|
||||
|
||||
学习和使用 Python 是很有趣的。由于它越来越受欢迎,有大量的方式可以让计算世界比现在更好。
|
||||
|
||||
想象一下,构建和运行 python 应用,无论是开发一个命令行工具,从互联网上获取你最喜欢的文章,还是启动一个直接在掌上运行的网络服务器,所有这些都只需要一个 Android 移动设备和开源工具。这将完全改变你对移动设备的看法,将它从一个仅仅让你消费内容的设备变成一个帮助你发挥创造力的设备。
|
||||
|
||||
在本文中,我将演示运行和测试一个简单的 Python 应用所需的所有的工具、软件包、步骤等等。我使用 [Flask 框架][2]来创建一个简单的 “Hello, World!” 应用,并在一个简单而强大的 Web 服务器上运行。最棒的是,这一切都发生在手机上。不需要笔记本或台式机。
|
||||
|
||||
### 在 Android 上安装 Termux
|
||||
|
||||
首先,[安装 Termux 应用程序][3]。Termux 是一个强大的终端仿真器,它提供了所有最流行的 Linux 命令,加上数百个额外的包,以便于安装。它不需要任何特殊的权限,你可以使用默认的 [Google Play][4] 商店或开源应用仓库 [F-Droid][5] 来安装。
|
||||
|
||||
![Welcome to Termux][6]
|
||||
|
||||
安装 Termux 后,启动它并使用 Termux 的 **pkg** 命令执行一些必要的软件安装。
|
||||
|
||||
订阅额外的仓库 “root-repo”:
|
||||
|
||||
|
||||
```
|
||||
`$ pkg install root-repo`
|
||||
```
|
||||
|
||||
执行更新,使所有安装的软件达到最新状态。
|
||||
|
||||
|
||||
```
|
||||
`$ pkg update`
|
||||
```
|
||||
|
||||
最后,安装 Python:
|
||||
|
||||
|
||||
```
|
||||
`$ pkg install python`
|
||||
```
|
||||
|
||||
![Install Python][7]
|
||||
|
||||
安装和自动配置完成后,就可以构建你的应用了。
|
||||
|
||||
### 在 Android 上构建一个 Android 应用
|
||||
|
||||
现在你已经安装了一个终端,你可以在很大程度上像使用另一台 Linux 电脑一样使用你的 Android 手机。这很好地展示了终端到底有多强大。
|
||||
|
||||
首先创建一个项目目录:
|
||||
|
||||
|
||||
```
|
||||
$ mkdir Source
|
||||
$ cd Source
|
||||
```
|
||||
|
||||
接下来,创建一个 Python 虚拟环境。这是 Python 开发者的常见做法,它有助于让你的 Python 项目独立于你的开发系统(在本例中是你的手机)。在你的虚拟环境中,你将能够安装特定于你应用的 Python 模块。
|
||||
|
||||
|
||||
```
|
||||
`$ python -m venv venv`
|
||||
```
|
||||
|
||||
激活你的新虚拟环境(注意,开头的两个点用空格隔开)
|
||||
|
||||
|
||||
```
|
||||
$ . ./venv/bin/activate
|
||||
(env)$
|
||||
```
|
||||
|
||||
请注意你的 shell 提示符现在以 **(env)** 开头,表示你在虚拟环境中。
|
||||
|
||||
现在使用 **pip** 安装 Flask Python 模块。
|
||||
|
||||
|
||||
```
|
||||
`(env) $ pip install flask`
|
||||
```
|
||||
|
||||
### 在 Android 上写 Python 代码
|
||||
|
||||
你已经准备好了。现在你需要为你的应用编写代码。
|
||||
|
||||
要做到这一点,你需要有经典文本编辑器的经验。我使用的是 **vi**。如果你不熟悉 **vi**,请安装并试用 **vimtutor**,它(如其名称所暗示的)可以教你如何使用这个编辑器。如果你有其他你喜欢的编辑器,如 **jove**、**jed**、**joe** 或 **emacs**,你可以安装并使用其中一个。
|
||||
|
||||
现在,由于这个演示程序非常简单,你也可以直接使用 shell 的 **heredoc** 功能,它允许你直接在提示符中输入文本。
|
||||
|
||||
|
||||
```
|
||||
(env)$ cat << EOF >> hello_world.py
|
||||
> from flask import Flask
|
||||
> app = Flask(__name__)
|
||||
>
|
||||
> @app.route('/')
|
||||
> def hello_world():
|
||||
> return 'Hello, World!'
|
||||
> EOF
|
||||
(env)$
|
||||
```
|
||||
|
||||
这是仅六行的代码,但有了它,你可以导入 Flask,创建一个应用,并将传入流量路由到名为 **hello_world** 的函数。
|
||||
|
||||
![Vim on Android][8]
|
||||
|
||||
现在你已经准备好了 Web 服务器的代码。现在是时候设置一些[环境变量][9],并在你的手机上启动一个 Web 服务器了。
|
||||
|
||||
|
||||
```
|
||||
(env) $ export FLASK_APP=hello_world.py
|
||||
(env) $ export FLASK_ENV=development
|
||||
(evn) $ python hello_world.py
|
||||
```
|
||||
|
||||
![Running a Flask app on your phone][10]
|
||||
|
||||
启动应用后,你会看到这条消息:
|
||||
|
||||
|
||||
```
|
||||
`serving Flask app… running on http://127.0.0.1:5000/`
|
||||
```
|
||||
|
||||
这表明你现在在 **localhost**(也就是你的设备)上运行着一个微型 Web 服务器。该服务器正在监听来自 5000 端口的请求。
|
||||
|
||||
打开你的手机浏览器并进入到 **<http://localhost:5000>**,查看你的网络应用。
|
||||
|
||||
![Your web app][11]
|
||||
|
||||
你并没有损害手机的安全性。你只运行了一个本地服务器,这意味着你的手机不接受来自外部世界的请求。只有你可以访问你的 Flask 服务器。
|
||||
|
||||
为了让别人看到你的服务器,你可以在**运行**命令中加入 **/\--host=0.0.0.0** 来禁用 Flask 的调试模式。这确实会打开手机上的端口,所以要谨慎使用。
|
||||
|
||||
|
||||
```
|
||||
(env) $ export FLASK_ENV=””
|
||||
(env) $ flask run –host=0.0.0.0
|
||||
```
|
||||
|
||||
按 **Ctrl+C** 停止服务器(使用特殊的 Termux 控制键)。
|
||||
|
||||
### 决定下一步怎么做
|
||||
|
||||
你的手机可能不是一个严肃的网络应用的理想服务器平台,但这个例子证明了可能性是无限的。你可能会在 Android 手机上编程,只是因为这是一种方便的实践方式,或者因为你有一个令人兴奋的本地化网络应用的新想法,或者你只是碰巧使用 Flask 应用来完成自己的日常任务。正如爱因斯坦曾经说过的”想象力比知识更重要“,对于任何一个新手编码者,或者一个经验丰富的 Linux 或 Android 爱好者来说,这是一个有趣的小项目。它可以扩展到无穷的层次,所以让你的好奇心接手,并做出一些令人兴奋的东西!
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/20/8/python-android-mobile
|
||||
|
||||
作者:[Phani Adabala][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/adabala
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/tux_penguin_linux_android.jpg?itok=ctgANLI7 (Tux and Android stuffed animals on shelf)
|
||||
[2]: https://opensource.com/article/18/4/flask
|
||||
[3]: https://opensource.com/article/20/8/termux
|
||||
[4]: https://play.google.com/store/apps/details?id=com.termux
|
||||
[5]: https://f-droid.org/repository/browse/?fdid=com.termux
|
||||
[6]: https://opensource.com/sites/default/files/termux-flask-1_0.webp (Welcome to Termux)
|
||||
[7]: https://opensource.com/sites/default/files/termux-install-python.webp (Install Python)
|
||||
[8]: https://opensource.com/sites/default/files/termux-python-vim.webp (Vim on Android)
|
||||
[9]: https://opensource.com/article/19/8/what-are-environment-variables
|
||||
[10]: https://opensource.com/sites/default/files/termux-flask-run.webp (Running a Flask app on your phone)
|
||||
[11]: https://opensource.com/sites/default/files/flask-app-android.webp (Your web app)
|
Loading…
Reference in New Issue
Block a user