This is what I came up with, and so far it is working pretty well. I subclass the RichIPythonWidget class and overload the _execute
method. Whenever the user types something into the console, I check it against a list of registered commands; if it matches a command, then I execute the command code, otherwise I just pass the input along to the default _execute
method.
Console Code:
from IPython.qt.console.rich_ipython_widget import RichIPythonWidget
class CommandConsole( RichIPythonWidget ):
"""
This is a thin wrapper around IPython's RichIPythonWidget. It's
main purpose is to register console commands and intercept
them when typed into the console.
"""
def __init__(self, *args, **kw ):
kw['kind'] = 'cc'
super(CommandConsole, self).__init__(*args, **kw)
self.commands = {}
def _execute(self, source, hidden):
"""
Overloaded version of the _execute first checks the console
input against registered commands. If it finds a command it
executes it, otherwise it passes the input to the back kernel
for processing.
"""
try:
possible_cmd = source.split()[0].strip()
except:
return super(CommandConsole, self)._execute("pass", hidden)
if possible_cmd in self.commands.keys():
# Commands return code that is passed to the console for execution.
s = self.commands[possible_cmd].execute()
return super(CommandConsole, self)._execute( s, hidden )
else:
# Default back to the original _execute
return super(CommandConsole, self)._execute(source, hidden)
def register_command( self, name, command ):
"""
This method links the name of a command (name) to the function
that should be called when it is typed into the console (command).
"""
self.commands[name] = command
Example Command:
from PyQt5.QtCore import pyqtSignal, QObject, QFile
class SelectCommand( QObject ):
"""
The Select command class.
"""
# This signal is emitted whenever the command is executed.
executed = pyqtSignal( str, dict, name = "selectExecuted" )
# This is the command as typed into the console.
name = "select"
def execute(self):
"""
This method is executed whenever the ``name`` command is issued
in the console.
"""
name = "data description"
data = { "data dict" : 0 }
# The signal is sent to my Qt Models
self.executed.emit( name, data )
# This code is executed in the console.
return 'print("the select command has been executed")'