Getting started with PythonHere#

Load the extension, and connect to the remote instance#

%load_ext pythonhere
%connect-there

Execute some code on the remote#

%%there
from kivy import platform
print("Hello from", platform)
Hello from android

Use Kivy widgets#

%%there
from kivy.uix.label import Label

# Kivy's root widget is available via the `root` variable.
root.clear_widgets()  # Remove all current childrens

# And add the new one
widget = Label(text="Kivy", font_size="50sp")
root.add_widget(widget)
%there screenshot -w 400
../_images/efa4f8c322ffb93bf322f6088ce50682f426df576524e59ede6709e7227eac96.png

Objects introspection#

%%there
print(root.children)
print(widget.color)
[<kivy.uix.label.Label object at 0x99d44db8>]
[1, 1, 1, 1]

Modify widget properties#

%%there
widget.color = [1, .5, 0, 1]
widget.text += " Rocks!"
%there screenshot -w 400
../_images/5eabc0397d8a9645177c5b2aa2c8ed00ce228701843fadfd9711b48c25707dc3.png

Make things dynamic with Clock#

%%there
from kivy.clock import Clock

def clock_callback(delta_time):
    widget.color[1] = (widget.color[1] + .1) % 1

clock = Clock.schedule_interval(clock_callback, 0.2)
%there -d .5 screenshot -w 400
../_images/99d5bc535b80389c68b4a04d390486914a2b6bed069041f823e64816cec9b092.png
%%there
Clock.unschedule(clock)

Use platform specific features with Plyer#

%%there
from plyer import tts
tts.speak("yo" * 10)

Declare interface with the KV language#

%%there kv
Button:
    text: "Click me"
    font_size: 10
    on_release: self.font_size += 1
%there -d 5 screenshot -w 400
../_images/dab606187bd2c26a2fbd36e1275e960f0ccf2bd9ad904021af123e265356aba7.png

Combine Python with KV#

%%there
from plyer import vibrator
from kivy.uix.button import Button

class VibroClone(Button):

    def on_release(self):
        self.root.add_widget(
            VibroClone(root=self.root)
        )
        vibrator.vibrate(0.05 * int(self.text))

root.clear_widgets()
%%there kv
#:import get_random_color kivy.utils.get_random_color
#:import random random

<VibroClone>:
    text: str(random.randint(1, 9))
    color: get_random_color()
    background_color: get_random_color()
    size_hint: 1, 1

GridLayout:
    cols: 10
    size_hint: 1, 1
    VibroClone:
        root: root
%there -d 4 screenshot -w 400
../_images/1dc3d2b74d14292a7ba3147717e6bda69360ae1e612806b14bc7efd357d534a9.png