Build a Python Windows app using your smartphone!
Build a Python Windows UI application using a Gitlab Windows runner.
Part of my work involves building a Windows apps that run powerful Python scripts for my users. I typically use tkinter to build the UI and nuitka to compile it into a standalone executable. I've been playing with the idea to automate the compilation step by using a CI/CD runner and recently found out that you can spin up a Windows shared runner on Gitlab for free, so I just had to test it out. Now the catch was that I wasn't close to my computer at the time and I only had my phone with me.
I enjoy a challenge so I thought: "Let me see if I can build this using only my smartphone. I just wanted a very simple test app, so I could copy-paste code from the internet.It should be doable without my IDE right?" So, I set out to build this using only my smartphone!
I created a new project on Gitlab and created the .gitlab-ci.yml
file first. I copied the code from this stack overflow answer and modified it. I first tried editing it using the Gitlab web editor but that proved impossible - the web editor is just not optimised for mobile devices at all, and I could not move around in the code. I decided to rather use a document editor on my phone (Google Docs). I copied the code there, made my changes and copied it back to Gitlab using the web editor.
The final .gitlab-ci.yml
code spins up the shared Windows runner, installs Python 3.9 using choco, then installs pyinstaller and builds the app and saves the output dist/app.exe to a build artifact that can later be downloaded.
.shared_windows_runners:
tags:
- shared-windows
- windows
- windows-1809
build:
extends:
- .shared_windows_runners
stage: build
script:
- choco install python --version=3.9.4 -y -f
- "C:\\Python39\\python.exe -m pip install -r requirements.txt"
- "C:\\Python39\\python.exe -m nuitka --onefile --assume-yes-for-downloads --enable-plugin=tk-inter --output-dir=dist app.py"
artifacts:
paths:
- dist/app.exe
Next, I created app.py
for the Python source code. I simply copied some code from this Real Python Tutorial on Python GUI Programming With Tkinter without modifying it. It is a text editor application that can create, open, edit, and save text files, a simple example to test the idea. The code looks like this:
import tkinter as tk
from tkinter.filedialog import askopenfilename, asksaveasfilename
def open_file():
"""Open a file for editing."""
filepath = askopenfilename(
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")]
)
if not filepath:
return
txt_edit.delete("1.0", tk.END)
with open(filepath, mode="r", encoding="utf-8") as input_file:
text = input_file.read()
txt_edit.insert(tk.END, text)
window.title(f"Simple Text Editor - {filepath}")
def save_file():
"""Save the current file as a new file."""
filepath = asksaveasfilename(
defaultextension=".txt",
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")],
)
if not filepath:
return
with open(filepath, mode="w", encoding="utf-8") as output_file:
text = txt_edit.get("1.0", tk.END)
output_file.write(text)
window.title(f"Simple Text Editor - {filepath}")
window = tk.Tk()
window.title("Simple Text Editor")
window.rowconfigure(0, minsize=800, weight=1)
window.columnconfigure(1, minsize=800, weight=1)
txt_edit = tk.Text(window)
frm_buttons = tk.Frame(window, relief=tk.RAISED, bd=2)
btn_open = tk.Button(frm_buttons, text="Open", command=open_file)
btn_save = tk.Button(frm_buttons, text="Save As...", command=save_file)
btn_open.grid(row=0, column=0, sticky="ew", padx=5, pady=5)
btn_save.grid(row=1, column=0, sticky="ew", padx=5)
frm_buttons.grid(row=0, column=0, sticky="ns")
txt_edit.grid(row=0, column=1, sticky="nsew")
window.mainloop()
This silly challenge turned out to be quite easy in the end and I showed that it is in fact possible to build a Windows Python app using only your smartphone and a Gitlab Windows runner. Editing code on a smartphone was not a great experience, but I guess it can be improved by using the right apps and a Bluetooth keyboard, but I'll stick to my computer and IDE for now.
Anyway, now that I can build Windows apps using a CI runner, then next logical step will be to try and automate a deployment cycle using the built artifact.