Output Mechanisms

General

This page presents the various methods for outputting information from scripts and details their pros and cons.

Toast notifications

Toast notifications are brief alerts that appear at the bottom of Workbench and fade out after a few seconds. They provide transient feedback without requiring user dismissal. The following script demonstrates how to create toast notifications with various severity levels:

from mapteksdk.project import Project
from mapteksdk.operations import show_toast_notification, Severity
 
if __name__ == "__main__":
  with Project() as project:
    show_toast_notification(
      "Default",
      "The default severity is information."
    )
    show_toast_notification(
      "Info",
      "Everything is fine.",
      Severity.INFORMATION)
    show_toast_notification(
      "Warning",
      "Something may have gone wrong.",
      Severity.WARNING)
    show_toast_notification(
      "Error",
      "Something has gone wrong.",
      Severity.ERROR)

The following animation shows the toast notifications generated by the script:

Note

Toast notifications:

  • Are suitable for output where it is not critical if the user misses the message.

  • Are not appropriate for outputs that the user may need to keep.

  • Require the script to be connected to a running application.

  • Have a default severity of Severity.INFORMATION.

  • Do not halt the script.

  • Appear in the workbench window and may be delivered late if the Workbench is not the active application.

Messages

Messages are designed to capture the user’s attention. They appear in a small window within the Workbench and remain until dismissed by the user. Messages with warning or error severity levels play a tone and cause the Workbench application to flash orange to further attract attention.

Given their attention-grabbing nature, messages should be used sparingly. The following script demonstrates how to create messages with different severity levels:

from mapteksdk.project import Project
from mapteksdk.operations import show_message, Severity
 
if __name__ == "__main__":
  with Project() as project:
    show_message(
      "Default",
      "The default severity is information."
    )
    show_message(
      "Info",
      "Everything is fine.",
      Severity.INFORMATION)
    show_message(
      "Warning",
      "Something may have gone wrong.",
      Severity.WARNING)
    show_message(
      "Error",
      "Something has gone wrong.",
      Severity.ERROR)

The messages generated by this script are shown below.

Note
  • Messages are appropriate for output where it is very important that the user notices it.

  • Using too many messages can be very obtrusive. They should be reserved for important information.

  • Messages with a severity of Severity.ERROR and Severity.WARNING will play a tone when they appear to alert the user that something is wrong.

  • The script will not wait for the user to click Dismiss before continuing.

  • Messages require the script to be connected to a running application.

Template script for message on error

The following script is a template for creating scripts where:

  • If the script finishes without encountering an error, a toast notification notifies the user.

  • If the script encounters an error, a message notifies the user that something is wrong. This includes the type of error raised and the error message similar to if the script was run from the command line, but it omits the stack trace.

import os
 
from mapteksdk.project import Project
from mapteksdk.operations import show_message, show_toast_notification, Severity
 
SCRIPT_NAME = os.path.basename(__file__)
 
def do_something_useful(project: Project):
  """A function which does something useful.
 
  Parameters
  ----------
  project
    The Project to do something useful with. This can be removed if you
    don't need it when using this template.
  """
  # Remember to rename this function to have a sensible name!
  # Absolutely do not keep the name as "do_something_useful".
 
if __name__ == "__main__":
  with Project() as project:
    try:
      do_something_useful(project)
      show_toast_notification(SCRIPT_NAME, "Completed")
    except Exception as error:
      message = f"{type(error).__name__}: {str(error)}"
      show_message(SCRIPT_NAME, message, Severity.ERROR)

Choosing the appropriate output type

A Python script can deliver output in various ways. This section covers different types of output and the appropriate output to use in those situations.

Standard output

The print() statement is the simplest method for outputting information. It outputs the supplied text to the command window the script is running in.

For example, the following script:

print("Hello, world!")

results in the following output in the terminal window:

Hello, world!

If the script runs in a pop-up shell window, the window closes upon completion, which can make debugging difficult if an error occurs. When running within Workbench or Visual Studio Code, the output window remains visible after the script ends.

Note
  • Using standard output in the Extend Python Script workflow component will cause the workflow to pause and prompt the user to dismiss the message box, which should be avoided.

  • Scripts with a .pyw extension do not display standard output.

Pros

The standard output has the following advantages:

  • It is always available to scripts; no imports are required.

Cons

The standard output has the following disadvantages:

  • It is not suitable for critical information as it may go unnoticed.

  • It can be obtrusive.

Raising an error

Raising an error is appropriate when a script encounters an unexpected condition. An unhandled error terminates the script, which is generally preferable to proceeding with incorrect assumptions. Errors can be caught and handled by the caller.

For example:

raise ValueError("Expected the input to be greater than zero.")
Pros

