...
Introduction
Python is a powerful environment for creating scientific processing applications. Python can be used with GX Developer to create extensions to Oasis montaj or stand-alone Python applications. This section documents how to configure your system to support Python development, which includes installing a Python distribution and the Geosoft Python modules, and how to create extensions that can be run from Oasis montaj. See Stand-Alone Programs for information on creating a Python script that can be run separately from Oasis montaj.
Info |
---|
See also the Python Tutorial for Geosoft GX Developer, which provides detail and examples for using the GX API's from Python. |
...
GX Developer for Python is intended for experienced python developers and learning Python is outside the scope of this document. For new Python developers there are many online resources that support learning. You will find that Google search is your best assistant as you develop Python programming skills. Here are some resources you may find helpful to get you going:
- Python.org – the Python.org website has the definitive documentation for every version of Python and has an excellent Python for Beginners section
- Google Python Class – Google is a big supporter of Python and has created a pretty good online course.
- There is a very nice online Python text at linuxtopia.
- Stackoverflow forums are an excellent source of topical help and you will find that Google searchs with questions like "python how do I ...?" will bring you to useful and specific Stackoverflow developer threads.
- Experimentation – Python is interactive. You can test and run code as you write it, create functions on the fly and so on. Python code examples that use the Geosoft gxpy can be found on github: https://github.com/GeosoftInc/gxpy/tree/master/examples
Info | ||||
---|---|---|---|---|
| ||||
|
Python Extensions
A Python extension is a Python script that contains a rungx()
function, and you can run a Python extension script just as you would a GX file (for example, see hello_world below). This includes adding a Python extension script file name to a menu or running a Python extension script from the "Settings / Run GX...".
Info |
---|
Before running Python extensions Oasis montaj needs to be configured to locate your Python distribution folder. This is the folder that contains the "python.exe" file. To do this select "Settings / Global settings / Python..." and locate the Python folder. The folder location and the default console setting are stored in the geosoft/core/environment registry. See Environment Variables for more information. |
If you are running Python extension scripts from a custom menu
You should place custom menu files in:
...\user\omn
You should place Python script referenced from your menus in
: ...\user\python
See Oasis montaj Menus for more information.
Geosoft Python API
The GX Developer Python package includes two modules:
...
This is the complete low-level GX API and includes all functions that are part of the standard API. Note that this is a direct mapping of the API available to all development languages, and as such the API style does not conform to a "pythonic" style of use. However, calling into the gxapi does provide access to everything within the development environment, and the usage follows the coding patterns you will find in Geosoft's legacy GXs that are part of Geosoft Desktop. Also, the gxapi is assured to be forward version compatible while the gxpy module is currently best-effort in this regard.
If you choose to work with the gxapi you may find the many coding examples from the installed GX Developer, which you will find at C:\Program Files\Geosoft\GX Developer\gx\src
.
...
import geosoft.gxapi as gxapi
...
The gxpy module contains a set of sub-modules that a provide a higher-level "pythonic" interface to underlying gxapi. Many standard Python needs are included in the modules, and we suggest that straight-forward Python applications will be able to work with only the gxpy.
The gxpy module covers only part of the full function of the gxapi, and with each release of GX Developer will continue to expand and extend the capabilities of gxpy.
...
import geosoft.gxpy as gxpy
Python Examples
A number of example python extensions are provided in the gxpy-examples
repository (https://github.com/GeosoftInc/gxpy/tree/master/examples), as well as in the Python Tutorial for Geosoft GX Developer.
To run the examples, open an Oasis montaj project and run the python script by clicking on the GX button (or choose "Settings / Run a GX..."), then browse to the script (for example "hello_world.py
"):
hello_world
This is a classically basic script that illustrates the of the API to do something very simple, which is say hello to the user.
Code Block |
---|
import geosoft.gxapi as gxapi # gxapi methods
import geosoft.gxpy as gxpy # gxpy methods
def rungx(): # a python script must have a rungx(), which is executed by OM when the script is run
gxp = gxpy.gx.GXpy() # get the current gx context
gxapi.GXSYS.display_message("GX Python", "Hello {}".format(gxp.gid)) # say hello to the user identified by gxp.gid. |
When you run this script from Oasis montaj you will see the following:
chanadd.py
This example shows how to work with a database by adding a constant value to a channel on all selected lines. User parameters are saved for re-use for the next time the extension is run from the same project.
...
Oasis montaj may also be extended using Python. Python scripts that are written as an extension can be run directly from the button, or the Settings / Run GX... menu item, or by specifying the name of the script with .py extension as an ITEM entry in an omn file (see Oasis montaj Menus).
The Python Tutorial for Geosoft GX Developer provides an overview of Python with lessons that demonstrate how to create extensions (see Hello World#DesktopExtension), and how to add an extension to a menu (see Create a Menu to Run Python Extensions).
See also Python Stand-Alone that describes how to run Python scripts directly from the Python interpreter.