Last active
March 29, 2023 13:15
-
-
Save nkpro2000sr/2d2a84e70470c8a8314006fad48ac3f8 to your computer and use it in GitHub Desktop.
colab with python, kivy, buildozer. To build apps easily online, no need to download SDK, NDK, ... 😩
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "kivy-buildozer-colab.ipynb", | |
"provenance": [], | |
"collapsed_sections": [] | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"# PYTHON KIVY BUILDOZER with noVNC\n", | |
"\n", | |
"* lang : [python3](https://www.python.org/ \"general-purpose programming language\")\n", | |
"\n", | |
"* gui : [kivy](https://kivy.org/#home \"cross-platform Python framework | multi-touch UI\")\n", | |
"\n", | |
"* builder : [buildozer](https://buildozer.readthedocs.io/en/latest/ \"to package mobiles application easily\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## Coding" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"### Persistence mode\n", | |
"using google drive" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"from google.colab import drive\n", | |
"drive.mount('/content/drive')" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# ! mkdir -p '/content/drive/My Drive/Colab Notebooks/buildozer'\n", | |
"# ! mkdir -p '/content/drive/My Drive/Colab Notebooks/root-buildozer'\n", | |
"# ! mkdir -p '/root/.buildozer' '/content/buildozer'\n", | |
"#\n", | |
"# ! mount --bind '/content/drive/My Drive/Colab Notebooks/buildozer' '/content/buildozer/.buildozer'\n", | |
"# ! mount --bind '/content/drive/My Drive/Colab Notebooks/root-buildozer' '/root/.buildozer'\n", | |
"# ! rm /content/buildozer/.buildozer/android/platform/build-armeabi-v7a/dists/myapp__armeabi-v7a/build/outputs/apk/debug/*.apk\n", | |
"#\n", | |
"## raises some permission errors :(" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# extract (approximately 2m30s)\n", | |
"! tar -xzvf '/content/drive/My Drive/Colab Notebooks/buildozer.tar.gz' -C /\n", | |
"! tar -xzvf '/content/drive/My Drive/Colab Notebooks/root-buildozer.tar.gz' -C /\n", | |
"\n", | |
"# just clearing old apk.\n", | |
"! rm /content/buildozer/.buildozer/android/platform/build-armeabi-v7a/dists/myapp__armeabi-v7a/build/outputs/apk/debug/*.apk" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# archive\n", | |
"! mkdir -p '/content/drive/My Drive/Colab Notebooks'\n", | |
"! tar -czvf '/content/drive/My Drive/Colab Notebooks/buildozer.tar.gz' /content/buildozer/.buildozer\n", | |
"! tar -czvf '/content/drive/My Drive/Colab Notebooks/root-buildozer.tar.gz' /root/.buildozer" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"### Uploding or writing the code" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"! mkdir -p /content/buildozer\n", | |
"%cd /content/buildozer\n", | |
"\n", | |
"from google.colab import files\n", | |
"files.upload()\n", | |
"\n", | |
"# (OR) #" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"%%writefile main.py\n", | |
"\n", | |
"__version__ = '0.0.0'\n", | |
"\n", | |
"from kivy.app import App\n", | |
"from kivy.uix.button import Button\n", | |
"\n", | |
"class TestApp(App):\n", | |
" def build(self):\n", | |
" return Button(text='Hello World')\n", | |
"\n", | |
"TestApp().run()" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## Building app with BUILDOZER\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"### Installing Dependencies" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# https://buildozer.readthedocs.io/en/latest/installation.html#android-on-ubuntu-20-04-64bit\n", | |
"! apt install -qq -y git zip unzip openjdk-8-jdk python3-pip autoconf libtool pkg-config zlib1g-dev libncurses5-dev libncursesw5-dev libtinfo5 cmake libffi-dev libssl-dev\n", | |
"! pip3 install --upgrade Cython==0.29.19 virtualenv\n", | |
"\n", | |
"! apt install python-kivy lld\n", | |
"! pip3 install kivy buildozer\n", | |
"\n", | |
"import os\n", | |
"os.environ['PATH'] += ':~/.local/bin/'" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"### Building" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"! yes | buildozer init" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"! yes | buildozer -v android debug" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## VNC" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"### Installing Dependencies" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"! sudo apt update -qq\n", | |
"! apt-get install -qq -y xvfb x11vnc # Install X Virtual Frame Buffer\n", | |
"! apt-get install -qq -y xfce4 xfce4-goodies\n", | |
"import os\n", | |
"os.system('Xvfb :1 -screen 0 1600x1200x16 &') # create virtual display with size 1600x1200 and 16 bit color. Color can be changed to 24 or 8\n", | |
"os.environ['DISPLAY']=':1.0' # tell X clients to use our virtual DISPLAY :1.0\n", | |
"os.system('startxfce4 > /dev/null &') # start xfce\n", | |
"! read -p 'Enter Passwd > ' -s PASSWD && mkdir -p /content/.vnc && x11vnc -storepasswd $PASSWD /content/.vnc/passwd\n", | |
"\n", | |
"# noVNC\n", | |
"! apt-get install -qq -y git netcat \n", | |
"! git clone --depth 1 https://github.com/novnc/noVNC.git /content/noVNC\n", | |
"! git clone --depth 1 https://github.com/novnc/websockify /content/noVNC/utils/websockify\n", | |
"from google.colab import output\n", | |
"def port_out(port):\n", | |
" return output.serve_kernel_port_as_window(port)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"### For testing before build" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"os.system('x11vnc -display $DISPLAY -rfbauth /content/.vnc/passwd -rfbport 5900 -forever &')" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"os.system('/content/noVNC/utils/launch.sh --vnc localhost:5900 --listen 5980 &')" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"! pip install pyngrok\n", | |
"from pyngrok import ngrok\n", | |
"public_url = ngrok.connect(5980)\n", | |
"print(public_url)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
For PERMISSION ERROR
just dochmod 777 that/file
No need for now 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try Now 😉