Python scripting is an important part of ParaView. There are several places where the users are expected to enter, edit or inspect Python scripts including the Python shell, the Properties panel for Programmable Filter or Programmable Source and the Python Script Editor used for adding macros/tracing. One of the common complaints is that these are simply text entry widgets not designed for accepting Python code, thus they have no proper syntax coloring or handling of tabs/indentation.
New changes on the way to being merged in ParaView's development repository will improve these greatly. ParaView will depend on the pygments Python module which will be included in ParaView. This module can be used to provide syntax highlighting for a variety of languages including Python. The Python script editor (most easily seen by clicking Tools->Start Trace followed by Tools->Stop Trace and covered in a prior blog) now supports Python syntax highlighting. This will make the code far easier to read.
Additionally the behavior of the tab key has been altered to insert four spaces instead of a tab. Since mixing spaces and tabs in Python code is illegal, the version of Pygments within ParaView has been altered to highlight tabs in red so that the user will know which indentation style is in use. If ParaView is using the system Pygments library this additional functionality will not be enabled.
Python code is also used in ParaView in the properties panel of the Programmable Filter and Programmable Source. The Properties panel text areas have also been changed to highlight syntax for python and convert tab key presses to spaces.
Aditionally, since the text areas in the properties panel are too small to edit large scripts, a button has been added to pop out the text area into a separate dialog that can be resized.
The Python Shell has been improved so that the tab key will work for indentation, but it does not support syntax highlighting yet. If this is a desired feature please let us know.
If you are writing a ParaView module that is using a QTextEdit subclass and you want to add Python syntax highlighting to your QTextEdit, the pqPythonSyntaxHighlighter class is provided. The modifications to the behavior of the QTextEdit can be added like this:
QTextEdit *edit = new QTextEdit(parent); new pqPythonSyntaxHighlighter(edit,parent);
If you are adding a filter type to ParaView and want the properties panel text area to be syntax highlighted and able to pop out into a dialog, this can be easily added to any multi-line widget. The hint syntax="python" in the XML will cause the python syntax highlighting, modified tab behavior and pop out to a dialog button to be added to that text area. The following is an excerpt from the Python Programmable Fitler xml, showing the hint in use:
<StringVectorProperty command="SetScript" name="Script" number_of_elements="1" panel_visibility="default"> <Hints> <Widget type="multi_line" syntax="python" /> </Hints> <Documentation>This property contains the text of a python program that the programmable filter runs.</Documentation> </StringVectorProperty>