Esempio n. 1
0
class Ipython(object):
    def __init__(self, scripts_path=''):
        self.kernel_manager = QtInProcessKernelManager()
        self.kernel_manager.start_kernel()
        self.kernel = self.kernel_manager.kernel
        sys.stdout = self.kernel.stdout
        sys.stderr = self.kernel.stderr

        self.scripts_path = scripts_path
        self.kernel.gui = 'qt4'

        self.kernel_client = self.kernel_manager.client()
        self.kernel_client.start_channels()

        self.control = RichJupyterWidget()
        self.control.kernel_manager = self.kernel_manager
        self.control.kernel_client = self.kernel_client
        self.control.exit_requested.connect(self.stop)

        self.control.setWindowTitle("IPython shell")

        self.execute('import numpy as np')
        self.execute('from matplotlib import pyplot as plt')
        self.execute('%matplotlib')
        self.execute('')

    def __del__(self):
        self.stop()
        self.close()

    def show(self):
        self.control.show()

        self.control.setWindowState(self.control.windowState()
                                    & ~QtCore.Qt.WindowMinimized
                                    | QtCore.Qt.WindowActive)
        self.control.activateWindow()

    def stop(self):
        self.kernel_client.stop_channels()
        self.kernel_manager.shutdown_kernel()

    def close(self):
        self.control.close()

    def push(self, vardic):
        self.kernel.shell.push(vardic)

    def execute(self, cmd):
        self.control.execute(cmd)

    def run_script(self, scriptname):
        scriptpath = os.path.join(self.scripts_path, scriptname)
        return self.control.execute('run -i %s' % scriptpath)
Esempio n. 2
0
class ipython_console(base_console, QtWidgets.QWidget):

	"""
	desc:
		An IPython-based debug window.
	"""

	def __init__(self, main_window):

		"""
		desc:
			Constructor.

		arguments:
			main_window:	The main window object.
		"""

		super(ipython_console, self).__init__(main_window)
		kernel_manager = QtInProcessKernelManager()
		kernel_manager.start_kernel()
		self.kernel = kernel_manager.kernel
		self.kernel.gui = 'qt4'
		self.kernel.shell.banner1 = ''
		kernel_client = kernel_manager.client()
		kernel_client.start_channels()
		self.control = RichIPythonWidget()
		self.control.banner = self.banner()
		self.control.kernel_manager = kernel_manager
		self.control.kernel_client = kernel_client
		self.verticalLayout = QtWidgets.QVBoxLayout(self)
		self.verticalLayout.setContentsMargins(0,0,0,0)
		self.setLayout(self.verticalLayout)
		self.verticalLayout.addWidget(self.control)

	def clear(self):

		"""See base_console."""

		self.control.reset(clear=True)

	def focus(self):

		"""See base_console."""

		self.control._control.setFocus()

	def reset(self):

		"""See base_console."""

		self.kernel.shell.reset()
		self.kernel.shell.push(self.default_globals())
		self.clear()
		super(ipython_console, self).reset()

	def show_prompt(self):

		"""See base_console."""

		self.control._show_interpreter_prompt()

	def get_workspace_globals(self):

		"""See base_console."""

		return self.kernel.shell.user_global_ns.copy()

	def set_workspace_globals(self, _globals={}):

		"""See base_console."""

		self.kernel.shell.push(_globals)

	def validTheme(self, cs):

		"""
		returns:
			desc:	True if the colorscheme is valid, False otherwise.
			type:	bool
		"""

		for key in [
			u'Background',
			u'Default',
			u'Prompt in',
			u'Prompt out',
			u'Comment',
			u'Keyword',
			u'Identifier',
			u'Double-quoted string',
			u'Invalid',
			u'Number',
			u'Operator',
			]:
			if key not in cs:
				return False
		return True

	def setTheme(self):

		"""
		desc:
			Sets the theme, based on the QProgEdit settings.
		"""

		from QProgEdit import QColorScheme
		if not hasattr(QColorScheme, cfg.qProgEditColorScheme):
			debug.msg(u'Failed to set debug-output colorscheme')
			return u''
		cs = getattr(QColorScheme, cfg.qProgEditColorScheme)
		if not self.validTheme(cs):
			debug.msg(u'Invalid debug-output colorscheme')
			return u''
		self.control._highlighter.set_style(pygments_style_factory(cs))
		qss = u'''QPlainTextEdit, QTextEdit {
				background-color: %(Background)s;
				color: %(Default)s;
			}
			.in-prompt { color: %(Prompt in)s; }
			.in-prompt-number { font-weight: bold; }
			.out-prompt { color: %(Prompt out)s; }
			.out-prompt-number { font-weight: bold; }
			''' % cs
		self.control.style_sheet = qss
		self.control._control.setFont(QtGui.QFont(cfg.qProgEditFontFamily,
			cfg.qProgEditFontSize))

	def setup(self, main_window):

		"""See base_subcomponent."""

		super(ipython_console, self).setup(main_window)
		self.kernel.shell.push(self.default_globals())

	def write(self, s):

		"""See base_console."""

		self.control._append_plain_text(str(s))
		self.control._control.ensureCursorVisible()

	def execute(self, s):

		"""See base_console."""

		self.main_window.ui.dock_stdout.setVisible(True)
		self.control.execute(s)

	def focusInEvent(self, e):

		self.control.setFocus()
		e.accept()
