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

쌍방향 콘솔

쌍방향 콘솔은 툴바, 입력 영역 그리고 출력 영역으로 이루어져 있습니다.

툴바

툴바는 다음 도구들을 제공합니다:

  • clearConsole Clear Console to wipe the output area;

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

  • showEditorConsole Show Editor: toggles 코드 편집기 visibility;

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

  • helpContents Help…: browses the current documentation.

콘솔

콘솔의 주요 기능은 다음과 같습니다:

  • 다음 API들을 위한 코드 완성, 문법 구문 강조, 그리고 팝업 도움말:

    • 파이썬

    • PyQGIS

    • PyQt5

    • QScintilla2

    • osgeo-gdal-ogr

  • Ctrl+Alt+Space: 옵션 에서 활성화하면 사용할 수 있는 자동 완성 목록 보기

  • 입력 영역에서 Enter 키를 누르거나 |iconRunConsole| 아이콘을 클릭해서 코드 조각(snippet) 실행하기

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

  • 입력 영역에서 UpDown 방향키를 눌러 명령어 이력을 탐색하고 원하는 명령어 실행하기

  • Ctrl+Shift+Space 조합키로 명령어 이력 살펴보기: 어느 행을 더블클릭하면 해당 명령어를 실행합니다. 입력 영역에서 컨텍스트 메뉴를 통해 Command History 대화창을 열 수도 있습니다.

  • 명령어 이력 저장 및 삭제: 명령어 이력은 ~/.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.

출력 패널에서 실행한 명령어 재사용

출력 패널에서 일부 텍스트를 선택한 다음 Ctrl+E 조합키를 누르면 코드 조각을 실행할 수 있습니다. 선택한 텍스트가 인터프리터 프롬프트(>>>, ...)를 담고 있어도 상관없습니다.

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

파이썬 콘솔

코드 편집기

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:

  • 다음 API들을 위한 코드 완성, 문법 구문 강조, 그리고 팝업 도움말:

    • 파이썬

    • PyQGIS

    • PyQt5

    • QScintilla2

    • osgeo-gdal-ogr

  • Ctrl+Space: 자동 완성 목록 보기

  • http://codepad.org/ 를 통해 코드 조각 공유하기

  • Ctrl+4: 문법 확인하기

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

    • 데스크탑 환경 기본 단축키(Ctrl+GShift+Ctrl+G)를 사용해서 다음/이전 찾기

    • 검색창 입력 시 첫 번째 일치 항목을 자동으로 찾기

    • 검색창을 열 때 처음 찾은 문자열을 선택하도록 설정하기

    • Esc 키를 눌러 검색창 닫기

  • 객체 조사기: 클래스 및 함수 탐색기

  • 마우스 클릭으로 객체 정의로 가기 (객체 조사기에서)

  • 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).

참고

코드 편집기에서 스크립트 일부 또는 전체를 실행하면 콘솔의 출력 영역에 결과를 산출합니다.

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

파이썬 콘솔 편집기

옵션

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.

옵션 저장하기

콘솔 위젯 상태를 저장하려면, 닫기 버튼을 사용해서 파이썬 콘솔을 종료해야 합니다. 이렇게 하면 다음 시작 시 도형이 복구되도록 저장할 수 있습니다.