inputipythonpyqt4jupyterqtconsole

How to change ipython qtconsole input


I'm making a guide with pyqt and I'm including an ipython qtconsole widget.

try:
    from qtconsole.rich_jupyter_widget import RichJupyterWidget as ipythonWidget
    from qtconsole.inprocess import QtInProcessKernelManager
except:
    from IPython.qt.console.rich_ipython_widget import RichIPythonWidget as ipythonWidget
    from IPython.qt.inprocess import QtInProcessKernelManager

I want to modify the qtconsole input from my code but is not working. I've tried the set_next_input function but it doesn't work and I can't find another function I can use to acomplish what I want. Is even possible to achieve what I want? and if so, how can I do it?

Here is my code:

try:
    from qtconsole.rich_jupyter_widget import RichJupyterWidget as ipythonWidget
    from qtconsole.inprocess import QtInProcessKernelManager
except:
    from IPython.qt.console.rich_ipython_widget import RichIPythonWidget as ipythonWidget
    from IPython.qt.inprocess import QtInProcessKernelManager

import sys
from PyQt4 import QtGui

class sympyIpython(QtGui.QWidget):

    def __init__(self):
        super().__init__()

        self.ipython = IpythonWidget()
        v = QtGui.QVBoxLayout(self)
        button = QtGui.QPushButton('append to input')
        v.addWidget(self.ipython)
        v.addWidget(button)
        button.clicked.connect(self.symClicked)

    def symClicked(self):
        self.ipython.kernel.shell.set_next_input(' appended text')

class IpythonWidget(ipythonWidget):

    def __init__(self):
        super().__init__()
        self.kernel_manager = QtInProcessKernelManager()
        self.kernel_manager.start_kernel()
        self.kernel = self.kernel_manager.kernel
        self.kernel.gui = 'qt4'
        self.kernel_client = self.kernel_manager.client()
        self.kernel_client.start_channels()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    m = sympyIpython()
    m.show()
    sys.exit(app.exec_())

Solution

  • Reposting as an answer:

    To change the text at the prompt in the Qt console, set input_buffer on the widget object:

    jupyter_widget.input_buffer = 'text'