SAM provides a python method, sam.ui_register(), to register python methods with the user interface. This method can be used to add arbitrary functionality to the SAM UI.
The ui_exec() method allows SamUI commands to be executed from python.
The sam python object provides the following methods to access the
SamUI, registers and unregister methods as commands with the SamUI.
Method Name | Description |
---|---|
ui_exec ("cmd") | Sends cmd to the SamUI. |
ui_register ("cmd_name", "help_string", cmd_function) | Registers the python function cmd_function with the SamUI as "cmd_name". The commands help string is "help_string". |
ui_unregister ("cmd_name") | Unregisters the command registered with the SamUI, "cmd_name". |
Here is an example that registers the python method
echo()
as "py_echo".
def echo(*argv):
print argv
sam.ui_register("py_echo", "Prints its arguments", echo)
Here is another example that runs python code from the SamUI:
def exec_py(*argv):
cmd = " ".join(argv[1:])
try:
exec(cmd)
except:
print "ERROR"
sam.ui_register("exec_py","exec_py runs python code",exec_py)
Now from the SamUI, one can type:
exec_py xyz = 10
exec_py print "xyz is %d" % xyz
and see
xyz is 10