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=8023

# Username, as set in PythonHere app Settings section
THERE_USERNAME=admin

# 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
  -d, --delay FLOAT          The time to wait in seconds before executing a
                             command

  --help                     Show this message and exit.

Commands:
  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.
  screenshot  Display the actual image of the Kivy window.
  shell       Execute shell command on remote side.
  upload      Upload files and directories to `remotepath`.

Default action for %there, if command is not specified - execute python code.

there

Execute python code on the remote side.

%%there 
import this

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] LOCALPATHS... REMOTEPATH

  Upload files and directories to `remotepath`.

Options:
  --help  Show this message and exit.

upload root directory is application current working directory.

!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

pin

%there pin --help
Usage: there pin [OPTIONS] SCRIPT

  Create pinned shortcut to run a Python script.

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
  execution thread forever.

Options:
  --help  Show this message and exit.

Note

Since the command blocks and never ends, it is useful to run with –backgroud (-b) option

%there -b -l 1 log
# wait, to make sure *log* cell connection is established before next cell is executed
import asyncio ; await asyncio.sleep(3) 
%%there
from kivy.logger import Logger
Logger.info(f"Hello from the main cell")

screeenshot

%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
../_images/commands_39_0.png