‹‹ Back to SVS Home
The Python Editor Window
6.3 The Python Editor Window
There are several ways to begin developing customized script routines. You could open a text editor and save your script commands in a .py file with the help of the Tools > Open UserScripts Folder menu option. Alternatively, you could use the Python Editor Window that is part of SVS found under the Tools > Open Python Editor menu option. The final option is to first open the Python shell window using the Tools > Open Python Shell menu option, enter script commands interactively and later copy them to the Python Editor Window to create and save a Python script.
Python Editor Overview
There are four major areas of the Python Editor Window. These are the menu bar, the toolbar, the Script Folder bar, and
the text box for writing the script (see Figure 35). In addition, in the lower right corner, there is an indicator of the line and
column positions of the edit cursor within the current text box.
The Menu Bar
The menu bar contains two menus, the File menu and the Edit menu. The items for these menus are listed below: The File Menu:
- New Script: Create a new script.
- Open Script: Open a provided script or user created script.
- Save Script: Save the current script. Requires that a script is open.
- Save Script As...: Save the current script to a different location. Requires that a script is open.
- Close Script: Close the current script. Requires that a script is open.
- View User Scripts Folder: Launches (or puts into focus, if one already exists) a file browser window from your operating system which is open to the main user script file folder. You may then use this window to conveniently open a Python script using a text editor of your choice.
- Quit Python Editor: Closes the Python Editor Window.
- Undo: Undoes the last operation. Requires that a script is open and there is an operation to undo.
- Redo: Redoes the last operation. Requires that a script is open and there is an operation to redo.
- Cut: Cuts the selected text. Requires that a script is open and there is selected text.
- Copy: Copies the selected text. Requires that a script is open and there is selected text.
- Paste: Pastes the copied or cut text to the current location of the cursor. Requires that a script is open and there has been text copied to the clipboard.
- Find/Replace: Brings up a find/replace window, with a tab for find and a tab for find and replace. Find features include specifying direction of search, specifying that a whole word must match, and specifying matching case. Replace features include being able to replace all occurrences of a string within the entire file and being able to replace all occurrences of a string within the selected area.
- Indent Left: Moves every line within the selected section (or the line where the cursor is, if there is no selection) left four spaces, or completely to the left if the line was not already indented by four spaces.
- Indent Right: Moves every line within the selected section (or the line where the cursor is, if there is no selection) right by four spaces.
The Toolbar
The toolbar contains icons for most menu items. The icons on the toolbar are listed below in order from left to right:
- New Script: Create a new script.
- Open Script: Open a provided script or user created script.
- Close Script: Close the current script. Requires that a script is open.
- Save Script: Save the current script. Requires that a script is open.
- Undo: Undoes the last operation. Requires that a script is open and there is an operation to undo.
- Redo: Redoes the last operation. Requires that a script is open and there is an operation to redo.
- Cut: Cuts the selected text. Requires that a script is open and there is selected text.
- Copy: Copies the selected text. Requires that a script is open and there is selected text.
- Paste: Pastes the copied or cut text to the current location of the cursor. Requires that a script is open and there has been text copied to the clipboard.
- Find/Replace: Brings up a find/replace window, with a tab for finding and a tab for find and replace. Find features include specifying direction of search, specifying that a whole word must match, and specifying matching case. Replace features include being able to replace all occurrences of a string within the entire file and being able to replace all occurrences of a string within the selected area.
- Indent Left: Moves every line within the selected section (or the line where the cursor is, if there is no selection) left four spaces, or completely to the left if the line was not already indented by four spaces.
- Indent Right: Moves every line within the selected section (or the line where the cursor is, if there is no selection) right by four spaces.
The Script Folder Bar
All possible locations to save a script and have it available through the SVS menu structure are listed on the Scripts Folder Bar. These folder locations and how they might be used are described below. Spreadsheet Scripts that are to be run on a spreadsheet should be saved in the appropriate subdirectory of this folder. The subdirectories correspond to the menus available from the Spreadsheet Viewer (the Column subdirectory corresponds to the Column Header Menu). Each subdirectory also has options for creating a new script in that folder, to create a new folder, or to launch a file browser window from your operating system which is open to that subdirectory folder.- Analysis: Scripts written to perform customized analysis.
- Column: Scripts written to perform customized tasks on a column.
- Edit: Scripts written to prepare a spreadsheet for analysis.
- File: Scripts written for custom export options.
- Plot: Scripts written for custom plot visualization launching.
- QualityAssurance: Scripts written for custom quality assurance methods.
- Scripts: Folder for storing scripts for any purpose.
- Select: Scripts written for custom row or column selection.
- Column: Scripts written for customized editing of a column.
- Scripts: Scripts written for customized editing of a spreadsheet or multiple columns.
- File: Scripts written for customized project creation.
- Import: Scripts written for customized or automation of import of files.
- Tools: Scripts written to create custom project tools.
Creating A New Script
A new script can be created through the File > New Script menu option, or by selecting the appropriate directory and
subdirectory for the script to be saved and then selecting “New Script...”.
For example, if a script to calculate the global mean of a spreadsheet was to be written and available in the Analysis menu of a Spreadsheet this can be done in two ways. These are:
- Create new script first and then save.
- Create new script using the File > New Script menu option.
- Save script using the File > Save Script menu option, in the
“../SVS Data Folder/scriptsSVS/user/Spreadsheet/Analysis” directory as GlobalMean.py.
- Create script by choosing save location first.
- Select Spreadsheet > Analysis > New Script... from the Script Folder Bar.
- Name the new script “GlobalMean”
Essential Command Lines for Scripts (Other than Spreadsheet Editor Scripts)
Each script to be run from a spreadsheet or from the project navigator window needs to have a function definition, and a command to try the function. An example of these commands are as follows.
def global_mean(): #defines the function
#Enter Python commands to calculate the global mean here try: #tries to run the defined function global_mean() except: #if there is an error then print an error message in the Python Shell ghi.error() |
Essential Command Lines for Spreadsheet Editor Scripts
Each script to be run from the Spreadsheet Editor, on the entire spreadsheet or on multiple columns, needs to have a function definition defined exactly as follows. This type of script does not need a command to try the function.
’’’
(This is a block comment in Python) Function definition for editing a spreadsheet or multiple columns. Input: dataEditModel: Python object for editing the spreadsheet data ’’’ def editData(dataEditModel): #This line must be written exactly this way #Enter Python commands here |
Essential Command Lines for Spreadsheet Editor Column Scripts
Each script to be run from a column in the Spreadsheet Editor needs to have a function definition defined exactly as follows. This type of script does not need a command to try the function.
’’’
(This is a block comment in Python) Function definition for editing a column. Input: dataEditModel: Python object for editing the spreadsheet data colIndex: the column number for the selected column ’’’ def editColumn(dataEditModel, colIndex): #This line must be written exactly this way #Enter Python commands here |
NOTE:
- The best way to create a script is to use a provided script as a guide for creating a new script. Scripts that are provided are a guide to the required Python syntax.