class QIPythonWidget_embed(QWidget): def __init__(self, parent=None, custom_banner=None, **kwargs): super(QIPythonWidget_embed, self).__init__(parent=parent, **kwargs) self.plugin = parent kernel_manager = QtKernelManager(kernel_name=USE_KERNEL) kernel_manager.start_kernel() kernel_client = kernel_manager.client() kernel_client.start_channels() self.jupyter_widget = RichJupyterWidget() self.jupyter_widget.kernel_manager = kernel_manager self.jupyter_widget.kernel_client = kernel_client if custom_banner is not None: self.jupyter_widget.banner = custom_banner layout = QHBoxLayout() layout.addWidget(self.jupyter_widget) def push_variables(self, variableDict): """ Given a dictionary containing name / value pairs, push those variables to the IPython console widget """ self.jupyter_widget.kernel_manager.kernel.shell.push(variableDict) def clear_terminal(self): """ Clears the terminal """ self.jupyter_widget._control.clear() def print_text(self, text): """ Prints some plain text to the console """ self.jupyter_widget._append_plain_text(text, True) def execute_command(self, command): """ Execute a command in the frame of the console widget """ self.jupyter_widget._execute(command, False)
class IPythonDock(Dock): def __init__(self, *args, **kwargs): super(IPythonDock, self).__init__(*args, **kwargs) self.setMinimumHeight(25) self.setMinimumWidth(500) self.setWindowTitle("IPython Shell") self.setAllowedAreas(QtCore.Qt.AllDockWidgetAreas) self.console = RichJupyterWidget(name="console") self.console.font_size = 7 self.setWidget(self.console) def setup_ipython(self, main_window): """ Sets up the ipython shell for the relevant docks. :return: """ self.console.kernel_manager = kernel_manager = \ QtInProcessKernelManager() kernel_manager.start_kernel(show_banner=True) kernel_manager.kernel.gui = 'qt' self.console.kernel_client = kernel_client = kernel_manager.client() kernel_client.start_channels() def stop(): kernel_client.stop_channels() kernel_manager.shutdown_kernel() self.console.exit_requested.connect(stop) self.push_vars({"KatilInstance": main_window}) self.console.show() self.inject_globals() self.inject_debugs() def push_vars(self, variableDict): """ Given a dictionary containing name / value pairs, push those variables to the Jupyter console widget """ self.console.kernel_manager.kernel.shell.push(variableDict) def pull_var(self, varName, delete=False): v = self.console.kernel_manager.kernel.shell.user_ns[varName] if delete: del self.console.kernel_manager.kernel.shell.user_ns[varName] return v def delete_var(self, varName): del self.console.kernel_manager.kernel.shell.user_ns[varName] def clear(self): """ Clears the terminal """ self.console._control.clear() # self.kernel_manager def print_text(self, text): """ Prints some plain text to the console """ self.console._append_plain_text(text) def execute_command(self, command): """ Execute a command in the frame of the console widget """ self.console._execute(command, False) def inject_globals(self, glbls=None): """ Inject the globals() dict into the IPython kernel under the key '_injected' :return: """ if glbls is None: glbls = globals() self.push_vars({"_injected": glbls}) self.execute_command("from PyQt5.QtWidgets import *") self.execute_command("from praatkatili import *") def inject_debugs(self): self.push_vars({'canvas': self.findChildren(PlotCanvas)})