| 1 | # vim: set expandtab shiftwidth=4 softtabstop=4:
|
|---|
| 2 |
|
|---|
| 3 | # === UCSF ChimeraX Copyright ===
|
|---|
| 4 | # Copyright 2016 Regents of the University of California.
|
|---|
| 5 | # All rights reserved. This software provided pursuant to a
|
|---|
| 6 | # license agreement containing restrictions on its disclosure,
|
|---|
| 7 | # duplication and use. For details see:
|
|---|
| 8 | # http://www.rbvi.ucsf.edu/chimerax/docs/licensing.html
|
|---|
| 9 | # This notice must be embedded in or attached to all copies,
|
|---|
| 10 | # including partial copies, of the software or any revisions
|
|---|
| 11 | # or derivations thereof.
|
|---|
| 12 | # === UCSF ChimeraX Copyright ===
|
|---|
| 13 |
|
|---|
| 14 | from chimerax.core.tools import ToolInstance
|
|---|
| 15 |
|
|---|
| 16 | # work around problem in ticket #3820
|
|---|
| 17 | import sys, asyncio
|
|---|
| 18 | if sys.platform == 'win32':
|
|---|
| 19 | asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
|---|
| 20 |
|
|---|
| 21 | class ShellUI(ToolInstance):
|
|---|
| 22 |
|
|---|
| 23 | # shell tool does not participate in sessions
|
|---|
| 24 | SESSION_ENDURING = True
|
|---|
| 25 |
|
|---|
| 26 | def __init__(self, session, tool_name):
|
|---|
| 27 | ToolInstance.__init__(self, session, tool_name)
|
|---|
| 28 | # 'display_name' defaults to class name with spaces inserted
|
|---|
| 29 | # between lower-then-upper-case characters (therefore "Tool UI"
|
|---|
| 30 | # in this case), so only override if different name desired
|
|---|
| 31 | self.display_name = "ChimeraX Python Shell"
|
|---|
| 32 | from chimerax.ui import MainToolWindow
|
|---|
| 33 | self.tool_window = MainToolWindow(self)
|
|---|
| 34 | parent = self.tool_window.ui_area
|
|---|
| 35 | # UI content code
|
|---|
| 36 | from ipykernel.ipkernel import IPythonKernel
|
|---|
| 37 | save_ns = IPythonKernel.user_ns
|
|---|
| 38 | IPythonKernel.user_ns = { 'session': session }
|
|---|
| 39 | from qtconsole.inprocess import QtInProcessKernelManager
|
|---|
| 40 | kernel_manager = QtInProcessKernelManager()
|
|---|
| 41 | kernel_manager.start_kernel()
|
|---|
| 42 | kernel_client = kernel_manager.client()
|
|---|
| 43 | kernel_client.start_channels()
|
|---|
| 44 |
|
|---|
| 45 | from qtconsole.rich_jupyter_widget import RichJupyterWidget
|
|---|
| 46 | self.shell = RichJupyterWidget(parent)
|
|---|
| 47 | def_banner = self.shell.banner
|
|---|
| 48 | self.shell.banner = "{}\nCurrent ChimeraX session available as 'session'.\n\n".format(
|
|---|
| 49 | def_banner)
|
|---|
| 50 | self.shell.kernel_manager = kernel_manager
|
|---|
| 51 | self.shell.kernel_client = kernel_client
|
|---|
| 52 | IPythonKernel.user_ns = save_ns
|
|---|
| 53 |
|
|---|
| 54 | from PyQt5.QtWidgets import QHBoxLayout
|
|---|
| 55 | layout = QHBoxLayout()
|
|---|
| 56 | layout.addWidget(self.shell)
|
|---|
| 57 | layout.setStretchFactor(self.shell, 1)
|
|---|
| 58 | parent.setLayout(layout)
|
|---|
| 59 | self.tool_window.manage(placement=None)
|
|---|