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.core 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.

Basic example

This is the most basic example. It starts and configures a very simple editor with only a few modes and panels.

Frontend:

 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
A simple example that shows how to setup CodeEdit.

In this example, we install a syntax highlighter mode (based on pygments), a
mode that highlights the current line and a _search and replace_ panel.

There are many other modes and panels, feel free to use this example as a
starting point to experiment.

"""
import logging
logging.basicConfig(level=logging.DEBUG)
import sys
from pyqode.core import api
from pyqode.core import modes
from pyqode.core import panels
from pyqode.qt import QtWidgets


def main():
    app = QtWidgets.QApplication(sys.argv)

    # create editor and window
    window = QtWidgets.QMainWindow()
    editor = api.CodeEdit()
    window.setCentralWidget(editor)

    # start the backend as soon as possible
    editor.backend.start('server.py')

    # append some modes and panels
    editor.modes.append(modes.CodeCompletionMode())
    editor.modes.append(modes.PygmentsSyntaxHighlighter(editor.document()))
    editor.modes.append(modes.CaretLineHighlighterMode())
    editor.panels.append(panels.SearchAndReplacePanel(),
                      api.Panel.Position.BOTTOM)

    # open a file
    editor.file.open(__file__)

    # run
    window.show()
    app.exec_()
    editor.file.close()


if __name__ == "__main__":
    main()

Backend:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Simple server which adds a DocumentWordsProvider to the CodeCompletion worker.

"""
from pyqode.core import backend

if __name__ == '__main__':
    backend.CodeCompletionWorker.providers.append(
        backend.DocumentWordsProvider())
    backend.serve_forever()

Custom actions

This example show you how to modify default actions, here we modify the shortcut and the text of the duplicate lines actions.

Note

This example shares the backend script with the Basic example.

 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This example show you how to change default action properties.

Here we change the duplicate line action to use Ctrl+Shift+Down instead of
Ctrl+D, we also chnage the text to upper case.

"""
import sys
from pyqode.qt import QtWidgets

from pyqode.core import api
from pyqode.core import modes
from pyqode.core import panels


def main():
    app = QtWidgets.QApplication(sys.argv)
    window = QtWidgets.QMainWindow()

    # configure editor (see examples/simple/basic.py)
    editor = api.CodeEdit()
    editor.file.open(__file__)
    editor.modes.append(modes.CaretLineHighlighterMode())
    editor.modes.append(modes.PygmentsSyntaxHighlighter(editor.document()))
    editor.panels.append(panels.SearchAndReplacePanel(),
                      api.Panel.Position.TOP)
    window.setCentralWidget(editor)
    window.show()

    # Change action properties
    editor.action_duplicate_line.setShortcut('Ctrl+Shift+Down')
    editor.action_duplicate_line.setText('DUPLICATE LINE')

    app.exec_()
    editor.file.close()
    del editor
    del window
    del app


if __name__ == "__main__":
    main()

Change editor properties

This example show you how to change some editor properties. Here we modify the pygments color scheme.

Note

This example shares the backend script with the Basic example.

 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
In this example, we are going to make a dark code editor widget and make it show visual
whitespaces.

"""
import sys
from pyqode.qt import QtWidgets, QtGui
from pyqode.core import api
from pyqode.core import modes
from pyqode.core import panels


def main():
    app = QtWidgets.QApplication(sys.argv)
    window = QtWidgets.QMainWindow()

    # code from the simple example
    editor = api.CodeEdit()
    editor.file.open(__file__)
    editor.modes.append(modes.CaretLineHighlighterMode())
    sh = modes.PygmentsSyntaxHighlighter(editor.document())
    editor.modes.append(sh)
    editor.panels.append(panels.SearchAndReplacePanel(),
                      api.Panel.Position.TOP)
    # make the code edit show whitespaces in dark gray
    editor.show_white_spaces = True
    editor.whitespaces_foreground = QtGui.QColor('#606020')

    # make a dark editor using the monokai theme
    sh.pygments_style = 'monokai'

    window.setCentralWidget(editor)
    window.show()

    app.exec_()

    editor.file.close()
    del editor
    del window
    del app


if __name__ == "__main__":
    main()

Notepad

This example is a complete but minimal 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.core