Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, title, icon):
DockWidget.__init__(self, core.mainWindow(), title, icon, "Alt+I")
self.setObjectName(title)
self.setAllowedAreas(Qt.BottomDockWidgetArea | Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
# Copied from https://github.com/jupyter/qtconsole/blob/master/examples/inprocess_qtconsole.py, then modified based on https://github.com/jupyter/qtconsole/blob/master/qtconsole/qtconsoleapp.py -- the QtInProcessKernelManager is blocking, so infinite loops crash Enki!
kernel_manager = QtKernelManager()
kernel_manager.start_kernel()
kernel_manager.client_factory = QtKernelClient
kernel_manager.kernel.gui = 'qt'
kernel_client = kernel_manager.client()
kernel_client.start_channels()
self.ipython_widget = RichJupyterWidget()
self.ipython_widget.kernel_manager = kernel_manager
self.ipython_widget.kernel_client = kernel_client
# By default, iPython adds a blank line between inputs. Per Monika's request, this eliminates the extra line. See https://qtconsole.readthedocs.io/en/latest/config_options.html#options; this fix was based on info from https://stackoverflow.com/questions/38652671/ipython-5-0-remove-spaces-between-input-lines.
self.ipython_widget.input_sep = ''
self.ipython_widget.show()
self.setWidget(self.ipython_widget)
self.setFocusProxy(self.ipython_widget)
def connected_console(console_class=RichIPythonWidget, **kwargs):
"""Create a console widget, connected to another kernel running in
the current process
This only works on IPython v1.0 and above
Parameters:
console_class : The class of the console widget to create
kwargs : Extra variables to put into the namespace
"""
shell = get_ipython()
if shell is None:
raise RuntimeError("There is no IPython kernel in this process")
client = QtKernelClient(connection_file=get_connection_file())
client.load_connection_file()
client.start_channels()
control = console_class()
control.kernel_client = client
control.shell = shell
control.shell.user_ns.update(**kwargs)
return control
connection_file=connection_file,
menu_actions=self.menu_actions,
hostname=hostname,
external_kernel=external_kernel,
slave=True,
show_elapsed_time=show_elapsed_time,
reset_warning=reset_warning,
ask_before_restart=ask_before_restart,
css_path=self.css_path)
# Change stderr_dir if requested
if self.test_dir is not None:
client.stderr_dir = self.test_dir
# Create kernel client
kernel_client = QtKernelClient(connection_file=connection_file)
# This is needed for issue spyder-ide/spyder#9304.
try:
kernel_client.load_connection_file()
except Exception as e:
QMessageBox.critical(self, _('Connection error'),
_("An error occurred while trying to load "
"the kernel connection file. The error "
"was:\n\n") + to_text_string(e))
return
if hostname is not None:
try:
connection_info = dict(ip = kernel_client.ip,
shell_port = kernel_client.shell_port,
iopub_port = kernel_client.iopub_port,
config_options=self.config_options(),
additional_options=self.additional_options(),
interpreter_versions=self.interpreter_versions(),
connection_file=connection_file,
menu_actions=self.menu_actions,
hostname=hostname,
external_kernel=external_kernel,
slave=True,
show_elapsed_time=show_elapsed_time,
reset_warning=reset_warning,
ask_before_restart=ask_before_restart)
if self.testing:
client.stderr_dir = self.test_dir
# Create kernel client
kernel_client = QtKernelClient(connection_file=connection_file)
kernel_client.load_connection_file()
if hostname is not None:
try:
connection_info = dict(ip = kernel_client.ip,
shell_port = kernel_client.shell_port,
iopub_port = kernel_client.iopub_port,
stdin_port = kernel_client.stdin_port,
hb_port = kernel_client.hb_port)
newports = self.tunnel_to_kernel(connection_info, hostname,
sshkey, password)
(kernel_client.shell_port,
kernel_client.iopub_port,
kernel_client.stdin_port,
kernel_client.hb_port) = newports
except Exception as e:
QMessageBox.critical(self, _('Connection error'),
self.kernel_client = kernel_client
self.shell = kernel_manager.kernel.shell
self.push = self.shell.push
elif isinstance(shell, TerminalInteractiveShell):
# if launching from an ipython terminal then adding a console is
# not supported. Instead users should use the ipython terminal for
# the same functionality.
self.kernel_client = None
self.kernel_manager = None
self.shell = None
self.push = lambda var: None
elif isinstance(shell, ZMQInteractiveShell):
# if launching from jupyter notebook, connect to the existing
# kernel
kernel_client = QtKernelClient(
connection_file=get_connection_file()
)
kernel_client.load_connection_file()
kernel_client.start_channels()
self.kernel_manager = None
self.kernel_client = kernel_client
self.shell = shell
self.push = self.shell.push
else:
raise ValueError(
'ipython shell not recognized; ' f'got {type(shell)}'
)
# Add any user variables
user_variables = user_variables or {}
self.push(user_variables)
def _createConsoleWidget(self):
if is_using_pyqt5():
layout = QtWidgets.QVBoxLayout()
else:
layout = QtGui.QVBoxLayout()
connection_file = find_connection_file(self.connection_file)
self.kernel_manager = QtKernelManager(connection_file=connection_file)
self.kernel_manager.load_connection_file()
self.kernel_manager.client_factory = QtKernelClient
self.kernel_client = self.kernel_manager.client()
self.kernel_client.start_channels()
widget_options = {}
if sys.platform.startswith('linux'):
# Some upstream bug crashes IDA when the ncurses completion is
# used. I'm not sure where the bug is exactly (IDA's Qt5 bindings?)
# but using the "droplist" instead works around the crash. The
# problem is present only on Linux.
# See: https://github.com/eset/ipyida/issues/8
widget_options["gui_completion"] = 'droplist'
widget_options.update(_user_widget_options)
self.ipython_widget = IdaRichJupyterWidget(self.parent, **widget_options)
self.ipython_widget.kernel_manager = self.kernel_manager
self.ipython_widget.kernel_client = self.kernel_client
layout.addWidget(self.ipython_widget)