Pythonista Writing Python for iOS

Apple's iPad is a well-known and ad-free device. But very often I want to use the full power of this device not only for games and entertainment, but for serious work. For example, for writing programs. Despite the 4-year history of the development of this gadget and the presence of different models of convenient environments for programming for iOS, there are very few. (Say at once, in order to avoid further confusion: programming on iOS - means writing code and run the program on your iPad or iPhone, and programming for iOS - writing applications that can be laid out in the App Store.)

I recently came across an excellent program from a Pythonista , which allows you to write on iOS for iOS

Short description

As the creators of this program write:
Pythonista brings the Zen of Python to your iPad or iPhone.
And indeed it is. The program is the best compiler for Python.

In my opinion, 3 things make this application the best:

  • No internet connection is required to run the program. iPad really becomes a workstation;
  • There are tooltips and built-in documentation (again, without access to the Internet);
  • And, of course, the most important thing is the ability to export to Xcode.


The environment is focused on Python 2.7. But there are some chips from the 3rd branch. For example, the following code will work:

print "Hello, world"
and code
print ("Hello, world")

In addition to standard libraries, there are several libraries for direct development for iOS. I will dwell on one. It is called ui and is responsible for the GUI.

Let's look at some examples of working with this library. It is very interesting that in Pythonista the graphical interface can be set programmatically, or it can be natively:

import ui
def button_tapped(sender):
    sender.title = 'Hello'
view = ui.View()                                      # [1] = 'Demo'                                    # [2]
view.background_color = 'white'                       # [3]
button = ui.Button(title='Tap me!')                   # [4] = (view.width * 0.5, view.height * 0.5) # [5]
button.action = button_tapped                         # [6]
view.add_subview(button)                              # [7]
view.present('sheet')                                 # [8]

This is the first example of working with the ui library. Let's analyze the program line by line:

1) First, create a View object;
2) Then we set the name of this object, it will be displayed in its title;
3) Set the background color of the object - white, you can set a word, or you can use RGB;
4) Create a button with the inscription "Tap me!";
5) We place the button on the object;
6) Set the function that will be executed when the button is pressed. (In this case, the inscription on the button will change);
7) We clarify that the "button" is the heir to the "view";
8) Finally, we call the view.present () method to display the object on the iOS device screen.

Here's what will happen on the iPad:


But the same thing can be done natively:

1) Let's create a script with UI:


2) Having opened the UI, press the “+” button and select button:


3) Stretch the button and place it in the center of the screen:


4) Open the button attributes and set the function that works when it is pressed:


4) Let's go in the script editor and write a function:

def button_tapped(sender):
    sender.title = 'Hello'

Let's say which UI to bind this script to:

ui.load_view('My UI').present('sheet')

5) Run the program:


In conclusion

In conclusion, I want to say that the review I have presented is far from complete and does not reveal all the functions of this program. A lot of examples, an excellent description of libraries - all this will allow you to quickly figure out all the properties of this application.

I recommend visiting the site of the creators of Pythonista. It has documentation , unfortunately, only in English.

UPD: Read my article on this program and iOS automation in the February issue of the magazine] [aker

Also popular now: