Outdated version of the documentation. Find the latest one here.

` `

La Console Python de QGIS

As you will see later in this chapter, QGIS has been designed with a plugin architecture. Plugins can be written in Python, a very famous language in the geospatial world.

QGIS brings a Python API (see PyQGIS Developer Cookbook for some code sample) to let the user interact with its objects (layers, feature or interface). QGIS also has a Python console.

The QGIS Python Console is an interactive shell for the python command executions. It also has a python file editor that allows you to edit and save your python scripts. Both console and editor are based on PyQScintilla2 package. To open the console go to Plugins ‣ Python Console (Ctrl+Alt+P).

La console interactive

La console interactive est composée d’une barre d’outils, d’une zone d’entrée et de sortie.

Barre d’outils

La barre d’outil propose les outils suivant:

  • iconClearConsole Clear console to wipe the output area;
  • iconClassConsole Import class: Processing, PyQt4.QtCore or PyQt4.QtGui class;
  • iconRunConsole Run command available in the input area: same as pressing Enter;
  • iconeShowEditorConsole Show editor: toggles L’éditeur de code visibility;
  • iconSettingsConsole Options...;
  • iconHelpConsole Help....

Console

Les principales fonctionnalités de la console sont :

  • Auto-complétion du code, coloration et rappels syntaxiques pour les APIs suivantes:

    • Python
    • PyQGIS
    • PyQt4
    • QScintilla2
    • osgeo-gdal-ogr
  • Ctrl+Alt+Space pour voir la liste d’auto-complétion si elle est activée dans Options;

  • Exécutez des fragments de code depuis la zone de saisie en les tapant puis en appuyant sur Entrée ou Exécuter la Commande;

  • Execute code snippets from the output area using the Enter selected from the contextual menu or pressing Ctrl+E;
  • Parcourez l’historique des commandes dans la zone de saisie en utilisant les touches directionnelles Haut and Bas  et exécutez la commande que vous voulez;

  • Ctrl+Shift+Espace pour voir l’historique des commandes: un double clic sur une ligne exécutera la commande. La boîte de dialogue Historique des Commandes peut aussi être ouverte depuis le menu contextuel ou la zone de saisie;

  • Sauvegardez et videz l’historique des commandes. L’historique sera sauvegardé dans le fichier ~/.qgis2/console_history.txt;

  • Open QGIS API documentation by typing _api;
  • Open PyQGIS Cookbook by typing _pyqgis.

Astuce

Réutilisez des commandes déjà exécutées depuis la zone de sortie

Vous pouvez exécuter des fragments de code depuis la zone de sortie en les sélectionnant et en appuyant sur Ctrl+E. Peu importe que le texte sélectionné contienne les caractères d’attente de l’interpréteur (>>>,``...``).

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

La console Python

L’éditeur de code

Use the iconeShowEditorConsole 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:

  • Auto-complétion du code, coloration et rappels syntaxiques pour les APIs suivantes:

    • Python
    • PyQGIS
    • PyQt4
    • 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 Environement shorcut, 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 Enter selected command;
  • Execute the whole script with the Run script command (this creates a byte-compiled file with the extension .pyc).

Note

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

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

The Python Console editor

Options

Accessible either from the Console toolbar or the contextual menu of Console output panel or Code Editor, this adds further settings to manage and control the Python console behavior:

  • Autocompletion: If checked the code completion is enabled. You can get autocompletion from current document, from installed APIs and both from APIs and current document.
  • Autocompletion threshold: Sets the threshold to display the autocompletion list (in chars typed).
  • Automatic parentheses insertion: If checked enables the autoclosing for bracket.
  • Auto-save script before running: Allows you to save automatically the script to be executed in order to avoid to save it after any modification. This action will store a temporary file into the temporary system directory that will be automatically deleted after running.
  • Using preloaded APIs file: You can choose whether use the preload APIs file or load some APIs files saved on your system.
  • Using prepared APIs file: If checked the *.pap file will be used for code completion. To generate a prepared APIs file you have to load at least an *.api file and then compile it by clicking on [Compile Apis...] button.

Astuce

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.