Package a PyApp as a Flatpak Package:
An HTTP Server for Example
潘建宏 Jian-Hong Pan (StarNight)
@ PyCon APAC 2022 Lightning Talk
Who am I
潘建宏 / Jian-Hong Pan (StarNight)
Endless OS Foundation
You can find me at
● http://www.slideshare.net/chienhungpan/
● GitHub: starnight
● Email:
jhp [AT] endlessos.org
chienhung.pan [AT] gmail.com
Flatpak
Flatpak is a framework for distributing desktop applications across
various Linux distributions.
● Rootless install
● Sandboxed applications: Applications that are run with Flatpak
have limited access to the host environment.
● Desktop integration
● Delta updates
● Space efficiency: Flatpak deduplicates libraries (runtime) and other
files used by multiple applications
● Bundling: Allows application developers to ship almost any
dependency or library as part of their application.
● Branches: stable, beta
The Comparison Between Container and Flatpak
App1
Bins/Libs
Container Engine
App2
Bins/Libs
App3
Bins/Libs
Operating System
Containers
App1
Flatpak Runtimes provide
basic binaries and libraries
App2 App3
Operating System
Flatpaks
Install Flatpak
Some major Linux distributions, like Fodora, openSUSU and Endless OS
have already pre-installed flatpak.
● apt install flatpak flatpak-builder
● yum install flatpak flatpak-builder
● dnf install flatpak flatpak-builder
● pacman -S flatpak flatpak-builder
● apk add flatpak flatpak-builder
● …
Note: Not only x86(_64) platforms, but also arm64 and so on …
HTTP Server with Bottle
$ cat main.py
#!/bin/env python3
from bottle import Bottle, run
app = Bottle()
@app.route('/')
@app.route('/hello/<name>')
def hello(name='Stranger'):
return "Hello {}!".format(name)
run(app, host='localhost', port=8080)
$ cat requirements.txt
bottle
Now, Build as a Flatpak App
Flatpak’s Building Introduction
Get flatpak-pip-generator
Because the HTTP server is written in Python and depends on bottle, the
bottle must be installed as part of the flatpak app as well. So, we need
flatpak-builder-tools to generate the required module's meta
information for the flatpak’s manifest.
$ git clone https://github.com/flatpak/flatpak-builder-tools
$ pip install requirements-parser
PS. flatpak-builder-tools depends on requirements-parser
Generate Dependecy List
Use flatpak-pip-generator to generate the python3-requirements.json as
the module in the manifest io.github.starnight.http.yml.
$ ~/flatpak-builder-tools/pip/flatpak-pip-generator --requirements-file=requirements.txt
…
Running: "pip3 download --exists-action=i --dest
/tmp/pip-generator-python3-requirementsbfht6p35 -r requirements.txt"
Collecting bottle
Downloading bottle-0.12.23-py3-none-any.whl (90 kB)
…
Generating dependencies for bottle
Output saved to python3-requirements.json
$ cat python3-requirements.json
{
"name": "python3-bottle",
"buildsystem": "simple",
"build-commands": [
"pip3 install --verbose --exists-action=i --no-index --find-links="file://${PWD}"
--prefix=${FLATPAK_DEST} "bottle" --no-build-isolation"
],
"sources": [
{
"type": "file",
"url":
"https://files.pythonhosted.org/packages/63/96/2713bb6e0ca10ee2b7be568d6e6d112cf0a1b604
e14879fe744f48ebbed6/bottle-0.12.23-py3-none-any.whl",
"sha256": "9f1c363257c590bd34db5fad4693a7f06ff4217e9ad18337451de69c25137127"
}
]
}
Have the manifest io.github.starnight.http.yml
$ cat io.github.starnight.http.yml
app-id: io.github.starnight.http
runtime: org.freedesktop.Platform
runtime-version: '22.08'
sdk: org.freedesktop.Sdk
command: main.py
finish-args:
- --share=network
cleanup-commands:
- rm -rf /app/bin/__pycache__
(continue to right side)
modules:
- python3-requirements.json
- name: http
buildsystem: simple
build-commands:
- install -D main.py /app/bin/main.py
sources:
- type: file
path: main.py
The Runtime Environment
$ flatpak run --command=sh org.freedesktop.Sdk
[📦 org.freedesktop.Sdk ~]$ ls -l /bin/{python,pip}
-rwxr-xr-x 4 zack zack 221 Jan 1 1970 /bin/pip
lrwxrwxrwx 1 zack zack 7 Sep 3 20:34 /bin/python -> python3
$ flatpak run --command=sh org.freedesktop.Platform
[📦 org.freedesktop.Platform ~]$ ls -l /bin/python
lrwxrwxrwx 1 zack zack 7 Sep 3 20:35 /bin/python -> python3
Files in the Working Directory
$ ls -ltr
total 16
-rwxr-xr-x 1 zack zack 230 Sep 3 18:15 main.py
-rw-r--r-- 1 zack zack 7 Sep 3 18:15 requirements.txt
-rw-r--r-- 1 zack zack 564 Sep 3 18:31 python3-requirements.json
-rw-r--r-- 1 zack zack 417 Sep 3 20:03 io.github.starnight.http.yml
Original source files
Files for Flatpak packaging
Build the io.github.starnight.http as a flatpak app
$ flatpak-builder build-dir io.github.starnight.http.yml
Install the io.github.starnight.http flatpak app
● $ flatpak-builder build-dir 
--force-clean --user --install io.github.starnight.http.yml
List Installed Flatpaks
$ flatpak list --columns=name,application,version,branch,origin
Name Application ID Version Branch Origin
http io.github.starnight.http master http-origin
Freedesktop Platform org.freedesktop.Platform 22.08.0 22.08 flathub
Mesa org.freedesktop.Platform.GL.default mesa-22.1.7 22.08 flathub
Intel org.freedesktop.Platform.VAAPI.Intel 22.08 flathub
openh264 org.freedesktop.Platform.openh264 2.1.0 2.0 flathub
openh264 org.freedesktop.Platform.openh264 2.1.0 2.3.0 flathub
Freedesktop SDK org.freedesktop.Sdk 22.08.0 22.08 flathub
Check the io.github.starnight.http Flatpak App
$ flatpak info io.github.starnight.http
ID: io.github.starnight.http
Ref: app/io.github.starnight.http/x86_64/master
Arch: x86_64
Branch: master
Origin: http-origin
Collection:
Installation: user
Installed: 459.8 kB
Runtime: org.freedesktop.Platform/x86_64/22.08
Sdk: org.freedesktop.Sdk/x86_64/22.08
Commit: b2c495a50e9b586dd07065246cb935baed02a2402526966b23ce4561b17802e6
Subject: Export io.github.starnight.http
Date: 2022-09-03 12:36:39 +0000
Run the io.github.starnight.http Flatpak App
$ flatpak run io.github.starnight.http
Bottle v0.12.23 server starting up (using WSGIRefServer())...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.
127.0.0.1 - - [03/Sep/2022 20:43:40] "GET / HTTP/1.1" 200 15
127.0.0.1 - - [03/Sep/2022 20:43:40] "GET /favicon.ico HTTP/1.1" 404 742
$ flatpak run --command=sh io.github.starnight.http
[📦 io.github.starnight.http ~]$ ls /app/bin/
bottle.py main.py
More like a Desktop Application
The Idea is …
We need .desktop as the desktop launcher and an icon.
1. User clicks the launcher.
2. Execute the HTTP server.
3. Open the browser and browse http://localhost:8080 automatically.
We can use the XDG feature to lauch the browser with the
command “xdg-open http://localhost:8080”.
$ cat run.sh
#!/usr/bin/sh
main.py &
xdg-open http://localhost:8080
Desktop Launcher
$ cat io.github.starnight.http.desktop
[Desktop Entry]
Name=flatpak-http
Exec=run.sh
Terminal=false
Type=Application
StartupNotify=true
MimeType=text/plain;
# TRANSLATORS: Do NOT translate or transliterate this text!
# This is an icon file name.
Icon=io.github.starnight.http
Icon
Choose an SVG icon and save it as io.github.starnight.http.svg.
Source: https://www.pngrepo.com/svg/340417/http
Modify the manifest io.github.starnight.http.yml
● The command becomes run.sh
● Install the desktop launcher io.github.starnight.http.desktop, the
icon io.github.starnight.http.svg and the script run.sh into proper
lcoation.
$ cat io.github.starnight.http.yml
app-id: io.github.starnight.http
runtime: org.freedesktop.Platform
runtime-version: '22.08'
sdk: org.freedesktop.Sdk
command: run.sh
finish-args:
- --share=network
cleanup-commands:
- rm -rf /app/bin/__pycache__
modules:
- python3-requirements.json
- name: http
buildsystem: simple
build-commands:
- install -D main.py /app/bin/main.py
- install -D run.sh /app/bin/run.sh
- mkdir -p /app/share/applications
- install -D io.github.starnight.http.desktop /app/share/applications/io.github.starnight.http.desktop
- mkdir -p /app/share/icons/hicolor/scalable/apps
- install -D io.github.starnight.http.svg /app/share/icons/hicolor/scalable/apps/
sources:
- type: file
path: main.py
- type: file
path: run.sh
- type: file
path: io.github.starnight.http.desktop
- type: file
path: io.github.starnight.http.svg
Build & Run Again
$ flatpak-builder build-dir --user --install --force-clean io.github.starnight.http.yml
$ flatpak run io.github.starnight.http
Reference
● Flatpak’s documents
○ Basic concepts
○ Building your first Flatpak
○ Manifests
○ Sandbox Permissions
○ Python
● Flathub
● Have an HTTP Server in Flatpak
Thanks for Your Attention ~

