Python API

The tools of the MCP server are described in Usage.

Client

Client of lys remote server (lys/localPlugins/Remote.py in lys).

This module does not depend on lys and Qt, so that it can be used from MCP server and command line. lys should be launched with –remote option, e.g. python -m lys –remote

Each lys has a label (python -m lys –remote LABEL, default is its process id). If only one lys is running, it is selected automatically. Otherwise, select it by –label or LYS_REMOTE environment variable.

lys on another computer can be used when it is launched with –port option (python -m lys –remote –port 8765). Select it by “tcp://<host>:<port>” (e.g. -l tcp://192.168.1.4:8765 or LYS_REMOTE=tcp://192.168.1.4:8765). If lys is launched with –token, give the token by –token or LYS_REMOTE_TOKEN environment variable.

Command line usage:

lys-remote --instances                    # list running lys
lys-remote "a = 1; a + 1"                 # execute code and print the result
lys-remote -l expA "a = 1; a + 1"         # execute code in lys launched with --remote expA
lys-remote -l ~/data/expA "a + 1"         # execute code in lys launched in ~/data/expA
lys-remote -f script.py                   # execute file
lys-remote --image out.png                # save image of the front canvas
lys-remote --image out.png --target g     # save image of the canvas/figure g
lys-remote --list                         # list variables in shell
lys-remote -l tcp://192.168.1.4:8765 "a"  # lys on another computer launched with --port 8765
class lys_mcp.client.LysClient(label=None, timeout=600, token=None)[source]

Bases: object

Client to send requests to lys.

Parameters:
  • label (str) – The label of lys. See resolveLabel().

  • timeout (float) – Timeout in seconds.

  • token (str) – The token for lys launched with –token. If None, LYS_REMOTE_TOKEN environment variable is used.

Example:

with LysClient() as c:
    res = c.exec("a = 1; a + 1")
    print(res["result"])   # "2"
close()[source]
connect()[source]
exec(code)[source]

Execute code in lys shell. The value of the last expression is returned as ‘result’.

image(target=None, dpi=100)[source]

Return response with png image (base64) of canvas, matplotlib figure or widget as ‘image’.

info()[source]

Return label, pid, home directory and protocol version of lys.

property label
list()[source]

Return variables in lys shell as ‘result’.

request(op, **kwargs)[source]

Send a request and return the response as dict.

exception lys_mcp.client.LysRemoteError[source]

Bases: Exception

lys_mcp.client.findLabel(key)[source]

Return the label of lys whose label or home directory is key. If not found, key is returned.

lys_mcp.client.formatResponse(res)[source]

Format response as text.

lys_mcp.client.instances()[source]

Return the information of running lys as list of dict.

lys on another computer is included when it is given by LYS_REMOTE environment variable.

lys_mcp.client.listLabels()[source]

Return the labels of running lys.

lys_mcp.client.main()[source]
lys_mcp.client.resolveLabel(label=None)[source]

Return the label of lys to be connected.

label can be the label or the home directory of lys (the directory where lys was launched), or “tcp://<host>:<port>” for lys on another computer. If label is None, LYS_REMOTE environment variable is used. If it is not set, the running lys is selected when only one lys is running.

lys_mcp.client.serverName(label)[source]

Return the name of the local socket for label. The same rule as serverName in lys/localPlugins/Remote.py.