Examples

If you downloaded a source archive or cloned pyQode from github, you will find a series of examples in the examples directory, at the root of the archive.

All examples requires pyqode.qt, pyqode.core and pyqode.python to be installed.

Note

All examples are bindings independent so that every user can run them without being required to install an unwanted qt binding.

Pre-configured

Basic example that show you how to use the pre-configured python code editor widget.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
"""
This simple example shows you how to use the pre-configured python editor.
"""
import logging
logging.basicConfig()
# optionally, set the qt api to use (in ['pyqt4', 'pyqt5', 'pyside'])
# import os; os.environ['QT_API'] = 'pyside'
import sys
from pyqode.qt import QtWidgets
from pyqode.python.backend import server
from pyqode.python.widgets import PyCodeEdit
from pyqode.python.widgets import code_edit


app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
editor = PyCodeEdit(server_script=server.__file__)
# show the PyCodeEdit module in the editor
editor.file.open(code_edit.__file__.replace('.pyc', '.py'))
window.setCentralWidget(editor)
window.show()
app.exec_()

Custom

Basic example that show you how to build a custom python code editor widget

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
A basic example that show you how to create a basic python code editor widget,
from scratch.

Editor features:
    - syntax highlighting
    - code completion (using jedi)
    - code folding
    - auto indentation
    - auto complete
    - comments mode (ctrl+/)
    - calltips mode
    - linters (pyflakes and pep8) modes + display panel
    - line number panel
    - builtin search and replace panel
"""
import logging
logging.basicConfig()
# optionally, set the qt api to use (in ['pyqt4', 'pyqt5', 'pyside'])
# import os; os.environ['QT_API'] = 'pyside'
import sys
from pyqode.qt import QtWidgets
from pyqode.python.backend import server
from pyqode.core import api, modes, panels
from pyqode.python import modes as pymodes, panels as pypanels, widgets


class MyPythonCodeEdit(widgets.PyCodeEditBase):
    def __init__(self):
        super(MyPythonCodeEdit, self).__init__()

        # starts the default pyqode.python server (which enable the jedi code
        # completion worker).
        self.backend.start(server.__file__)

        # some other modes/panels require the analyser mode, the best is to
        # install it first
        self.modes.append(pymodes.DocumentAnalyserMode())

        #--- core panels
        self.panels.append(panels.FoldingPanel())
        self.panels.append(panels.LineNumberPanel())
        self.panels.append(panels.CheckerPanel())
        self.panels.append(panels.SearchAndReplacePanel(),
                           panels.SearchAndReplacePanel.Position.BOTTOM)
        self.panels.append(panels.EncodingPanel(), api.Panel.Position.TOP)
        # add a context menu separator between editor's
        # builtin action and the python specific actions
        self.add_separator()

        #--- python specific panels
        self.panels.append(pypanels.QuickDocPanel(), api.Panel.Position.BOTTOM)

        #--- core modes
        self.modes.append(modes.CaretLineHighlighterMode())
        self.modes.append(modes.CodeCompletionMode())
        self.modes.append(modes.ExtendedSelectionMode())
        self.modes.append(modes.FileWatcherMode())
        self.modes.append(modes.OccurrencesHighlighterMode())
        self.modes.append(modes.RightMarginMode())
        self.modes.append(modes.SmartBackSpaceMode())
        self.modes.append(modes.SymbolMatcherMode())
        self.modes.append(modes.ZoomMode())

        #---  python specific modes
        self.modes.append(pymodes.CommentsMode())
        self.modes.append(pymodes.CalltipsMode())
        self.modes.append(pymodes.FrostedCheckerMode())
        self.modes.append(pymodes.PEP8CheckerMode())
        self.modes.append(pymodes.PyAutoCompleteMode())
        self.modes.append(pymodes.PyAutoIndentMode())
        self.modes.append(pymodes.PyIndenterMode())


app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
editor = MyPythonCodeEdit()
editor.file.open(__file__)
window.setCentralWidget(editor)
window.show()
app.exec_()

Modes

Auto complete

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"""
Minimal example showing the use of the PyAutoCompleteMode.
"""
import logging
logging.basicConfig(level=logging.DEBUG)
import sys

from pyqode.qt import QtWidgets
from pyqode.core.api import CodeEdit
from pyqode.python.backend import server
from pyqode.python.modes import PyAutoCompleteMode


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    editor = CodeEdit()
    editor.backend.start(server.__file__)
    editor.resize(800, 600)
    print(editor.modes.append(PyAutoCompleteMode()))
    editor.appendPlainText(
        '# Please press "("\n'
        'class Foo:\n'
        '    def bar')
    editor.show()
    app.exec_()
    editor.close()
    del editor
    del app

Auto indent

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""
Minimal example showing the use of the AutoIndentMode.
"""
import logging
logging.basicConfig(level=logging.DEBUG)
import sys

