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

Author: Ankitha VP
September 11, 2024
Getting started with “Pythonista” : Building iOS apps in python

Imagine developing a Python iOS app that is entirely functional and combines strength and simplicity right on your iPhone or iPad." Pythonista promises to be a special tool that helps you build iOS apps with Python easily. 

Pythonista serves as a bridge between the fields of iOS app development and Python programming, not merely a stepping stone. This platform is quickly taking the lead among developers wishing to start working with Python iOS development. It is because of its expanding community and abundance of Pythonista examples and scripts .

Did you know that 257 billion mobile apps were downloaded globally in 2023, according to Statista? The need for adaptable, user-friendly development tools like Pythonista is greater than ever as this number keeps rising. This changed the trend of building iOS apps with Python.

This blog will walk you through the fundamentals so that your team may more easily utilize Pythonista's potential and Python iOS app development. Whether you're looking to learn how to make an iPhone app with Python or are looking for the best Pythonista UI instruction, this blog will be beneficial.

Features of Pythonista Scripts

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 for python iOS app development. The 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.

Get your queries answered fromour experts

Step-by-Step Guide: How to Make an iPhone App with Python

Setting up your Pythonista environment is the first step before you begin Python iOS development. Using the Python programming language on your iOS device, Pythonista is an app that lets you create and execute scripts on your iPhone or iPad. To begin with:

Pythonista can be downloaded from the App Store. Make sure you are using the most recent version to ensure the best performance.

When Pythonista is opened after installation, you'll see a straightforward, intuitive interface ideal for working on the go.

Get to know the user interface. Your Pythonista scripts will be written in the editor, and you may test and debug your code on the console.

Now that your environment is configured, you can start investigating the possibilities of using Python to create iOS apps.

Pythonista UI Tutorial

Now that Pythonista is operational, it's time to create your first Python iOS app. This tutorial will teach you how to create a basic application that shows a message on the screen.

  • Launch a New Script: Start Pythonista, then write a new script. Give it a meaningful name, such as "MyFirstApp."

  • Bring Required Modules: To use this basic application, import the pre-installed UI module from Pythonista. Apply the code below:

                         import ui

  • Design Basic UI: Establish the user interface by generating a label for text display. Here's a simple illustration:

def create_view():

v = ui.View()

label = ui.Label(frame=(0, 0, 200, 50))

label.text = "Hello, Pythonista!"

v.add_subview(label)

return v

view = create_view()

view.present('sheet')

  • Run Script: To run your script, use the play button. Your message should appear in a little window that pops up.

This easy activity demonstrates how quickly you can construct working Pythonista examples and introduces you to the fundamentals of Pythonista UI development.

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.

Ready to build your first iOS app with Python

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.

Transform your iOS app development journey

Final Thoughts

To become proficient in iOS app development, you no longer need to dive deeply into complicated languages like Swift or Objective-C. Pythonista makes it easier and more accessible to use Python to create iOS apps. 

Pythonista provides a flexible framework that makes creating useful and intuitive Python iOS apps easier, regardless of experience level in mobile app development.

By using Pythonista, you can use both Python's ease of use and a robust toolkit that will hasten the process of developing apps. Platforms like Pythonista enable developers to create without the high learning curve typically associated with iOS programming while mobile technology advances.

Thus, Toobler can be your best partner who can assist you to build an iPhone app using Python. 

Connect with us for information and insights on how to make an iPhone app with Python.