UBports Training Update 4 aka 0100: How to import Python libraries in your Clickable app

With this monthly update we like to inform you about the progress of the UBports Training initiative. A lot has happened this month.

A challenging public development meeting

In our second public development meeting on May 11th, Juan Biondi (@yeyeto2788) joined and asked us to assist him in developing a Clickable app in Python. This immediately presented us with a problem: how do you import Python modules that aren’t included by default?

We struggled together to get this working, but Juan found a solution.

Here’s a quick description of the steps needed to be able to import any Python library in your Python-based Ubuntu Touch app.

First create a clean environment:

# Create a virtual environment
python3 -m venv .venv --system-site-packages
# Activate the virtual environment
source ./.venv/bin/activate
# Install Clickable
pip install clickable-ut
# Move into the app directory
cd appname
# Create directory for Python libraries
mkdir python-lib

Then we need to modify two files in order to add the dependencies to the build stage.

  • In the appname.apparmor file we need to add the read_path and write_path attributes with at least the following directories:

    • - /opt/click.ubuntu.com/appname.username

    • - /usr/local/lib/python3.5/dist-packages/

  • The other file you need to edit is the CMakeLists.txt file with the following lines:

    • - install(DIRECTORY python_lib DESTINATION ${DATA_DIR})

    • - file(GLOB_RECURSE I18N_SRC_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/po qml/*.qml qml/*.js python-lib) (Note that the only word being added here is our python-lib path.)

On the Python side of things it’s important to add the following code at the top of the main file:

import os
import sys

lib_path = os.path.join(
    os.path.abspath(os.path.dirname(os.path.dirname(__file__))),
    "python_lib",
)
sys.path.append(lib_path)

This tells Python where our libraries are.

To install libraries into our python-lib directory, execute the following command:

pip install <library name> --python-version=3.5 --target=./python_lib/ --only-binary=:all: --upgrade

This approach has some known limitations:

  • - Executing clickable might show errors and you may need to install the app on your phone by executing clickable install.

  • - The app will have to be manually reviewed before publishing it to OpenStore, as it is unconfined.

  • - There’s still investigation needed for non-Python binary dependencies, such as gpg.

Here’s a link to a working example with source code: Juan Biondi’s meme creator app for Ubuntu Touch.

A big thanks to Juan Biondi for providing us with this nice challenging evening.

We’re looking forward to the next public development meeting on June 1.

Next topic in our app development course: designing your Ubuntu Touch app with QML

Every update of this newsletter we like to put the spotlight on one of the modules of our Ubuntu Touch app development course.

Have a look at the third module, Designing your Ubuntu Touch app with QML.

In this third module we focus on designing the screen with the QML language.

  • - How to position buttons?

  • - How to make sure that the title is centered in the middle?

  • - How to change the background color of a list?

  • - How to pop up a confirmation dialog?

All those questions are answered in detail and described in module 3.

Have a good read!

Our next app

We are about to finish the Stream app. Its functionality is more or less complete. You can find the latest release in the OpenStore.

In the previous newsletter we asked for suggestions and ideas for a new app we could develop. After consulting the reactions we received, we decided to discover more about and develop our knowledge of the sensors of the phone.

The first sensor we want to use is GPS. Therefore we want to make an app that you can use to prepare a route to run and track the route you have been running. You can think of many other use cases, such as cycling and driving.

Hopefully we are able to build a nice app for others to build on. We’ll keep you posted on our progress.

Next public development meetings

The next public development meeting will be:

  • - Wednesday the 1st of June from 19:00 till 21:30 CEST

During the holiday season we will organize one public development meeting on:

  • - Wednesday the 13th of July from 19:00 till 21:30 CEST

We will come back in September on:

  • - Wednesday the 7th of September from 19:00 till 21:30 CEST

On any of these public development meetings you are more than welcome to enjoy an evening of programming Ubuntu Touch apps. We’ll be on the public Discord server in the Lobby room.

Thanks for reading our fourth edition of the monthly update.

Love to see you next time.

Kind regards,

UBports Training Team



Uitschrijven

Your biweekly UBports news letter is here, once again