Python Fellow, ASF Member and hacker
I’d heard plenty of great things about the BeeWare project for Python over the past year. Python certainly doesn’t come to mind when you think about mobile app development, but it is a language and the tools exist to compile/convert it into native code on iOS, Android and Windows.
In this tutorial I will show how to build a basic TV remote for the Panasonic Viera TVs on iPhone.
Creating a test project is straightforward, we download a package called briefcase
pip install briefcase, make a new folder for our project
mkdir tv_remote and then within that creating a folder called
Remote with the files
__init__.py can be empty,
app.py is our entry point so at a minimum we need the boiler plate code below
Also create a file
setup.py with some information on the package. This is where we describe to briefcase which platforms we want to build for. Also, very importantly we can specify the
pip packages that this app will need. Our’s need the toga UI library and also a Python library for controlling the TV called PyViera.
Once all that is done, your directory should look like this.
Now you can run (from within
tv_remote directory) a command to download all the packages and components and build an xcode project
python setup.py ios
After a few minutes you’ll get an iOS subdirectory and an xcodeproj directory which you can open from XCode.
Going into XCode and building that project, it will start up the iPhone simulator and we have our working Hello World application. You’ll see in the bottom right a console window where you can send debug info for your app.
Nice! That was insanely easy.
Our second prototype, we’re going to add button to change the channel, nothing complicated, just for the number 5.
Rebuilding that app and launching it, once the discover TVs method operates, it’ll ask for network permissions so we know it’s working correctly.
You’ll need to turn the TV on first, then try pressing that number 5!
Now working through a full example, we want to understand the layout model for Toga. Within Toga you can apply CSS styles for margin, padding, box layout and justification.
In this more detailed example we can see creating 5 rows of buttons and aligning them to draw a remote
Go ahead and build the project (making sure you’re editing app.py within the xcode project, not the original directory) and voila! A working TV remote.
This isn’t about not having to learn Swift or Objective-C, Python is an ecosystem not just a language.
Using this pattern, you can take any of the 90,000+ packages on PyPi and turn them into simple mobile apps.
Going back over those steps, simply adjust setup.py to have the ‘android’ platform and run
python setup.py android and briefcase will build out an Android project for you.
Create your free account to unlock your custom reading experience.