Raising an error has the following advantages:

  • It is useful for halting a script due to false assumptions.

  • It is always available to scripts; no imports required.

  • Errors often include a stack trace, which can be helpful for diagnosing the cause of the error.

Cons

Raising an error has the following disadvantages:

  • By default unhandled errors are written to the standard output, inheriting all the disadvantages of using print().

  • Errors often include a stack trace, which may be intimidating for less technical users.

Toast notification

For example:

from mapteksdk.project import Project
from mapteksdk.operations import show_toast_notification
 
with Project() as project:
  show_toast_notification("Toast notification", "Hello world!")

Toast notifications are suitable for transient information where it is acceptable if the user misses the message.

Pros

Toast notifications have the following advantages:

  • They are less obtrusive than other forms of output.

  • They can make the script feel like it is part of the application.

Cons

Toast notifications have the following disadvantages:

  • They are not appropriate for large amounts of output.

  • They are not appropriate for output that the user may want to keep or refer back to.

  • They require an import and a connection to a running application.

  • They may be delivered late if the application is not in focus.

Message box

Message boxes are appropriate for small amounts of critical information that must be acknowledged by the user. They remain visible until dismissed and can include a tone to attract attention.

For example:

from mapteksdk.project import Project
from mapteksdk.operations import show_message
 
with Project() as project:
  show_message("Message", "Hello world!")
  • Message boxes are appropriate for small amounts of important information.

  • The message box does not disappear until the user dismisses it, so any information reported by a message box cannot be missed by the user.

Pros

Message boxes have the following advantages:

  • They are difficult for the user to miss.

  • They can play a tone when it appears to attract the user’s attention.

  • They can make the script feel like it is part of the application.

Cons

Message boxes have the following disadvantages:

  • They are not appropriate for large amounts of data.

  • They are not appropriate for output the user may want to keep.

  • They Are very obtrusive.

  • They require an import and a connection to a running application.

Report window

The report window is ideal for small to medium amounts of information that users may want to review later. The information remains accessible until the Workbench is closed or the report is manually cleared.

For example:

from mapteksdk.project import Project
from mapteksdk.operations import write_report
 
with Project() as project:
  write_report("Report", "Hello world!")
Pros

The report window has the following advantages:

  • It is not too obtrusive.

  • It retains data until the user closes the Workbench or manually clears it, allowing the user to refer back to the information as needed.

  • It integrates well with the application, making the script feel like a native part of it.

  • It is suitable for medium to large amounts of data.

Cons

The report window has the following disadvantages:

  • It is not appropriate for large amounts of data.

  • It requires an import and a connection to a running application.

File

Files are the best choice for handling large volumes of output that users need to retain. Files are persistent and must be stored in a location accessible to the user. The file format (e.g. JSON, CSV) should be chosen based on the intended use of the data.

For examples, see reading and writing files in the Python documentation.

Pros

Files have the following advantages:

  • They are the only way to handle large amounts of output that user may want to keep.

  • They are persistent and will remain available until the user manually deletes them.

Cons

Files have the following disadvantages:

  • They need to be saved in a location that is easily accessible to the user.

  • They require choosing an appropriate format (e.g., JSON, CSV, pickle) based on the intended use of the data.

  • They have a greater potential for errors.

Advanced: logging

For examples, see the Python documentation for logging.

Logging is a special type of output designed for the developer of a script rather than the user of the script. Ideally, the user of the script will never see the log messages. Rather, if something goes wrong with the script they send the log files to the developer of the script who can use the content of the log file to help them fix the issue.

By default, log messages are written to standard output; however, it is often preferable to configure the log to be written to a file in a well-known location. If log messages are written to standard output they have all the same pros and cons as standard output. If log messages are written to a file they have the following pros and cons.

Pros

Logging has the following advantages:

  • It is completely unobtrusive. Typically the user will not be aware that logging is happening.

  • It can include information that is too technical, which may intimidate a user of the script.

  • It can include large amounts of information.

  • Logs remain intact even if the script crashes, making them valuable for understanding what the script was doing at the time of the crash.

Cons

Logging has the following disadvantages:

  • It requires that any user-facing information be written to a separate output channel.

  • It can slow down script execution if too many log messages are generated.

  • It relies on the user knowing to send the log file to the developer if a problem occurs.

Comparison of output types

Output type Obtrusiveness Maximum data quantity Appropriate uses
Standard output (i.e. print) Moderate Large Inconsequential status updates, small amounts of data the user may want to keep, development and initial testing of scripts
Raising an error (i.e. raise) High Small Notifying the user that something unexpected has happened
Toast notification Low Small Transient information that the user may miss
Message High Small Important information that the user should not be allowed to miss
Report window Moderate Medium Information that the user may want to keep
File Low Very large Information the user will want to keep or persistent information
Logging (file) Low Very large Information useful for fixing bugs in the script