Jupyter magic commands#
Commands are provided by the pythonhere extension
%load_ext pythonhere
%connect-there#
Connect to remote interpreter via SSH
Command takes single optional argument: location of connection config.
If argument is not provided, values are loaded from the there.env file.
Config values could be overridden by environment variables with same names.
import os
os.environ["THERE_PORT"] = "8022"
%connect-there there.env
there.env example#
# PythonHere device IP address
THERE_HOST=127.0.0.1
# Port, as set in PythonHere app Settings section
THERE_PORT=8022
# Username, as set in PythonHere app Settings section
THERE_USERNAME=here
# Password, as set in PythonHere app Settings section
THERE_PASSWORD=xxx
%there group of commands#
%there --help
Usage: there [OPTIONS] COMMAND [ARGS]...
Group of commands to run on remote side.
Options:
-b, --background Run in background
-l, --limit INTEGER RANGE Number of lines to show when in background mode
[1<=x<=1000]
-d, --delay FLOAT The time to wait in seconds before executing a
command
--help Show this message and exit.
Commands:
download Download files and directories.
get Evaluate expression on remote side and return its value.
kv Insert given rules into the Kivy Language Builder.
log Listen for log records, send logging output to stdout.
pin Create pinned shortcut to run a Python script from Android...
screenshot Display the actual image of the Kivy window.
shell Execute shell command on remote side.
upload Upload files and directories.
Default action for %there, if command is not specified - execute python code.
there#
Execute python code on the remote side.
%%there
import this
Show code cell output
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
get#
Evaluate a Python expression on the remote PythonHere side and return the value to the local notebook.
%there get --help
Usage: there get [OPTIONS] [EXPRESSION]...
Evaluate expression on remote side and return its value.
Options:
--help Show this message and exit.
%%there
device_status = {
"root_class": root.__class__.__name__ if "root" in globals() else None,
"child_count": len(root.children) if "root" in globals() else None,
}
status = %there get device_status
status
{'root_class': 'BoxLayout', 'child_count': 0}
The expression is evaluated remotely, so it can also inspect live objects:
root_size = %there get tuple(root.size)
root_size
(1080, 2125)
Use %there get for small and inspectable values. For large text, binary data,
images, CSV files, or directories, write a remote file and use %there download.
kv#
%there kv --help
Usage: there kv [OPTIONS]
Insert given rules into the Kivy Language Builder.
Options:
-c, --clear-style Unload previously applied rules
--help Show this message and exit.
If option --clear-style is provided,
all previous rules, that was loaded with %%there kv command,
are unloaded before command execution.
If root widget is defined, it will replace App’s current root.
%%there kv
Image:
source: "../app/data/logo/logo-128.png"
canvas.before:
PushMatrix
Rotate:
angle: 45
origin: self.center
canvas.after:
PopMatrix
shell#
%there shell --help
Usage: there shell [OPTIONS]
Execute shell command on remote side.
Options:
--help Show this message and exit.
%%there shell
pwd
/data/data/me.herethere.pythonhere_dev/files/upload
%%there shell
for i in 1 2 3
do
echo -n "$i"
done
123
Listen to Android system logs in the background and show last two lines of output:
%%there -bl 2 shell
logcat
upload#
%there upload --help
Usage: there upload [OPTIONS] LOCAL_PATH... [REMOTE_PATH]
Upload files and directories.
With one path, upload to the current remote SFTP directory. With multiple
paths, the last path is the remote destination.
Options:
--help Show this message and exit.
upload root directory is application current working directory.
%%bash
touch some.ico script.py
mkdir -p dir1/dir2
%there upload some.ico script.py dir1 ../
%%there shell
find
.
./some.ico
./script.py
./dir1
./dir1/dir2
./pythonhere-report.csv
./usage.csv
download#
%there download --help
Usage: there download [OPTIONS] REMOTE_PATH... [LOCAL_PATH]
Download files and directories.
With one path, download to the current local directory. With multiple paths,
the last path is the local destination.
Options:
--help Show this message and exit.
Files are downloaded from the same remote SFTP root used by %there upload.
With one remote path, the destination is the current local directory:
%there download some.ico
Provide a local destination path explicitly:
%there download some.ico ./downloaded-some.ico
Directories use the same command:
%there download dir1 ./downloaded-dir1
For generated data that is too large for %there get, save it remotely first:
%%there
import csv
rows = [
{"name": "root_class", "value": root.__class__.__name__},
{"name": "child_count", "value": len(root.children)},
]
with open("pythonhere-report.csv", "w", newline="") as file:
writer = csv.DictWriter(file, fieldnames=["name", "value"])
writer.writeheader()
writer.writerows(rows)
%there download pythonhere-report.csv ./pythonhere-report.csv
pin#
%there pin --help
Usage: there pin [OPTIONS] SCRIPT
Create pinned shortcut to run a Python script from Android home screen.
Options:
-l, --label TEXT Label for shortcut
--help Show this message and exit.
%there pin script.py --label "My script"
log#
%there log --help
Usage: there log [OPTIONS]
Listen for log records, send logging output to stdout. This command blocks
the execution thread until stopped.
Options:
--help Show this message and exit.
Note
Since the command blocks and never ends, it is useful to run with –background (-b) option
Listen to Python logs in the background and show the last line of output:
%there -b -l 1 log
%%there --delay 4
from kivy.logger import Logger
Logger.info("Example: Hey, Logger!")
screenshot#
%there screenshot --help
Usage: there screenshot [OPTIONS]
Display the actual image of the Kivy window.
Options:
-w, --width INTEGER Width in pixels to which to constrain a displayed
image
-o, --output FILENAME Path to a local file to save screenshot as PNG image
--help Show this message and exit.
Wait for half of a second before a command execution,
make a screenshot,
display a result constrained to 200px width,
and save image to a local file:
%there -d 0.5 screenshot -w 200 -o /tmp/screenshot_test.png
%%there ai#
Generate a reviewable %%there Python cell from a plain-language request.
The generated cell is inserted locally below the prompt cell. Review or edit it
before running it on the connected PythonHere device.
%there ai --help
Usage: %%there ai [OPTIONS]
Options:
--prompts TEXT Comma-separated prompt sections to append to the active
prompt stack.
--fix Fix the last executed %%there Python cell using the prompt
as guidance.
--help Show this message and exit.
%%there ai
Show Python version, Kivy platform, current working directory,
and root widget class.
Add an optional prompt section for one request:
%%there ai --prompts midi
Build a small MIDI note test UI with play, stop, and status controls.
Generate a replacement for the last executed Python %%there cell:
%%there ai --fix
Button was not imported.