Esempio n. 3
0
class ipython_console(base_console, QtWidgets.QWidget):
    """
	desc:
		An IPython-based debug window.
	"""
    def __init__(self, main_window):
        """
		desc:
			Constructor.

		arguments:
			main_window:	The main window object.
		"""

        super(ipython_console, self).__init__(main_window)
        kernel_manager = QtInProcessKernelManager()
        kernel_manager.start_kernel()
        self.kernel = kernel_manager.kernel
        self.kernel.gui = 'qt4'
        self.kernel.shell.banner1 = ''
        kernel_client = kernel_manager.client()
        kernel_client.start_channels()
        self.control = RichIPythonWidget()
        self.control.banner = self.banner()
        self.control.kernel_manager = kernel_manager
        self.control.kernel_client = kernel_client
        self.verticalLayout = QtWidgets.QVBoxLayout(self)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.setLayout(self.verticalLayout)
        self.verticalLayout.addWidget(self.control)

    def clear(self):
        """See base_console."""

        self.control.reset(clear=True)

    def focus(self):
        """See base_console."""

        self.control._control.setFocus()

    def reset(self):
        """See base_console."""

        self.kernel.shell.reset()
        self.kernel.shell.push(self.default_globals())
        self.clear()
        super(ipython_console, self).reset()

    def show_prompt(self):
        """See base_console."""

        self.control._show_interpreter_prompt()

    def get_workspace_globals(self):
        """See base_console."""

        return self.kernel.shell.user_global_ns.copy()

    def set_workspace_globals(self, _globals={}):
        """See base_console."""

        self.kernel.shell.push(_globals)

    def validTheme(self, cs):
        """
		returns:
			desc:	True if the colorscheme is valid, False otherwise.
			type:	bool
		"""

        for key in [
                u'Background',
                u'Default',
                u'Prompt in',
                u'Prompt out',
                u'Comment',
                u'Keyword',
                u'Identifier',
                u'Double-quoted string',
                u'Invalid',
                u'Number',
                u'Operator',
        ]:
            if key not in cs:
                return False
        return True

    def setTheme(self):
        """
		desc:
			Sets the theme, based on the QProgEdit settings.
		"""

        from QProgEdit import QColorScheme
        if not hasattr(QColorScheme, cfg.qProgEditColorScheme):
            debug.msg(u'Failed to set debug-output colorscheme')
            return u''
        cs = getattr(QColorScheme, cfg.qProgEditColorScheme)
        if not self.validTheme(cs):
            debug.msg(u'Invalid debug-output colorscheme')
            return u''
        self.control._highlighter.set_style(pygments_style_factory(cs))
        qss = u'''QPlainTextEdit, QTextEdit {
				background-color: %(Background)s;
				color: %(Default)s;
			}
			.in-prompt { color: %(Prompt in)s; }
			.in-prompt-number { font-weight: bold; }
			.out-prompt { color: %(Prompt out)s; }
			.out-prompt-number { font-weight: bold; }
			''' % cs
        self.control.style_sheet = qss
        self.control._control.setFont(
            QtGui.QFont(cfg.qProgEditFontFamily, cfg.qProgEditFontSize))

    def setup(self, main_window):
        """See base_subcomponent."""

        super(ipython_console, self).setup(main_window)
        self.kernel.shell.push(self.default_globals())

    def write(self, s):
        """See base_console."""

        self.control._append_plain_text(str(s))

    def execute(self, s):
        """See base_console."""

        self.main_window.ui.dock_stdout.setVisible(True)
        self.control.execute(s)

    def focusInEvent(self, e):

        self.control.setFocus()
        e.accept()