24.3. Console python di QGIS

Come vedrai più avanti in questo capitolo, QGIS è stato progettato con un’architettura a plugin. I plugin possono essere scritti in Python, una lingua molto famosa nel mondo geospaziale.

QGIS ha un’API Python (vedi PyQGIS Developer Cookbook per alcuni esempi di codice) per permettere all’utente di interagire con i suoi oggetti (layer, elementi o interfacce). QGIS ha anche una console Python.

La Console QGIS Python è una shell interattiva per l’esecuzione dei comandi python. Ha anche un editor di file python che permette di modificare e salvare i tuoi script python. Sia la console che l’editor sono basati sul package PyQScintilla2. Per aprire la console vai in Plugins ▶ Console Python (Ctrl+Alt+P).

24.3.1. The Interactive Console

The interactive console is composed of a toolbar, an input area and an output one.

24.3.1.1. Barra degli Strumenti

The toolbar proposes the following tools:

  • clearConsole Clear Console to wipe the output area;

  • runConsole Run Command available in the input area: same as pressing Enter;

  • showEditorConsole Show Editor: toggles The Code Editor visibility;

  • options Options…: opens a dialog to configure console properties (see Opzioni);

  • helpContents Help…: browses the current documentation.

24.3.1.2. Console

The console main features are:

  • Code completion, highlighting syntax and calltips for the following APIs:

    • Python

    • PyQGIS

    • PyQt5

    • QScintilla2

    • osgeo-gdal-ogr

  • Ctrl+Alt+Space to view the auto-completion list if enabled in the Opzioni;

  • Execute code snippets from the input area by typing and pressing Enter or Run Command;

  • Execute code snippets from the output area using the Enter Selected from the contextual menu or pressing Ctrl+E;

  • Browse the command history from the input area using the Up and Down arrow keys and execute the command you want;

  • Ctrl+Shift+Space to view the command history: double-clicking a row will execute the command. The Command History dialog can also be accessed from context menu of input area;

  • Save and clear the command history. The history will be saved into the file ~/.qgis2/console_history.txt;

  • Open QGIS C++ API documentation by typing _api;

  • Open QGIS Python API documentation by typing _pyqgis.

  • Open PyQGIS Cookbook by typing _cookbook.

Suggerimento

Reuse executed commands from the output panel

You can execute code snippets from the output panel by selecting some text and pressing Ctrl+E. No matter if selected text contains the interpreter prompt (>>>, ...).

../../../_images/python_console.png

Fig. 24.29 The Python Console

24.3.2. The Code Editor

Use the showEditorConsole Show Editor button to enable the editor widget. It allows editing and saving Python files and offers advanced functionalities to manage your code (comment and uncomment code, check syntax, share the code via codepad.org and much more). Main features are:

  • Code completion, highlighting syntax and calltips for the following APIs:

    • Python

    • PyQGIS

    • PyQt5

    • QScintilla2

    • osgeo-gdal-ogr

  • Ctrl+Space to view the auto-completion list.

  • Sharing code snippets via codepad.org.

  • Ctrl+4 Syntax check.

  • Search bar (open it with the default Desktop Environment shortcut, usually Ctrl+F):

    • Use the default Desktop Environment shortcut to find next/previous (Ctrl+G and Shift+Ctrl+G);

    • Automatically find first match when typing in find box;

    • Set initial find string to selection when opening find;

    • Pressing Esc closes the find bar.

  • Object inspector: a class and function browser;

  • Go to an object definition with a mouse click (from Object inspector);

  • Execute code snippets with the runConsole Run Selected command in contextual menu;

  • Execute the whole script with the start Run Script command (this creates a byte-compiled file with the extension .pyc).

Nota

Running partially or totally a script from the Code Editor outputs the result in the Console output area.

../../../_images/python_console_editor.png

Fig. 24.30 The Python Console editor

24.3.3. Opzioni

Accessible from the Console toolbar and the contextual menus of the Console output panel and the Code Editor, the Python Console Settings help manage and control the Python console behavior.

For both Console and Editor you can specify:

  • Autocompletion: Enables code completion. You can get autocompletion from the current document, the installed API files or both.

    • Autocompletion threshold: Sets the threshold for displaying the autocompletion list (in characters)

  • Typing

    • Automatic parentheses insertion: Enables autoclosing for parentheses

    • Automatic insertion of the “import” string on “from xxx”: Enables insertion of “import” when specifying imports

For Editor you can also specify:

  • Run and Debug

    • Enable Object Inspector (switching between tabs may be slow): Enable the object inspector.

    • Auto-save script before running: Saves the script automatically when executed. This action will store a temporary file (in the temporary system directory) that will be deleted automatically after running.

  • Font and Colors: Here you can specify the font to use in the editor and the colors to use for highlighting

For APIs you can specify:

  • Using preloaded APIs file: You can choose if you would like to use the preloaded API files. If this is not checked you can add API files and you can also choose if you would like to use prepared API files (see next option).

  • Using prepared APIs file: If checked, the chosen *.pap file will be used for code completion. To generate a prepared API file you have to load at least one *.api file and then compile it by clicking the Compile APIs… button.

Suggerimento

Save the options

To save the state of console’s widgets you have to close the Python Console from the close button. This allows you to save the geometry to be restored to the next start.