from pyqode.qt import QtWidgets
from pyqode.core.api import CodeEdit
from pyqode.python.backend import server
from pyqode.core.modes import SymbolMatcherMode
from pyqode.python.modes import PyAutoIndentMode, PythonSH


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    editor = CodeEdit()
    editor.backend.start(server.__file__)
    editor.resize(800, 600)
    # PyAutoIndent needs the matcher mode and a syntax highlighter to work
    # properly
    editor.modes.append(PythonSH(editor.document()))
    editor.modes.append(SymbolMatcherMode())
    print(editor.modes.append(PyAutoIndentMode()))
    editor.show()
    editor.appendPlainText(
        'class Foo:')
    app.exec_()
    editor.close()
    del editor
    del app

Call tips

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"""
Minimal example showing the use of the CalltipsMode.
"""
import logging
logging.basicConfig(level=logging.DEBUG)
import sys

from pyqode.qt import QtWidgets
from pyqode.core.api import CodeEdit
from pyqode.python.backend import server
from pyqode.python.modes import CalltipsMode


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    editor = CodeEdit()
    editor.backend.start(server.__file__)
    editor.resize(800, 600)
    print(editor.modes.append(CalltipsMode()))
    editor.show()
    editor.appendPlainText(
        'import os\nos.path.join')
    app.exec_()
    editor.close()
    del editor
    del app

Comment/Uncomment

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""
Minimal example showing the use of the CommentsMode.
"""
import logging
logging.basicConfig(level=logging.DEBUG)
import sys

from pyqode.qt import QtWidgets
from pyqode.core.api import CodeEdit
from pyqode.python.backend import server
from pyqode.python.modes import CommentsMode, PythonSH


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    editor = CodeEdit()
    editor.backend.start(server.__file__)
    editor.resize(800, 600)
    print(editor.modes.append(CommentsMode()))
    editor.modes.append(PythonSH(editor.document()))  # looks better
    editor.show()
    editor.appendPlainText(
'''# press Ctrl+/ to comment/uncomment selected text or the current line
class Foo:
    def bar(self):
        pass''')
    app.exec_()
    editor.close()
    del editor
    del app

Go to assignment

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""
Minimal example showing the use of the GoToAssignmentsMode.
"""
import logging
logging.basicConfig(level=logging.DEBUG)
import sys

from pyqode.qt import QtWidgets
from pyqode.core.api import CodeEdit
from pyqode.python.backend import server
from pyqode.python.modes import GoToAssignmentsMode, PythonSH


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    editor = CodeEdit()
    editor.backend.start(server.__file__)
    editor.resize(800, 600)
    print(editor.modes.append(GoToAssignmentsMode()))
    editor.modes.append(PythonSH(editor.document()))  # looks better
    editor.show()
    editor.appendPlainText(
'''press_me = 10


def spam():
    # press Ctrl+Left click on ``press_me`` to go to its definition
    print(press_me)
''')
    app.exec_()
    editor.close()
    del editor
    del app

PEP8 linter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""
Minimal example showing the use of the PEP8CheckerMode.
"""
import logging
logging.basicConfig(level=logging.DEBUG)
import sys

from pyqode.qt import QtWidgets
from pyqode.core.api import CodeEdit
from pyqode.core.panels import CheckerPanel
from pyqode.python.backend import server
from pyqode.python.modes import PEP8CheckerMode, PythonSH


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    editor = CodeEdit()
    editor.backend.start(server.__file__)
    editor.resize(800, 600)
    print(editor.modes.append(PEP8CheckerMode()))
    editor.modes.append(PythonSH(editor.document()))  # looks better
    editor.panels.append(CheckerPanel())
    editor.show()
    editor.setPlainText('class Foo:\n\n\ndef __init__(self):\n\npass',
                        'text/x-python', 'utf-8')
    app.exec_()
    editor.close()
    del editor
    del app

PyFlakes linter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""
Minimal example showing the use of the FrostedCheckerMode.

We use Frosted (a fork of pyFlakes) that provides an easier API for running
pyFlakes on strings (instead of files).
"""
import logging
from pyqode.core.panels import CheckerPanel

logging.basicConfig(level=logging.DEBUG)
import sys

from pyqode.qt import QtWidgets
from pyqode.core.api import CodeEdit
from pyqode.python.backend import server
from pyqode.python.modes import FrostedCheckerMode, PythonSH


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    editor = CodeEdit()
    editor.backend.start(server.__file__)
    editor.resize(800, 600)
    print(editor.modes.append(FrostedCheckerMode()))
    editor.modes.append(PythonSH(editor.document()))  # looks better
    editor.panels.append(CheckerPanel())
    editor.show()
    editor.setPlainText('print("foo\n', 'text/x-python', 'utf-8')
    app.exec_()
    editor.close()
    del editor
    del app

Syntax highlighter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"""
Minimal example showing the use of the PythonSH mode.
"""
import logging
logging.basicConfig(level=logging.DEBUG)
import sys

