Import Python files from disk, call their functions, and automatically reload the code when the files change.
This function is meant to be used during development; see PyRun for a production version of the function.
PyLab can be used in two forms:
Register a module:
PyLab( "register:"; module )
Run a function from the module:
PyLab( module; object {; arguments... } )
PyLab( "register:"; module )
"register:"
keyword, case-insensitive, filteredIndicates that this is a call to register a module. The parameter must end
with a colon, but case or non-alphanumeric characters do not matter:
register:
, REGISTER:
, and --register:
will work the same.
module
textPath to the module in system format.
Normally the function returns no result. Internally the function imports the specified module and remembers it under its file name. The module becomes available to the whole plug-in; e.g. you can call its functions with PyRun.
Note that if you import a module with the same name as the module in the standard library, these modules may conflict and disrupt plug-in functioning.
If there's an error, then the function returns a question mark (?
). You
can read the exact error message with PyLastError. The function itself
raises the following errors:
If the module
path does not exist: The path {path} does not exist
,
class IOError
.
If the module
path is not a file: The path {path} is not a file
,
class IOError
.
It may also raise exceptions propagated from the lower levels, such as syntax errors if the file is malformed.
PyLab( "register:", "C:\Users\mikhail\Documents\hello.py" )
This call will try to import the specified Python module and remember it as
hello
. Note that the path may not be case-sensitive, but the resulting
Python module name is case-sensitive.
PyLab( module; object {; arguments... } )
module
string, case-sensitive, previously registeredPython module name that has been registered as described in Register a module.
object
string, case-sensitiveName of a callable object (usually a function) or any object that can be
directly returned to FileMaker in the module
.
arugments
Optional arguments as required by the callable. If the object is not callable, the function will ignore the arguments.
Each time you register a module, PyLab remembers the module name and the corresponding source file. When later you call the module, PyLab checks if the file has changed since the last call and if yes, reloads it before calling the function.
The final result and side effects depend on the called function.
PyLab itself does not explicitly raise any errors, but may send up errors received from lower levels.
PyLab( "hello", "hello", 123, 456 )
This call will try to find the previously registered module hello
and then
the hello
object in the module. If the object is callable, the function
will call it with two parameters: 123
and 456
. If the object is not
callable, the function will try to return it as is.
Right now PyLab can only work with single files and won't understand standard Python packages, for example. PyLab only checks the module itself; it does not analyze module dependencies and thus won't be able to properly reload multi-file setups.
The imported module will work as if it was bundled together with the plug-in, but only for the current session; if you restart FileMaker or the plug-in, you'll need to register the modules again.