Skip to content

Instantly share code, notes, and snippets.

@Sharpie
Created February 9, 2011 05:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sharpie/817914 to your computer and use it in GitHub Desktop.
Save Sharpie/817914 to your computer and use it in GitHub Desktop.
Main application for a pure PyQGIS app running an IPython console
"""
/***************************************************************************
ipythonDialog
A QGIS plugin
An enhanced QGIS console powered by IPython
-------------------
begin : 2011-02-06
copyright : (C) 2011 by Charlie Sharpsteen
email : source@sharpsteen.net
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import sys
# IPython needs sys.argv to be defined
if not hasattr(sys, 'argv'):
sys.argv = []
from PyQt4.QtCore import Qt, QCoreApplication
from PyQt4.QtGui import QDockWidget, QWidget, QVBoxLayout
from IPython.frontend.qt.console.ipython_widget import IPythonWidget
from IPython.frontend.qt.kernelmanager import QtKernelManager
from IPython.utils.localinterfaces import LOCALHOST
class IPythonConsole(QDockWidget):
def __init__(self, parent=None):
QDockWidget.__init__(self, parent)
self.setObjectName("IPython Console")
self.setWindowTitle(QCoreApplication.translate("IPython Console",
"IPython Console"))
self.setAllowedAreas(Qt.BottomDockWidgetArea)
self.container = QWidget()
self.layout = QVBoxLayout(self.container)
if sys.platform == 'darwin':
import subprocess
# This is here because QGIS defines sys.executable to be the QGIS
# app on OS X and not a Python interpreter.
sys.executable = subprocess.check_output(
['which', 'python']).rstrip()
self.kernel_manager = QtKernelManager()
self.kernel_manager.start_kernel()
self.kernel_manager.start_channels()
self.console = IPythonWidget(local_kernel=LOCALHOST)
self.console.kernel_manager = self.kernel_manager
self.layout.addWidget(self.console)
self.setWidget(self.container)
#!/usr/bin/env python
import sys
import os
from qgis.core import *
from qgis.gui import *
from PyQt4.QtGui import QAction, QMainWindow, QApplication
from PyQt4.QtCore import SIGNAL, Qt, QString
from console import IPythonConsole
class MainWin(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.resize(1000,1000)
self.canvas = QgsMapCanvas()
self.canvas.setCanvasColor(Qt.white)
self.setCentralWidget(self.canvas)
self.console = IPythonConsole()
self.addDockWidget(Qt.BottomDockWidgetArea, self.console)
def main():
QgsApplication.setPrefixPath(os.environ['QGISPATH'], True)
QgsApplication.initQgis()
app = QApplication(sys.argv)
main_win = MainWin()
main_win.show()
app.exec_()
QgsApplication.exitQgis()
if __name__ == '__main__':
if not os.environ.has_key('QGISPATH'):
# Assuming you are running some flavor of Linux as a shot in the dark
os.environ['QGISPATH'] = '/usr'
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment