like several programming languages. • I've been using Python regularly for over 20 years and my favorite programming language to use. • My past python experience: ◦ Hobby: i2y/mochi: Dynamically typed functional programming language (github.com) ◦ Job: document translation (combination of Natural Language Processing, Image Processing and ML) on AWS Step Functions Self Introduction
I have used various languages depending on the requirements. • In the applications and backend systems at my current employer, various programming languages are used depending on the purpose. Technology Stack | Mercari Engineering • This is common since different languages and frameworks have different strengths and weaknesses. Use of various programming languages and frameworks
can be written in one language, it reduces the burden on the brain when switching a language to another language on programming. • Switching Cost 2: I may not need to acquire knowledge of multiple languages and frameworks. • Hiring Cost?: Hiring someone may be easier (if it’s not an obscure language). To avoid incurring these costs, there are two directions to take: write the web- frontend/app parts and backend parts in a single language or framework. Or write all UI code (all frontend/app parts) in less languages and frameworks. I don't want to write code from scratch for each platform, especially in the case of UI code, which can be mostly the same. I think its’ a common desire for other programmers too. Want to write apps/services using less languages or frameworks if I can..
years Note: Please forgive that the search is based on Jetpack Compose Multiplatform instead of Compose Multiplatform, but it doesn't change most results.
Kotlin ◦ These are primarily implemented in that language and have APIs specific to that language. These are not wrappers for existing UI frameworks. So… it’s easy to use in that language and seems to be easy to hack or understand. • Declarative UI: UI can be described declaratively in the language ◦ Other than Flutter and Compose Multiplatform, most of the other UI frameworks that have emerged in the recent past claim to offer Declarative UI (React Native/React, SwiftUI, etc.) Common Features of Flutter and Compose Multiplatform
seem to be a clear definition. Of course, Wikipedia doesn’t have an entry for it. However, roughly speaking, it seems that the display (view) and state are defined declaratively, the view switches automatically when the state changes. Also, the view can be defined structurally. Non-declarative UI is sometimes called Imperative UI. # Declarative UI state = State(“text”) view = Page(Text(state), Button(“button”) .on_click(lambda: state.set(“text2”))) view.show() click
= Text(“text”) p.add_child(txt) btn = Button(“button”) p.add_child(btn) def change_text(): p.remove_child(txt) new_txt = Text(“text2”) p.add_child(new_text) btn.on_click(change_text) p.show() # Declarative UI state = State(“text”) view = Page(Text(state), Button(“button”) .on_click(lambda: state.set(“text2”))) view.show() I've done some research, but there doesn’t seem to be a clear definition. Of course, Wikipedia doesn’t have an entry for it. However, roughly speaking, it seems that the display (view) and state are defined declaratively, the view switches automatically when the state changes. Also, the view can be defined structurally. Non-declarative UI is sometimes called Imperative UI.
Especially something that isn’t a wrapper for an existing framework. • It may be tough to use for product applications, but it could be used for internal tools or for private use. Is there a cross-platform declarative UI framework in the Python World?
two platform supported) Name Declarative UI View can be defined declaratively in Python Not a wrapper Qt for Python (PySide6) ✔ (QML) (Qt) Kivy ✔ (Kv Language) ✔ Tkinter (tcl/tk) wxPython (WxWidgets) Enaml ✔ (Qt) Toga ✔ ✔ (OS Natives/Web) Flexx ✔ ✔ (Web) Edifice ✔ ✔ (Qt)
and feel. This has changed recently as web-based applications have become more common. ① Both Flutter and Compose Multiplatform use Skia. This is an important and essential part of the implementation, but it or an equivalent have only recently become available in Python. ③ The demand for writing views declaratively had been met by KV language of Kivy and others, so the motivation to create such an API from scratch was weak in the community and among individuals. ② CPU and GPU performance and the libraries to utilize them were lacking in the past. ④
Python using Python from scratch. ◦ Except for dependent window management/rendering libraries • Declarative UI • Cross Platform ◦ Windows, Mac OS, Linux, Web Browsers ▪ Currently, it supports only desktop OS and browsers. Main Features of Castella
Python using Python from scratch. ◦ Except for dependent window management/rendering libraries • Declarative UI • Cross Platform ◦ Windows, Mac OS, Linux, Web Browsers ▪ Currently, it supports only desktop OS and browsers. Main Features of Castella
except for dependent window management and rendering libraries. • For Desktop App: (GLFW or SDL2) and Skia • For Web Browser: CanvasKit and PyScript/PyOdide GLFW Castella Desktop OS Skia SDL2 Castella Desktop OS Skia PyScri pt Castella Web Browser Canva sKit ※ It’s important for Castella that there are nice bindings for each native libraries in Python. E.g., skia-python.
each widget module is highly portable because it relies only on the Painter and Frame Protocols, which define the I/F common to each backend. If you add a backend that implements the common I/F for a certain platform, Core and each widget should work correctly. GLFW Castella Desktop OS Skia SDL2 Castella Desktop OS Skia PyScri pt Castella Web Browser Canvas Kit ※ It’s important for Castella that there are nice bindings for each native libraries in Python. E.g., skia-python. Painter Frame Painter Frame Painter Frame
wrapper. It’s made for Python using Python from scratch. ◦ Except for dependent window management/rendering libraries • Declarative UI • Cross Platform ◦ Windows, Mac OS, Linux, Web Browsers ▪ Currently, it supports only desktop OS and browsers.
Python using Python from scratch. ◦ Except for dependent window management/rendering libraries • Declarative UI • Cross Platform ◦ Windows, Mac OS, Linux, Web Browsers ▪ Currently, it supports only desktop OS and browsers. Main Features of Castella
Child Column( Text(“First"), Text(“Second"), Text(“Third"), ) Row( Text(“First"), Text(“Second"), Text(“Third"), ) Box(Text(“Child")) Column aligns child elements vertically. Row aligns child elements horizontally. Box takes only one child elements and if the child size is larger than the box, it provides scrollbars automatically.
height. There are three types of policies that can be specified • Expanding: maximum size that will fit in the parent widget • Fixed: specified size • Content: size according to the content (e.g., for text, according to the size of that text) The default policy is different depending on the kind of widget, but it is basically “Expanding” for both width and height.
example on the left without omission Each child element (widgets)’s default size policy is basically Expanding. In this example, the three child elements share their width equally. The height of each child element is the height of the row. First Second Third Row( Text(“First"), Text(“Second"), Text(“Third"), ) Row( Text(“First").WidthPolicy(SizePolicy.Expanding) .HeightPolicy(SizePolicy.Expanding), Text(“Second").WidthPolicy(SizePolicy.Expanding) .HeightPolicy(SizePolicy.Expanding), Text(“Third").WidthPolicy(SizePolicy.Expanding) .HeightPolicy(SizePolicy.Expanding), )
percentage of the overall size. In this example, the width of the entire row is divided in the ratio 1:2:1. First Second Third Row( Text(“First").flex(1), Text(“Second").flex(2), Text(“Third").flex(1), ) Mix with Other Policies Of course, you can mix multiple size policies for the children of a single parent widget. In this example, the first element is displayed at the specified fixed size. The remaining two child elements share the remaining width in 2:1 ratio. First Second Third Row( Text(“First").fixed(300, 100), Text(“Second").flex(2), Text(“Third").flex(1), )
to create a custom widget. It is defined as a subclass of Component or a subclass of Stateful Component. (You can also define custom widgets without combining existing widgets, but that's another story.) • Component ◦ A component that doesn’t have the state of the component as a whole • Stateful Component ◦ A component that has state as the component
momentum. • In Declarative UI, view and state are defined declaratively, and the view switches automatically when the state changes. Also, the view can be defined structurally. • There are many good Cross Platform UI Frameworks available in Python. However, there doesn't seem to be a pure python one that allows you to write declarative UI in Python. • I tried making Castella. If you like, please try using it to create something. I hope this presentation has been of some use to you all. Summary