Hello World

What You Will Learn

  1. How to import Geosoft modules and call into the Geosoft GX environment.
  2. How to establish context for the GX API.
  3. How to use PyCharm as an IDE.
  4. How to create a stand-alone script
  5. How to create and run a Geosoft Desktop (Oasis montaj) extension
  6. How to create a script that runs as either an extension or a stand-alone script.

In this session we will create the classic "Hello World" application, though we will personalize it to your Geosoft ID. We will create both a stand-alone script and a Geosoft Desktop extension that can be run from Oasis montaj.

Stand Alone

The script:

hello_world_simple.py
import geosoft.gxpy as gxpy

# a GX context is required, and must be assigned to a variable that persists through the life of execution.
gxc = gxpy.gx.GXpy()

# gid is a property of the context that holds the user's Geosoft ID.  Say hello.
print('Hello {}'.format(gxc.gid))
line 1Import the geosoft.gxpy module.
line 4Establish the Geosoft GX context, which identifies the user from their Geosoft ID and determines the rights and entitlements to enable more advanced functions. 

PyCharm

To add a new Python script named hello_world.py to the tutorial project, hover over the tutorial project, right-click, New → Python File:

   



Enter the script above into the hello_world.py:

To run the script in PyCharm, place the cursor anywhere in the hello_world.py window, right-click, Run 'hello_world':

The console output from the script is displayed in the Run window that opens at the bottom of the IDE window:


Desktop Extension

A Geosoft Desktop application (Oasis montaj, Geosoft Viewer and Target) is able to run a Python script as an extension.  The desktop application must be configured to recognize the Python installed on your user's computer.  Refer to Python Installation and Configuration#ConfiguringOasismontajtoRunPythonExtensions for more information.

hello_world_extension.py

Now we will create a Geosoft Desktop extension that displays a "Hello" message.  Notable in this exercise:

  1. Creating a context is not required for an extension because the context of the running Geosoft Desktop is implied.  However, in this example we will establish context simply to get the Geosoft ID, which is the gid property of the context.
  2. Extensions must define a function named rungx(), which is the entry point when the script is run from Geosoft Desktop.
  3. In this exercise we demonstrate calling both the geosoft.gxpy module and the geosoft.gxapi module.  geosoft.gxpy.gx.GXpy() is called to get the context, and geosoft.gxapi.GXSYS.display_message() is called to display a message dialog in Geosoft Desktop.

In a text editor (or in PyCharm), create the extension script, which we will call hello_world_extension.py:

hello_world_extension.py
import geosoft.gxapi as gxapi
import geosoft.gxpy as gxpy

# a python script must have a rungx(), which is executed by OM when the script is run
def rungx():

    # get the current gx context
    gxp = gxpy.gx.GXpy()

    # say hello to the user identified by gxp.gid.
    gxapi.GXSYS.display_message("GX Python", "Hello {}".format(gxp.gid))

We will now run the extension. Open Oasis montaj, select the Run GX... button from the Project Explorer window, then browse to the script:


Selection OK will load the Python interpreter as part of the current session, open a console window for Python output, then execute the rungx() function in the script.  The rungx() function gets the GX context and displays a message box inside Oasis montaj:

Click OK, to close both the message box and the Python console window.

Now we will introduce an error into our extension so that we can see how an error exception is reported.  In line 5 of the script, change gxpy.gx.GXpy() to gxpy.GXpy(), save the change and run the script extension:

hello_world_extension_bug.py
import geosoft.gxapi as gxapi
import geosoft.gxpy as gxpy

# a python script must have a rungx(), which is executed by OM when the script is run
def rungx():

    # get the current gx context - BUG, gxpy has no attribute GXpy
    gxp = gxpy.GXpy()

    # say hello to the user identified by gxp.gid.
    gxapi.GXSYS.display_message("GX Python", "Hello {}".format(gxp.gid))

Running this displays the following window:

Showing the Call Stack will identify that code line 8 contains the error.

Combined extension and stand-alone program

You may want to create scripts that can be run either from the command line or as an extension.  In this case include both a rungx() function as the entry point for an extension, and a section with the standard Python pattern to execute only if run from directly by a Python interpreter (if __name__ == "__main__": ).  In this example we are only creating a context to determine the Geosoft ID, so a local variable is not required to hold the context:

hello_world.py
import geosoft.gxapi as gxapi
import geosoft.gxpy as gxpy

def rungx():
	gxapi.GXSYS.display_message("GX Python", "Hello {}".format(gxpy.gx.GXpy().gid))

if __name__ == "__main__":
    print('Hello {}'.format(gxpy.gx.GXpy().gid))

This script can be run as either an extension or a script from the Python interpreter:

C:\Development\Projects\tutorial>c:\ProgramData\Anaconda3\python.exe hello_world.py
Hello makiriderexplorer@gmail.com

C:\Development\Projects\tutorial>