Introduction
When an IronPython script potentially may execute for a longer time it may be convenient to display the progress in a progress bar and give the user the option to cancel the script. This example shows how to achieve that.
By using the ProgressService and placing your script code inside a progress operation, you will automatically get a progress bar prompted after a couple of seconds. It is important to catch any ProgressCanceledException. Also, the script must not be configured to be executed in a transaction. Make sure that setting in the script dialog is unchecked:
When the script runs in the installed client the following dialog shows up after a while. Title, description and details such as subtasks and progress reporting can be controlled by your script.
And here is the progress indicator when running the same script in the web client:
![Progress dialog in the web client How-to-Add-Progress-Bar-and-Cancellation](https://community.spotfire.com/applications/core/interface/js/spacer.png)
Code Sample
The code below shows two different methods in the Progress class of running subtasks and reporting progress. You can easily apply a similar progress pattern to your own script.
# Copyright © 2017. TIBCO Software Inc. Licensed under TIBCO BSD-style license. from Spotfire.Dxp.Framework.ApplicationModel import * import time ps = Application.GetService[ProgressService]() def execute(): try: # subtask 1 - simulates an indeterminate step ps.CurrentProgress.ExecuteSubtask("Subtask 1"); time.sleep(5) # do some work ps.CurrentProgress.CheckCancel() # subtask 2 - simulates a determinate step start = time.clock() stepCount = 10 with ps.CurrentProgress.BeginSubtask("Subtask 2", stepCount, "Step {0} of {1}") as f: for step in range(0, stepCount): if (time.clock() - start) > stepCount: break ps.CurrentProgress.CheckCancel() time.sleep(1) # do some work ps.CurrentProgress.TryReportProgress() except: # user canceled pass ps.ExecuteWithProgress("Progress title", "Progress description", execute)
If the work is executed in one or more background tasks, the progress script needs to wait for all the tasks to complete.
References
License: TIBCO BSD-Style License
-
1
Recommended Comments
There are no comments to display.