from pyqode.qt import QtWidgets
from pyqode.core.api import CodeEdit, ColorScheme
from pyqode.python.backend import server
from pyqode.python.modes import PythonSH


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    editor = CodeEdit()
    editor.backend.start(server.__file__)
    editor.resize(800, 600)
    # add a native python syntax highlighter
    sh = editor.modes.append(PythonSH(editor.document()))
    # change its color scheme (from pygments)
    sh.color_scheme = ColorScheme('monokai')
    editor.show()
    editor.file.open(__file__)
    app.exec_()
    editor.close()
    del editor
    del app

Panels

Documentation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
Minimal example showing the use of the QuickDockPanel.
"""
import logging
logging.basicConfig(level=logging.DEBUG)
import sys

from pyqode.qt import QtWidgets
from pyqode.core.api import code_edit
from pyqode.core.api import CodeEdit
from pyqode.python.backend import server
from pyqode.python.panels import QuickDocPanel


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    editor = CodeEdit()
    editor.backend.start(server.__file__)
    editor.resize(800, 600)
    # we must add the document analyser prior to adding the symbol browser
    # panel
    editor.panels.append(QuickDocPanel(), QuickDocPanel.Position.BOTTOM)
    editor.show()
    editor.setPlainText('''# Press Alt+Q on a symbol to get its documentation
import os


class Foo:
    """
    A foo class
    """

    def spam(self, eggs):
        """ spams some eggs """
        return str(eggs).lower()

foo = Foo()
os.path.join(foo.spam())
''', '', '')
    app.exec_()
    editor.close()
    del editor
    del app

Symbol browser

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""
Minimal example showing the use of the SymbolBrowserPanel.

This panel works in combination with the DocumentAnalyserMode.
"""
import logging
logging.basicConfig(level=logging.DEBUG)
import sys

from pyqode.qt import QtWidgets
from pyqode.core.api import code_edit
from pyqode.core.api import CodeEdit
from pyqode.python.backend import server
from pyqode.python.modes import DocumentAnalyserMode
from pyqode.python.panels import SymbolBrowserPanel


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    editor = CodeEdit()
    editor.backend.start(server.__file__)
    editor.resize(800, 600)
    # we must add the document analyser prior to adding the symbol browser
    # panel
    editor.modes.append(DocumentAnalyserMode(editor.document()))
    editor.panels.append(SymbolBrowserPanel(), SymbolBrowserPanel.Position.TOP)
    editor.show()
    editor.file.open(code_edit.__file__)
    app.exec_()
    editor.close()
    del editor
    del app

Widgets

Interactive console

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""
This example show you how to use the PyInteractiveConsole. We run a python
script which raises an exception. The exception traceback will be highlighted
differently than the rest of the process output and each module file path are
clickable by the user. We open an editor to show the specified module at the
specified line.
"""
import os
import sys
from pyqode.core.api import TextHelper
from pyqode.qt import QtWidgets
from pyqode.python.widgets import PyInteractiveConsole, PyCodeEdit


editor_windows = []


def open_editor(path, line):
    global editor_windows
    print(path, line)
    editor = PyCodeEdit()
    # prevent the restoration of cursor position which will reset the position
    # we will set after opening the file
    editor.file.restore_cursor = False
    editor.file.open(path)
    TextHelper(editor).goto_line(line)
    editor.show()
    editor_windows.append(editor)


app = QtWidgets.QApplication(sys.argv)
console = PyInteractiveConsole()
console.open_file_requested.connect(open_editor)
console.start_process(sys.executable, [
    os.path.join(os.getcwd(), 'interactive_process.py')])
console.show()
app.exec_()
1
2
3
4
5
6
7
8
"""
Simple interactive process used to demonstrate the use of the
PyInteractiveConsole widget.
"""
print('running an interactive process that will raise an error soon')

# traceback will be highlighted
raise RuntimeError()

Outline

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"""
This example show you how to use the PyOuytlineTreeWidget to show the structure
of a python document.

The widget works in combination with a DocumentAnalyserMode. Such a mode must
be installed on the editor widget otherwise no data will be displayed.
"""
import sys
from pyqode.qt import QtCore, QtWidgets
from pyqode.core.api import code_edit
from pyqode.python.widgets import PyCodeEdit, PyOutlineTreeWidget


app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
editor = PyCodeEdit()
editor.file.open(code_edit.__file__)
window.setCentralWidget(editor)
window.resize(800, 600)
outline = PyOutlineTreeWidget()
outline.set_editor(editor)
dock_outline = QtWidgets.QDockWidget('Outline')
dock_outline.setWidget(outline)
window.addDockWidget(QtCore.Qt.RightDockWidgetArea, dock_outline)
window.show()
app.exec_()

PyNotepad

This example is a complete but minimal python code editor application. It is too large to be included here but you should really have a look at it as this example combines nearly all the concepts exposed by pyqode.python.