codolis header baner 2
codolis blog header
Blog

Android UI Development - Jetpack Compose

Tamara Glisic PyScript

Since it was announced by Google in 2021, Jetpack Compose has quickly emerged as a widely used toolkit in Android UI development of native applications. From an engineer’s view, this simplifies UI creation and improves efficiency by reducing the code amount.

In Jetpack Compose, each component that needs to be rendered on the screen can be defined as a Kotlin Unit function marked with the @Composable annotation. We call those functions composables. Every time the value changes, the UI will be updated to reflect the updated values. This is what we call recomposition. You may only call composable functions from other composable functions. Because of this, activities that use Composable to render their layouts will look like this:

  • Designed for readability (“Fits in your head”)
  • Easy to learn
  • Rich library ecosystem
  • Does many things with modest effort
  • Very popular

PyScript is a framework invented to provide users with an opportunity to create Python applications directly in the browser. We can write the code directly in an HTML interface thanks to Pyodide, Wasm, and other modern web technologies. These are the six basic components:

  1. Python in the browser – allows drop-in content, hosting applications without configuration servers
  2. Python ecosystem – usage of various popular scientific tools such as Numpy, Pandas, Sciki-learn, etc.
  3. Python with JavaScript – BI-directional communication between Python and JavaScript objects and namespaces
  4. Environment management – we can define which packages and files to include in running page code
  5. Visual application development – ease to use of UI components, such as Buttons, Containers, Text boxes, etc.
  6. Flexible framework – can be used for creating and sharing new components that can be included or excluded directly in Python

1. <py-config>

With Py-config we can configure metadata and define dependencies needed for PyScript applications. Configuration is possible in TOML and JSON formats and we should place it within the <body> tag.

Attributes:

PyScript elements

2. <py-repl> 

Read Eval Print Loop is an interactive Python framework that runs multiple code lines. If we print any of the REPL elements – the output will be shown at the py-terminal.

Attributes:

PyScript Elements 3

3. <py-script>

Allows running Python scripts whether in code or in a specific folder imported by src attributes.

Attributes:

PyScript Elements 5

Buttons in Compose

<py-terminal>

This is one of the core plug-ins, and it’s active by default. It is used for printing across stdout and stderr and for listing export data on the py-terminal page.

PyScript Elements 7

PyScript or JavaScript?

By launching the PyScript project, now we have a choice to use the language we prefer. The best thing is that we don’t even have to decide about only one of them because PyScript allows us to use JavaScript libraries. This makes JavaScript and PyScript compatible. The only weakness may be the download duration, so in some cases, PyScript would not be the best choice.

If you ask if PyScript tends to replace JavaScript, the answer would be no. This framework should give extra value and increase flexibility for all Python users. In cases where one would never use HTML, CSS, and JavaScript, now have the motivation to combine benefits from both languages.

This blog post was written by our software engineer Tamara Glišić.