IOS application development with Python (kivy) that even monkeys can do

Introduction

Python is active in the fields of machine learning and data science, but its GUI is also excellent, and the kivy used this time can be built to create applications that work on desktop, Android, and iOS.

This time, I will give a brief explanation for those who have never made an application but know Python to some extent. Also as a memorandum for myself. ..

What is kivy

One of the Python libraries that makes it easy to create a GUI (graphical user interface: not a black console screen).

Japanese Reference also exists, and it is a library that is still under development.

As for how to use it, many other people have written easy-to-understand and polite articles, so I will omit it here. This time, we will proceed on the assumption that it will be an application.

Things necessary

kivy-ios I will clone it from the GitHub here page.

__ Support for iOS 13 __ From iOS 13, there seems to be a big change in the Objective-C language, and for a while this kivy-ios stopped working. As of December 28, 2019, the pull request of the corrected part has been merged and is ready for use.

Objective-C iOS 13.0 breaking changes #53

Start build

Start building the app immediately. kivy-ios First, install the dependent packages to run kivy-ios.

xcode-select --install
brew install autoconf automake libtool pkg-config
brew link libtool
pip3 install cython==0.28.1

In the cloned directory,

pip3 install -r requirements.txt

To execute. Now you are ready to use kivy-ios.

After this, we will embed the python program in the app, so we need to build python itself for the app. The command is:

python3 toolchain.py build python3 kivy

This process took about an hour in my environment (Mac book Pro 2016). Sweat

Creating an application

This time it's a tutorial, so let's make a stopwatch. As for the contents, create one button and output the difference between the time when the button is pressed the first time datetime.datetime.now () and the time when the button is pressed the second time.

main.py


from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import StringProperty
from datetime import datetime


class MainWindow(Widget):
    button_text = StringProperty("Push Button!!")
    timer_text = StringProperty("START")

    def __init__(self, **kwargs):
        super(MainWindow, self).__init__(**kwargs)
        self.timer_check = False
        self.timer_before = 0
    
    def push_button(self):
        if not self.timer_check:
            self.timer_before = datetime.now()
            self.timer_check = True
            self.button_text = "STOP"
            self.timer_text = "Running..."
        else:
            self.timer_check = False
            self.button_text = "START"
            self.timer_text = str(datetime.now() - self.timer_before)


class StopWatchApp(App):
    def __init__(self, **kwargs):
        super(StopWatchApp, self).__init__(**kwargs)

    def build(self):
        return MainWindow()


if __name__ == '__main__':
    StopWatchApp().run()

stopwatch.kv


MainWindow:

<MainWindow>:
    BoxLayout:
        orientation: "vertical"
        size: root.size
        Label:
            text: root.timer_text
        Button:
            text: root.button_text
            on_press: root.push_button()

スクリーンショット 2019-12-28 14.03.35.png スクリーンショット 2019-12-28 14.03.58.png

It looks really cheap, but I was able to do it. At this time, save the Python script as main.py. Put the created py file and kv file in an appropriate directory. (This time I saved it in a directory called stopwatch.)

Apple profile creation

If you're enrolled in the Apple Developer Program, that's okay, but you'll have to pay an annual fee to actually publish your app to the store. However, even if it is free, you can only build the actual machine, so please set it referring to the following article.

[Xcode] How much can you do with a free real machine build

__ Free real machine build __ The profile is valid for __7 days __. After this, you will not be able to start from the iPhone, so you need to transfer to the actual device again.

XCode project creation

Type the command according to the following command syntax.

python3 toolchain.py create <title> <app_directory>

This time, I put a directory called stopwatch in the directory where kivy-ios-master is located, so

python3 kivy-ios-master/toolchain.py create StopWatch ./stopwatch

It will be. Then, a directory called stopwatch-ios will be created in the same directory, so open the XCode project stopwatch.xcodeproj inside.

スクリーンショット 2019-12-28 14.13.23.png

Select the provisioning profile you set earlier from the project settings, select the connected iPhone from the terminal selection screen on the upper left, and press the triangle button to build. At this time, if you have a passcode on your iPhone, you need to cancel it and leave it on the home screen.

Also, the first build will start automatically, but it won't start because the profile is not official. You need to authenticate from the iPhone's Settings> General> Device Administrator.

It seems that the build was successful. I'm happy.

ファイル名 ファイル名

I'm sorry that the image quality is so bad that I'm dead because I took it with a webcam.

Finally

I never thought it would be so easy to create an app. What kind of head structure does a person who comes up with such a library have? Lol

I want to make various things again.

Recommended Posts

IOS application development with Python (kivy) that even monkeys can do
[Mac OS] Use Kivy with PyCharm! [Python application development]
An introduction to Python that even monkeys can understand (Part 3)
An introduction to Python that even monkeys can understand (Part 1)
Python-PageRank calculation & social graph visualization that even monkeys can do
An introduction to Python that even monkeys can understand (Part 2)
Application development with Docker + Python + Flask
Python | What you can do with Python
Othello app (iOS app) made with Python (Kivy)
Automatic browser operation that even beginners can do
Python knowledge notes that can be used with AtCoder
Build a machine learning application development environment with Python
Do Houdini with Python3! !! !!
Articles that enable system development with Django (Python) _Introduction
"First time Kikaigakushu" that even university students with no knowledge can do --AWS SageMaker
What you can do with the Python standard library statistics
Do Django with CodeStar (Python3.6.8, Django2.2.9)
Python + Kivy development on Windows
Web application development with Flask
Web application with Python + Flask ② ③
Web application with Python + Flask ④
[Python] Make a graph that can be moved around with Plotly
If you know Python, you can make a web application with Django
8 services that even beginners can learn Python (from beginners to advanced users)
Consider what you can do with Python from the Qiita article
I made a shuffle that can be reset (reverted) with Python
Web application development memo in python
Monitor Python application performance with Dynatrace ♪
To do tail recursion with Python2
Othello game development made with Python
What to do with PYTHON release?
Prepare Python development environment with Atom
Python application: Data cleansing # 2: Data cleansing with DataFrame
Basic summary of scraping with Requests that beginners can absolutely understand [Python]
Mathematical optimization that can be used for free work with Python + PuLP
I tried using "Streamlit" which can do the Web only with Python
"Manim" that can draw animation of mathematical formulas and graphs with Python
You can do it with Python! Structural analysis of two-dimensional colloidal crystals
I made a module PyNanaco that can charge nanaco credit with python