Package a PyApp as a Flatpak Package: An HTTP Server for Example @ PyCon APAC 2022 Lightning Talk

  • 1.
    Package a PyAppas a Flatpak Package: An HTTP Server for Example 潘建宏 Jian-Hong Pan (StarNight) @ PyCon APAC 2022 Lightning Talk
  • 2.
    Who am I 潘建宏/ Jian-Hong Pan (StarNight) Endless OS Foundation You can find me at ● http://www.slideshare.net/chienhungpan/ ● GitHub: starnight ● Email: jhp [AT] endlessos.org chienhung.pan [AT] gmail.com
  • 3.
    Flatpak Flatpak is aframework for distributing desktop applications across various Linux distributions. ● Rootless install ● Sandboxed applications: Applications that are run with Flatpak have limited access to the host environment. ● Desktop integration ● Delta updates ● Space efficiency: Flatpak deduplicates libraries (runtime) and other files used by multiple applications ● Bundling: Allows application developers to ship almost any dependency or library as part of their application. ● Branches: stable, beta
  • 4.
    The Comparison BetweenContainer and Flatpak App1 Bins/Libs Container Engine App2 Bins/Libs App3 Bins/Libs Operating System Containers App1 Flatpak Runtimes provide basic binaries and libraries App2 App3 Operating System Flatpaks
  • 5.
    Install Flatpak Some majorLinux distributions, like Fodora, openSUSU and Endless OS have already pre-installed flatpak. ● apt install flatpak flatpak-builder ● yum install flatpak flatpak-builder ● dnf install flatpak flatpak-builder ● pacman -S flatpak flatpak-builder ● apk add flatpak flatpak-builder ● … Note: Not only x86(_64) platforms, but also arm64 and so on …
  • 6.
    HTTP Server withBottle $ cat main.py #!/bin/env python3 from bottle import Bottle, run app = Bottle() @app.route('/') @app.route('/hello/<name>') def hello(name='Stranger'): return "Hello {}!".format(name) run(app, host='localhost', port=8080) $ cat requirements.txt bottle
  • 7.
    Now, Build asa Flatpak App Flatpak’s Building Introduction
  • 8.
    Get flatpak-pip-generator Because theHTTP server is written in Python and depends on bottle, the bottle must be installed as part of the flatpak app as well. So, we need flatpak-builder-tools to generate the required module's meta information for the flatpak’s manifest. $ git clone https://github.com/flatpak/flatpak-builder-tools $ pip install requirements-parser PS. flatpak-builder-tools depends on requirements-parser
  • 9.
    Generate Dependecy List Useflatpak-pip-generator to generate the python3-requirements.json as the module in the manifest io.github.starnight.http.yml. $ ~/flatpak-builder-tools/pip/flatpak-pip-generator --requirements-file=requirements.txt … Running: "pip3 download --exists-action=i --dest /tmp/pip-generator-python3-requirementsbfht6p35 -r requirements.txt" Collecting bottle Downloading bottle-0.12.23-py3-none-any.whl (90 kB) … Generating dependencies for bottle Output saved to python3-requirements.json
  • 10.
    $ cat python3-requirements.json { "name":"python3-bottle", "buildsystem": "simple", "build-commands": [ "pip3 install --verbose --exists-action=i --no-index --find-links="file://${PWD}" --prefix=${FLATPAK_DEST} "bottle" --no-build-isolation" ], "sources": [ { "type": "file", "url": "https://files.pythonhosted.org/packages/63/96/2713bb6e0ca10ee2b7be568d6e6d112cf0a1b604 e14879fe744f48ebbed6/bottle-0.12.23-py3-none-any.whl", "sha256": "9f1c363257c590bd34db5fad4693a7f06ff4217e9ad18337451de69c25137127" } ] }
  • 11.
    Have the manifestio.github.starnight.http.yml $ cat io.github.starnight.http.yml app-id: io.github.starnight.http runtime: org.freedesktop.Platform runtime-version: '22.08' sdk: org.freedesktop.Sdk command: main.py finish-args: - --share=network cleanup-commands: - rm -rf /app/bin/__pycache__ (continue to right side) modules: - python3-requirements.json - name: http buildsystem: simple build-commands: - install -D main.py /app/bin/main.py sources: - type: file path: main.py
  • 12.
    The Runtime Environment $flatpak run --command=sh org.freedesktop.Sdk [📦 org.freedesktop.Sdk ~]$ ls -l /bin/{python,pip} -rwxr-xr-x 4 zack zack 221 Jan 1 1970 /bin/pip lrwxrwxrwx 1 zack zack 7 Sep 3 20:34 /bin/python -> python3 $ flatpak run --command=sh org.freedesktop.Platform [📦 org.freedesktop.Platform ~]$ ls -l /bin/python lrwxrwxrwx 1 zack zack 7 Sep 3 20:35 /bin/python -> python3
  • 13.
    Files in theWorking Directory $ ls -ltr total 16 -rwxr-xr-x 1 zack zack 230 Sep 3 18:15 main.py -rw-r--r-- 1 zack zack 7 Sep 3 18:15 requirements.txt -rw-r--r-- 1 zack zack 564 Sep 3 18:31 python3-requirements.json -rw-r--r-- 1 zack zack 417 Sep 3 20:03 io.github.starnight.http.yml Original source files Files for Flatpak packaging
  • 14.
    Build the io.github.starnight.httpas a flatpak app $ flatpak-builder build-dir io.github.starnight.http.yml Install the io.github.starnight.http flatpak app ● $ flatpak-builder build-dir --force-clean --user --install io.github.starnight.http.yml
  • 15.
    List Installed Flatpaks $flatpak list --columns=name,application,version,branch,origin Name Application ID Version Branch Origin http io.github.starnight.http master http-origin Freedesktop Platform org.freedesktop.Platform 22.08.0 22.08 flathub Mesa org.freedesktop.Platform.GL.default mesa-22.1.7 22.08 flathub Intel org.freedesktop.Platform.VAAPI.Intel 22.08 flathub openh264 org.freedesktop.Platform.openh264 2.1.0 2.0 flathub openh264 org.freedesktop.Platform.openh264 2.1.0 2.3.0 flathub Freedesktop SDK org.freedesktop.Sdk 22.08.0 22.08 flathub
  • 16.
    Check the io.github.starnight.httpFlatpak App $ flatpak info io.github.starnight.http ID: io.github.starnight.http Ref: app/io.github.starnight.http/x86_64/master Arch: x86_64 Branch: master Origin: http-origin Collection: Installation: user Installed: 459.8 kB Runtime: org.freedesktop.Platform/x86_64/22.08 Sdk: org.freedesktop.Sdk/x86_64/22.08 Commit: b2c495a50e9b586dd07065246cb935baed02a2402526966b23ce4561b17802e6 Subject: Export io.github.starnight.http Date: 2022-09-03 12:36:39 +0000
  • 17.
    Run the io.github.starnight.httpFlatpak App $ flatpak run io.github.starnight.http Bottle v0.12.23 server starting up (using WSGIRefServer())... Listening on http://localhost:8080/ Hit Ctrl-C to quit. 127.0.0.1 - - [03/Sep/2022 20:43:40] "GET / HTTP/1.1" 200 15 127.0.0.1 - - [03/Sep/2022 20:43:40] "GET /favicon.ico HTTP/1.1" 404 742 $ flatpak run --command=sh io.github.starnight.http [📦 io.github.starnight.http ~]$ ls /app/bin/ bottle.py main.py
  • 18.
    More like aDesktop Application
  • 19.
    The Idea is… We need .desktop as the desktop launcher and an icon. 1. User clicks the launcher. 2. Execute the HTTP server. 3. Open the browser and browse http://localhost:8080 automatically. We can use the XDG feature to lauch the browser with the command “xdg-open http://localhost:8080”. $ cat run.sh #!/usr/bin/sh main.py & xdg-open http://localhost:8080
  • 20.
    Desktop Launcher $ catio.github.starnight.http.desktop [Desktop Entry] Name=flatpak-http Exec=run.sh Terminal=false Type=Application StartupNotify=true MimeType=text/plain; # TRANSLATORS: Do NOT translate or transliterate this text! # This is an icon file name. Icon=io.github.starnight.http
  • 21.
    Icon Choose an SVGicon and save it as io.github.starnight.http.svg. Source: https://www.pngrepo.com/svg/340417/http
  • 22.
    Modify the manifestio.github.starnight.http.yml ● The command becomes run.sh ● Install the desktop launcher io.github.starnight.http.desktop, the icon io.github.starnight.http.svg and the script run.sh into proper lcoation.
  • 23.
    $ cat io.github.starnight.http.yml app-id:io.github.starnight.http runtime: org.freedesktop.Platform runtime-version: '22.08' sdk: org.freedesktop.Sdk command: run.sh finish-args: - --share=network cleanup-commands: - rm -rf /app/bin/__pycache__
  • 24.
    modules: - python3-requirements.json - name:http buildsystem: simple build-commands: - install -D main.py /app/bin/main.py - install -D run.sh /app/bin/run.sh - mkdir -p /app/share/applications - install -D io.github.starnight.http.desktop /app/share/applications/io.github.starnight.http.desktop - mkdir -p /app/share/icons/hicolor/scalable/apps - install -D io.github.starnight.http.svg /app/share/icons/hicolor/scalable/apps/ sources: - type: file path: main.py - type: file path: run.sh - type: file path: io.github.starnight.http.desktop - type: file path: io.github.starnight.http.svg
  • 25.
    Build & RunAgain $ flatpak-builder build-dir --user --install --force-clean io.github.starnight.http.yml $ flatpak run io.github.starnight.http
  • 26.
    Reference ● Flatpak’s documents ○Basic concepts ○ Building your first Flatpak ○ Manifests ○ Sandbox Permissions ○ Python ● Flathub ● Have an HTTP Server in Flatpak
  • 27.
    Thanks for YourAttention ~