python - Coopt IPython Qt Console for my PyQt Application -


i'm creating console driven qt application using python. rather implement own custom console, i'd embed ipython qt console, make responsive application. instance, keywords input console trigger actions in main application. type "dothis" in console , in window of application plot displayed.

i've seen questions along these lines: this one discusses how embed ipython qt widget application , pass through functions, although looks these functions execute in ipython kernel , not kernel of main app. there this guy, can't execute code in examples (it's 2 years old), , doesn't it's doing want either.

is there way can pass in functions or methods execute in main kernel, or @ least simulate behavior somehow communicating ipython kernel? has done before?

this came with, , far working pretty well. subclass richipythonwidget class , overload _execute method. whenever user types console, check against list of registered commands; if matches command, execute command code, otherwise pass input along default _execute method.

console code:

from ipython.qt.console.rich_ipython_widget import richipythonwidget  class commandconsole( richipythonwidget ):     """     thin wrapper around ipython's richipythonwidget. it's     main purpose register console commands , intercept     them when typed 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 _execute first checks console         input against registered commands. if finds command         executes it, otherwise passes input kernel         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 passed console execution.             s = self.commands[possible_cmd].execute()             return super(commandconsole, self)._execute( s, hidden )         else:             # default original _execute             return super(commandconsole, self)._execute(source, hidden)       def register_command( self, name, command ):         """         method links name of command (name) function         should called when typed console (command).          """         self.commands[name] = command 

example command:

from pyqt5.qtcore import pyqtsignal, qobject, qfile  class selectcommand( qobject ):     """     select command class.      """     # signal emitted whenever command executed.     executed = pyqtsignal( str, dict, name = "selectexecuted" )      # command typed console.     name = "select"      def execute(self):         """         method executed whenever ``name`` command issued         in console.          """         name = "data description"         data = { "data dict" : 0 }         # signal sent qt models         self.executed.emit( name, data )         # code executed in console.         return 'print("the select command has been executed")' 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -