TECHNICAL

Getting started with “Pythonista” : Building iOS apps in python

ByDona Feble

Ole Zorn introduced Pythonista a code editor for developing iOS apps in python programming language in 2012 and is available at appstore for $6.99 and recently I happened to be fortunate enough to be part of an iOS app development project using python for one of ourclients. After initial phase of the development i thought it would a be a good time to give some real heads-up to the python development community using pythonista . So here is a brief intro into those wonderful features “Pythonista” offers for iOS App Development in Python

1) Pythonista comes with some very cool development friendly features like syntax highlighting and code auto-completion. For highlighting syntax, you have 6 color themes to choose from in the Settings, including two Solarized options (dark and light); in the Settings, you can also change the editor font (Adobe’s new Source Code Pro is available), font size, line spacing, and tab width.

2) The editor offers highly convenient way to auto-complete your code: suggestions appear directly above the keyboard and they’re colored according to your color scheme. When typing, you can tap on the “auto-complete bubble” to let Pythonista complete your code; code completion is also smart in that only functions/class names/etc related to a module you’ve imported will be suggested.

3) Code completion can be deactivated in the Settings alongside other options enabled by default.

4) You can set Pythonista to highlight matching (), which is a nice feature to have as it provides a subtle hint to confirm you’ve matched parentheses in the right way.

5) Auto-pairing: characters like parentheses, square brackets, and single/double quotes will be matched with closing characters automatically. If you’re coming from nvALT or Sublime Text 2, you should be familiar with this option. For instance, if you begin typing ( a closing ) character will be immediately put on the right, with the cursor in the middle ready to type. Or, if you select text and hit ( the entire text will be wrapped inside ( ) ).

6) There’s a search field in the code editor; you can also navigate your code’s structure from a document browser available in the top title bar.

7) Pythonista lets you organize scripts in the Script Library. There are two views to choose from: a “snippet view”, which lets you see scripts as thumbnails with a preview of the first lines of code, and a more traditional list view. Both views can be sorted by name or modified date; you can add scripts with a + button at the top and delete existing ones by tapping on the Edit button.

8) A downside of Pythonista is that the Script Library doesn’t offer further organizational features to better sort your scripts. You can’t create folders, and it gets pretty confusing in snippet view.

9) Due to restrictions imposed by Apple, Pythonista can’t have a sync feature to import executable code from external sources like Dropbox. You can export, but you can’t import – not even from iTunes.

10) It Works great considering the limitations Apple puts on developers. It includes standard python libraries, also Dropbox, graphics and clipboard libraries, specifically made for iOS.

11) You can program games using accelerometers and touch.

12) It works great for automating tedious tasks like formatting text or images for blogs etc.

13) You can also add home screen shortcuts, making your scripts available directly from your home screen of your iOS device. All you have to do to create a Pythonista home screen shortcut is to go to a web page and enter the script name and optional arguments. After that, just add the page home-screen with Safari’s bookmarks button.

14) Pythonista supports the x-callback-url specification which makes it possible to send information to one app and get the results back and then continue process it or sending it to yet another app.

Extended Keyboard

There is a great extended keyboard with the most used keys you need when programming Python.You can move the cursor in the text by just sliding your finger from back and forth across the extended part of the keyboard, and the cursor moves accordingly.

Pythonista URL Schemes

Pythonista has an URL Scheme, so it can be called from other programs. This makes it perfect for apps on the iPhone like Launch Center Pro on iPhone that supports URL Schemes to start your Python scripts.

With more and more apps supporting handleOpenURL, the things you can do starts to look very handy indeed.

You can launch the app itself with pythonista://, but the good stuff lies in the parameters you can pass along with the URL. Firstly, you can open a specific script using something like pythonista MyScript – but even better, you can open and run a script by using pythonista://MyScript?action=run in the URL. To use this, a script with that exact name will have to be in your Library, and no other script will have to be running upon calling action=run.

The really good stuff is what you get by combining the URL scheme with command line arguments. As documented by Ole, you can pass one argument at a time by using &args= or multiple ones with &argv= in the URL. This is best explained with an example: because of the URL scheme, you can create JavaScript bookmarklets that launch Pythonista and pass command line arguments in the URL.

Libraries Included

The standard libraries are included for Python, but the developer of Pythonista has also created some handy libraries for things specific to the iPhone/iPad

canvas — Vector Graphics
clipboard — Copy and paste
console — Functions for working with the text output and keyboard input
editor — Functions for scripting Pythonista’s text editor
keychain — Secure Password Storage
scene — 2D Graphics and Animation
sound — Play simple sounds

Other handy libraries that are included are:

bs4 — BeautifulSoup 4
Dropbox
feedparser — Universal Feed Parser
Markdown
Python Imaging Library
Requests – HTTP for Humans

So there are plenty of libraries available, and more more are coming.

Inconveniences of Developing iOS apps using Pythonista

1) The major problem with the app is the way to import scripts into it. Most of the blame goes to Apple for not allowing importing of scripts from Dropbox

2) Apple wants to keep the iOS out of the troubles of malicious code execution, and that’s understandable, but the apps are already jailed, so they can’t access files outside its own folders.

3) Pythonista supports several modules of the standard Python library, but, due to limitations of iOS, it can’t support everything a desktop Python installation can.

4) You won’t be able to programmatically read files from the filesystem on iOS: there’s no “Desktop” or “Documents” folder to read from on iOS. For the same reason, you won’t be able to “save” files to specified locations on your local filesystem, as Pythonista can’t break open the sandbox and, say, process a file for GoodReader.

5) But there is one layer of data that is constantly shared across iOS apps: the system clipboard. Text and images we copy on iOS are stored in the clipboard(also known as “pasteboard”), and they are available at a system-wide level.

Latest stories
Call