Cross-platform development with Python and BeeWare


[ad_1]

If you want to develop for Android, you have to use Java. If you want to develop for iOS, you have to use Objective C. And if you want to develop for the web, you have to use JavaScript. Right?

These may be the preferred languages for these platforms, but at the end of the day, mobile phones and web browsers are computing platforms, and with a little work, you can use any language you want. With the BeeWare suite of libraries and bridges, you can use just Python. And, you can use the same code to deploy on all these platforms.

This article offers a preview of our upcoming PyCon US 2017 talk, Snek in the Browser, which is a deep dive into how the BeeWare project tackles using Python for front-end development using Batavia and Toga.

Why Python?

Because we don’t have to use HTML and JavaScript, we can start looking at the browser as a platform for which we can deliver applications, rather than a specific set of technologies you have to write code to suit. Once we’ve adopted that mindset, it frees us up to look at all sorts of new ways of constructing web apps.

Python is one of the easier languages to pick up, and it doesn’t have the learning curve of Java or Objective C. And recently, many scientific communities, including astrology and data science, have picked up Python as their go-to language. To be able to use a language they already know to create their own applications, such as mobile-based data recording systems, without having to learn entire new languages would be a great asset to all these scientists.

How does it work?

The only language that works natively in the browser is JavaScript. Sure, there are workarounds with Flash, Silverlight, etc., but they are prone to security and development issues alike. Batavia, however, uses JavaScript to run Python. How? By implementing the Python virtual machine in JavaScript. Python itself is just a language specification. CPython, on the other hand, is the Python implementation most people use, with PyPy being another. CPython once compiled generates those .pyc files that you might see in your filesystem. These are bytecode representation of a combination of around 100 different base-level operations. Implement a way for JavaScript to understand how these operations work, and you have an interpreter.

Given this, if we create a JavaScript-based application that can take Python bytecode and return the same results as the CPython implementation, we can run Python in the browser. In fact, you can do this in less than 500 lines of code, as Allison Kaptur explains in her article “500 Lines or Less, A Python Interpreter Written in Python“.

Emerging technologies such as ASM.js and WebAssembly make the prospect of in-browser language interpreters even more promising, given the performance improvements these projects provide.

Native rendering

To be able to render websites with the same code as for a mobile deployment, we need a universal wrapper that allows us to target these platforms. This is where Toga, an OS-native Graphical User Interface (GUI) toolkit, comes in. Toga abstracts out the interface options of different systems. If you want to create a Quit button, it will be in a different place on macOS than on Windows or Linux. It’s not just a wrapper around native system calls—it’s abstraction over the native layer, capturing the high-level user interaction use cases.

At the end of the day, a web page and a mobile screen are just bitmap devices rendering font, shapes, and lines on a screen. HTML has brought in a new way of thinking about the building blocks of graphic user interfaces, but by consolidating how we think about putting stuff on a screen, abstracting this to all platforms is made simpler.

Work in progress, but you can help!

The entire BeeWare suite, including Batavia and Toga, are still a work in progress. We have an open offer to mentor anyone who wants to get involved with the project. If you make a contribution to the project, be it code, documentation, writing a tutorial, or helping someone else contribute, you’ll earn yourself a shiny challenge coin.

We’ll be at PyCon US 2017 throughout the entire event, from the first tutorial day (our tutorial has, sadly, already sold out), all the way through the sprints. We’ll be in the Exhibit Hall in Booth 103 during the main conference, so drop in and say hi, and see just how shiny our coins—and code—are.

[ad_2]

Source link

,

Leave a Reply