\(\renewcommand\AA{\unicode{x212B}}\)
This is a quick summary of how to use python scripts in MantidWorkbench. To learn more about this, see the further tutorials on the home page. Even if you are a novice at Python, it is useful to know how to run a script as you can produce one within Mantid: from the “Generate a script” button on a plot toolbar, or from project recovery when you reopen MantidWorkbench after a crash.
Within MantidWorkbench you can make use of the script window or IPython prompt to manipulate data and build plots exactly to your specifications in ways that you would not be able to with the interface alone.
You can also use the IPython tab below the script editor to change to an inline Python interpreter. Both the python interpreter and scripts run through MantidWorkbench are able to interact with Mantid workspaces, plot and algorithms.
By default any new script opened in MantidWorkbench will come with some default imports:
from mantid.simpleapi import *
This imports all the algorithms from Mantid to be used within your Python script.
import matplotlib.pyplot as plt
import numpy as np
This imports Matplotlib for use as well as the excellent NumPy package.
In MantidWorkbench these imports are visibly added at the top of the script, so that script will also work outside of MantidWorkbench.
To open a new script you can click on the + button, or to open an existing script in a new tab go to “File” -> “Open Script” in the MantidWorkbench taskbar or use the hotkey Ctrl + O. From the script window you can also tab different scripts working on each independently and multiple scripts can be run simultaneously. MantidWorkbench will restore any tabs that were open the last time MantidWorkbench was shut down.
From here you can run or abort your script, toggle options for accessibility, and use the find and replace tool.
#
at the beginning of the line.The toggle whitespace visible option is global and will affect the appearance of all your tabs.
The status bar tells you if the code in the currently open tab is running or not. If the code has been run previously the status bar will give details of the previous run including runtime, and whether the code ran without errors.