Programming Mobile Apps with
Python
Andreas Schreiber <[Link]@[Link]>
EuroPython 2012, Florence, Italy (July 3, 2012)
[Link]
Medando – Mobile Health Apps
EuroPython 2012, Florence, 03.07.2012 Slide 2 [Link]
My Blood Pressure
EuroPython 2012, Florence, 03.07.2012 Slide 3 [Link]
Overview
• Apps and Statistics
• Apps, Hardware, Markets
• Early Mobile Development with Python
• Nokia, Windows Mobile / CE
• Current Mobile Development with Python
• Android, iOS, Windows Phone
• Everything else…
• Other systems, summary, conclusions
EuroPython 2012, Florence, 03.07.2012 Slide 4 [Link]
Mobile Apps
What is a mobile app?
• A software designed to run on smartphones, tablet computers, and other
mobile devices
• Limited memory, small screens, touch UI, battery efficient
• Runs on mobile operating systems
• Available through application distribution platforms
• Apps are either free or paid
• “App” was Word of the Year in 2010 (USA)
EuroPython 2012, Florence, 03.07.2012 Slide 5 [Link]
Smartphones
What is a smartphone?
• Mobile phone based on a mobile
operating platform
• Combination of
• PDA functions, camera, media player, Web browser, …
• Equipped with
• Touch screen, high-speed networking (Wi-Fi, mobile
broadband), GPS, NFC, acceleration sensors, …
EuroPython 2012, Florence, 03.07.2012 Slide 6 [Link]
Tablet Computers
What is a Tablet?
• A mobile computer that is
larger than a smartphone
• Built as touch screen device
• Virtual keyboard, sometimes
stylus or digital pen
• Mostly based on x86 or ARM
architectures
EuroPython 2012, Florence, 03.07.2012 Slide 7 [Link]
Mobile Operating Systems
Popular mobile operating systems
• Android
• Apple iOS
• Windows Phone / Mobile / CE
• RIM BlackBerry
• Nokia OS / Symbian
• Samsung Bada
• …
EuroPython 2012, Florence, 03.07.2012 Slide 8 [Link]
Market Share: Smartphone Sales
Source: [Link]
EuroPython 2012, Florence, 03.07.2012 Slide 9 [Link]
App Distribution
• Application Distribution Platforms
• Google Play
• Apple App Store
• Windows Phone Marketplace
(and Windows Marketplace for Mobile)
• BlackBerry App World
• Nokia Ovi Store
• Many more
• Cross-platform, manufacturer-specific,
carrier-specific, …
EuroPython 2012, Florence, 03.07.2012 Slide 10 [Link]
Apps in the iOS App Store
Source: [Link]
EuroPython 2012, Florence, 03.07.2012 Slide 11 [Link]
Apps in the Android Market / Google Play
Google Play Apps
700.000
600.000
500.000
400.000
300.000
200.000
100.000
EuroPython 2012, Florence, 03.07.2012 Slide 12 [Link]
Apps in Google Play
Google Play Downloads
[Link]
[Link]
[Link]
[Link]
[Link]
EuroPython 2012, Florence, 03.07.2012 Slide 13 [Link]
Apps in the Windows Phone Marketplace
Source: [Link]
EuroPython 2012, Florence, 03.07.2012 Slide 14 [Link]
So Many Apps…
… but almost none of them
are developed in Python!
EuroPython 2012, Florence, 03.07.2012 Slide 15 [Link]
Early Mobile Development with Python
• Symbian
• Window Mobile / CE
EuroPython 2012, Florence, 03.07.2012 Slide 16 [Link]
Symbian
Symbian OS
• Operating system for smartphones and PDAs
• Micro kernel, 32bit, single user
• GUI-centric application framework
S60
A schematic diagram of the S60 platform architecture.
• User interface for smartphones
Programming for Symbian
• C++, Java, Web Widgets
• (Flash Lite), (Python)
EuroPython 2012, Florence, 03.07.2012 Slide 17 [Link]
Python for S60 (PyS60)
• Python port for S60 platform
• Developed by Nokia, but development has stopped
• Open-Source-License (Apache Version 2)
• Stable Release
• Last release: 1.4.5 (27.01.2009)
• Available on SourceForge
[Link]
EuroPython 2012, Florence, 03.07.2012 Slide 18 [Link]
“Hello World”
import appuifw
[Link] = u„Hello World"
[Link](u"Hello World!", 'info')
EuroPython 2012, Florence, 03.07.2012 Slide 19 [Link]
Starting a Python Interpreter
EuroPython 2012, Florence, 03.07.2012 Slide 20 [Link]
Running Scripts
EuroPython 2012, Florence, 03.07.2012 Slide 21 [Link]
Interactive Console
EuroPython 2012, Florence, 03.07.2012 Slide 22 [Link]
Source Code Template
import appuifw
1. Importing modules import e32
2. Setting screen size [Link] = 'normal'
3. Applications menu def item1():
print "item1 was selected.”
4. Set. exit key handler [Link] = [(u"item 1", item1)]
(Callback function) def quit():
[Link].set_exit()
5. Setting app title
app.exit_key_handler=quit
6. Application body
[Link] = u'Simple Application'
7. Active Objects
app_lock = e32.Ao_lock()
8. Main Loop # starts scheduler -> event processing
app_lock.wait()
Complete template: [Link]
EuroPython 2012, Florence, 03.07.2012 Slide 23 [Link]
Example „Location Based Service“
Send SMS to someone if certain cell is near (using GSM Cell ID)
import location
HOME_CELL_ID = u"98521779" # my home in Cologne, Germany
WIFE = u"+49173247****“ # cell phone number of my wife
entries = [u"can’t wait to see you!",
u"I’m hungry.",
u"I’m in a bad mood."]
listbox = [Link](entries, shout)
home = 0
while home == 0:
country, provider, lac, cell = location.gsm_location()
if (cell== HOME_CELL_ID):
home = 1
message = u"Will be at home in a moment and %s" % mood
messaging.sms_send(WIFE, message)
EuroPython 2012, Florence, 03.07.2012 Slide 24 [Link]
Example „OpenGL“
[Link]
EuroPython 2012, Florence, 03.07.2012 Slide 25 [Link]
Windows Mobile / CE
Windows Mobile
• Operating system for smartphones
• Based on Windows CE kernel
• Implemented in C
Programming for Windows Mobile
• Visual C++ (native code)
• .NET framework
• Tcl-Tk (with eTcl)
• Python with PythonCE
EuroPython 2012, Florence, 03.07.2012 Slide 26 [Link]
Python CE
• Python port for Windows CE
• Python CE: [Link]
• Outdated since 2007
# Send a quick popup message to the user
import win32sys
[Link](0, "My Message", "My Title", 1)
[Link]()
# Launch another CE application
import win32sh
[Link](0, 0, "", "\\Windows\\[Link]",
"", "\\Windows", 1)
EuroPython 2012, Florence, 03.07.2012 Slide 27 [Link]
Current Mobile Development with Python
• Android
• iOS
• Windows Phone
EuroPython 2012, Florence, 03.07.2012 Slide 28 [Link]
Android
Android
• Linux-based operating system for mobile devices
• Developed by the Open Handset Alliance, led by Google
• Open Source, Apache License
• Kernel based on Linux-kernel
• Libraries and APIs written in C
• Application Framework in Java
• Java VM Dalvik with JIT
• Hardware platform: ARM
EuroPython 2012, Florence, 03.07.2012 Slide 29 [Link]
Programming Android
• Apps are usually developed in Java
• Software development tools
• Android Software Development Kit
• Native Development Kit
• App Inventor for Android
• Simple project and Basic4android
• some others
• Python!?
EuroPython 2012, Florence, 03.07.2012 Slide 30 [Link]
Python for Android
Several options
• Scripting Layer for Android (SL4A)
• Python for Android (Py4A)
• Kivy
• PySide for Android
• PyGame
EuroPython 2012, Florence, 03.07.2012 Slide 31 [Link]
Scripting Layer for Android (SL4A)
• Executes scripts and interactive interpreters on Android
• Access to many APIs
• Support for
• Python
• Perl
• Ruby
• Lua
• BeanShell
• JavaScript Source: Practical Android Projects by Lucas Jordan and Pieter Greyling
• Tcl
• Available on Google Code:
[Link]
EuroPython 2012, Florence, 03.07.2012 Slide 32 [Link]
Scripting Layer for Android (SL4A)
• Support for some UI elements: Dialogs, Toasts, Progressbar, …
• Interactive console
Source: Practical Android Projects by Lucas Jordan and Pieter Greyling
EuroPython 2012, Florence, 03.07.2012 Slide 33 [Link]
Scripting Layer for Android (SL4A)
• Scripts
• Sharing via barcodes or APKs
• Hello Word:
import android
droid = [Link]()
[Link]('Hello, Android!')
EuroPython 2012, Florence, 03.07.2012 Slide 34 [Link]
Python for Android (Py4A)
• Python interpreter for Android
• CPython, cross-compiled for Android
• Used in SL4A
• Access to many Android APIs
• Native UI support not complete
• Website: [Link]
EuroPython 2012, Florence, 03.07.2012 Slide 35 [Link]
Building an App
• To build a stand-alone app, the Python for Android interpreter must be
included in an APK
• Project: [Link]
• Template files
• Build files
• Basically requires to change Java code to add your own script
public static final String
PYTHON_MAIN_SCRIPT_NAME = "[Link]";
public static final String
PYTHON_PROJECT_ZIP_NAME = "my_python_project.zip";
File: [Link]
EuroPython 2012, Florence, 03.07.2012 Slide 36 [Link]
Kivy
• Cross platform framework for innovative user interfaces
• Multi-touch
• Runs on Android, iOS, Linux, Windows, Mac OSX
• Graphics engines uses OpenGL ES2
• Open Source
• Website: [Link]
EuroPython 2012, Florence, 03.07.2012 Slide 37 [Link]
Kivy Hello World
from [Link] import App
from [Link] import Button
class TestApp(App):
def build(self):
return Button(text='Hello World')
TestApp().run()
Source: Kivy Website [Link]
EuroPython 2012, Florence, 03.07.2012 Slide 38 [Link]
See Kivy Poster Outside…
Poster by Mathieu Virbel
EuroPython 2012, Florence, 03.07.2012 Slide 39 [Link]
PySide for Android
• For writing GUI applications for Android using Python
• Based on
• Python for Android (Py4A)
• Qt for Android (Necessitas)
• Python bindings for Qt (PySide)
• Website: [Link]
EuroPython 2012, Florence, 03.07.2012 Slide 40 [Link]
PySide for Android Example
import sys
import traceback
def main():
from [Link] import *
from [Link] import *
app = QApplication([Link])
w = QLabel('Hello World!')
[Link]()
app.exec_()
if __name__=="__main__":
main()
EuroPython 2012, Florence, 03.07.2012 Slide 41 [Link]
Pygame Subset for Android
• Subset of Pygame for the Android platform
• Good for developing simple games
• Website: [Link]
EuroPython 2012, Florence, 03.07.2012 Slide 42 [Link]
iOS
EuroPython 2012, Florence, 03.07.2012 Slide 43 [Link]
Apple iOS
iOS
• Mobile operating system by Apple
• Multi-touch user interface
• OS layer: Core OS layer, Core Services,
Media layer, Cocoa Touch layer
Programming iOS
• Apps are written in Objective-C
• Software Development Kit and IDE Xcode
• Python on iOS almost not possible
EuroPython 2012, Florence, 03.07.2012 Slide 44 [Link]
Python for iOS – A Python IDE for the iOS
[Link]
EuroPython 2012, Florence, 03.07.2012 Slide 45 [Link]
Python for iOS – On iPhone
EuroPython 2012, Florence, 03.07.2012 Slide 46 [Link]
Python for iOS – On iPad
EuroPython 2012, Florence, 03.07.2012 Slide 47 [Link]
Python for iOS – On iPad
EuroPython 2012, Florence, 03.07.2012 Slide 48 [Link]
Python for iOS – On iPad
EuroPython 2012, Florence, 03.07.2012 Slide 49 [Link]
Kivy
• You can publish Kivy Apps
in the Apple App Store
• No dynamic linking
• First App: Deflectouch
EuroPython 2012, Florence, 03.07.2012 Slide 50 [Link]
Windows Phone
Windows Phone
• Mobile operating system by Microsoft
• Successor to Windows Mobile
Programming for Windows Phone
• Apps must be based on XNA or Silverlight
• Windows Phone Developer Tools
EuroPython 2012, Florence, 03.07.2012 Slide 51 [Link]
IronPython
IronPython
• Python implementation for the .NET platform
• Since version 2.7.2 (March 2012):
• Preliminary support for Windows Phone 7.5
• No documentation yet
• [Link]
EuroPython 2012, Florence, 03.07.2012 Slide 52 [Link]
Everything else…
EuroPython 2012, Florence, 03.07.2012 Slide 53 [Link]
Other Mobile Devices
Many other devices with Apps
• Livescribe
• Amazon Kindle
• No Python support
• Maybe support via Jython
EuroPython 2012, Florence, 03.07.2012 Slide 54 [Link]
Summary
Symbian Android iOS Windows
Phone
Python Yes Yes, with Via Kivy Not now
support restrictions
EuroPython 2012, Florence, 03.07.2012 Slide 55 [Link]
Comparison Matrix by Thomas Perl (@thp4)
Source: [Link]
EuroPython 2012, Florence, 03.07.2012 Slide 56 [Link]
Questions?
@onyame
Conclusions
• Python on mobile phone not ready for production
development of apps
• Some solutions for scripting and interactive interpretation
EuroPython 2012, Florence, 03.07.2012 Slide 57 [Link]