Developing Ubuntu Touch apps with Python using pyOtherSide

18 November 2019 Updated 19 November 2019 by Peter

Ubuntu Touch apps are developed using Qt5 and C++. Using pyOtherSide we can use Python instead of C++.

post main image
https://unsplash.com/@wildlittlethingsphoto

Probably many of you know only two mobile phone operating systems, iOS on Apple phones, and Googles Android on all other phones (some 80%). But there is also Ubuntu Touch (UT) originally developed by Canonical Ltd. and continued by UBports. I write a post about this because I am using UT on a Nexus 5 and wondered how difficult it would be to develop apps for it. When I discovered that you can use Python instead of C++, I thought that may be I can tempt you to look into Ubuntu Touch and start developing an app for it.

My history with Ubuntu Touch

I ordered the BQ Aquaris E4.5 Ubuntu Edition in April 2015 for me and a friend after Ubuntu Touch was released in 2014. After some six months of updates most problems were solved by Ubuntu and the phone became my daily driver, wonderful OS. I lost the phone during a trip and switched to Lineageos without Gaps. Although security is a serious topic in the Android custom ROM communities, privacy is not really hot, meaning that almost all custom ROMs 'phone home' to Google, when opening the phone, have default contact search by Google enabled, try to extract and send your location, etc., not even mentioning all the vulnerabilites and bad apps we hear of the whole time. Google is just doing everything to get your data, they get fined but continue. I think you can call them thieves. Sick of this I recently bought an old Nexus 5, flashed UT on it and am happy again.

Ubuntu Touch today

The UBports team made tremendous progress the last year but there is still very much to do. To mention a few. Move to Ubuntu 20.04. There is a Bluetooth problem, that prevents you using your carkit (no sound during call). You cannot block a phone number. The number of applications is limited, and a number of apps are still being polished. FluffyChat, a Matrix client, does not support end-to-end encryption. Dekko 2, the mail client, is being updated. This is the price you pay for privacy. 

Ubuntu Touch application development

UT applications are typically developed using Qt5 and C++. Fortunately pyOtherSide was created that allows us to call Python from within QML. To start development you must install Clickable, see links below. I run Ubuntu 18.04 with Python 3.6, so this is easy. First I created a Python virtual environment and activated it. To install Clickable type:

pip3 install git+https://gitlab.com/clickable/clickable.git

Next you can create your first application by typing:

clickable create

This will ask you some questions and generates an UT application. You can then run this application on your desktop by typing:

clickable desktop

If you want to run it on your UT phone, first make sure you enabled Developer mode on your phone:

System settings -> About -> Developer mode

Then type:

clickable

and the application in loaded and started in the phone. This sounds easy and it is, but it is just a starting point.

Using Python and pyOtherSide

We can replace C++  with Python by using pyOtherSide. To make things work install pyOtherSide:

sudo ap-get install pyotherside

The application below shows a window, dimensions 200x200, with a text 'Nothing here yet' and a button. When you click the button the text should change to 'Hello from Python!'. The new text comes from a function hello_world in applogic.py. We create two files, in the same directory, first one is hello.qml:

// hello.qml

import QtQuick 2.6
import QtQuick.Controls 1.2

import io.thp.pyotherside 1.0


Rectangle {
    width: 200
    height: 200
    color: 'blue'

    Python {
        id: py
        Component.onCompleted: {
            // Print version of plugin and Python interpreter
            console.log('PyOtherSide version: ' + pluginVersion());
            console.log('Python version: ' + pythonVersion());
            
            addImportPath(Qt.resolvedUrl('.'));
            importModule('applogic', function() {});
            console.log('after importModule');
        }
    }

    Label {
        id: label; 
        text: "Nothing here yet" 
    }

    Button {
        text: "press"
        anchors.centerIn: parent
        onClicked: {
            console.log('in onClicked');

            py.call('applogic.hello_world', [], function(result) {
                // async call
                console.log('after call');
                label.text = result;
            });
        }
    }
}

and the other one is applogic.py:

# applogic.py

def hello_world():
    print("printing ... Hello from Python!")
    return "Hello from Python!"

Now we can run this with qmlscene. This allows us to run it without being complete:

qmlscene hello.qml

It should show a window and prints to the console:

loadRulesFromFile: Loading "/home/peter/.config/QtProject/qtlogging.ini" ...
onCompleted: PyOtherSide version: 1.4.0
onCompleted: Python version: 3.6.2
onCompleted: after importModule

After clicking the button the console should say:

onClicked: in onClicked
printing ... Hello from Python!
: after call

and the text in the popup should change to: Hello from Python! If that's working then also take a look at the examples available in the pyOtherSide repository.

Using QtCreator

We definitely do not want design the screens by hand ourselves and that is where QtCreator comes in. If it is not installed yet you can install it using Ubuntu Software Center. Now you can open hello.qml, edit the screen, save it and run it again.

More advanced

I suggest you look at other UT apps created using Python. An example is ActivityTracker, the code is on Github. This app also uses SQLite as a database. You can get it by typing:

git clone https://github.com/ernesst/ActivityTracker.git

You can run it on your desktop or phone using Clickable as described above. Because it uses GPS you cannot do much on your desktop ...

Summary

I am a total noob on Ubuntu Touch app development, the above is just a summary of my research for a day. You can build Ubuntu Touch apps with Python and it is not that difficult. If I find some time I will try to build an Ubuntu Touch app, I hope you will too.

Links / credits

ActivityTracker app for Ubuntu
https://github.com/ernesst/ActivityTracker

App development
http://docs.ubports.com/en/latest/appdev/

Asynchronous Python 3 Bindings for Qt 5
https://github.com/thp/pyotherside

Clickable
http://clickable.bhdouglass.com/en/latest/

Creating Ubuntu Phone Apps using Python and Qt
https://github.com/timsueberkrueb/ubports-gettogether/blob/master/timsueberkrueb/pyotherside/presentation.md

How do I get my QML program to utilize Unity7 notifications? Is there an API I can use?
https://askubuntu.com/questions/540352/how-do-i-get-my-qml-program-to-utilize-unity7-notifications-is-there-an-api-i-c

No valid kits found, although a kit exists
https://askubuntu.com/questions/696740/no-valid-kits-found-although-a-kit-exists

Qt 5 Hello World Tutorial using Qt Creator
https://prognotes.net/2016/11/qt-5-hello-world-tutorial-using-qt-creator/

[SOLVED]How to Import Python library to clickable?
https://forums.ubports.com/topic/1321/solved-how-to-import-python-library-to-clickable