Skip to content

BaseIPythonWidget

Qt Base Class: QWidget

Signature: QWidget(self, parent: Optional[PySide6.QtWidgets.QWidget] = None, f: PySide6.QtCore.Qt.WindowType = Default(Qt.WindowFlags)) -> None

Base classes

Name Children Inherits
RichJupyterWidget
qtconsole.rich_jupyter_widget
An JupyterWidget that supports rich text, including lists, images, and
WidgetMixin
prettyqt.widgets.widget

Subclasses

Class Module Description
InProcessIPythonWidget prettyqt.ipython.inprocessipythonwidget Convenience class for a live IPython console widget.
OutOfProcessIPythonWidget prettyqt.ipython.outofprocessipythonwidget Convenience class for a live IPython console widget running out-of-process.

⋔ Inheritance diagram

graph TD
  1473572106368["ipython.BaseIPythonWidget"]
  1473572135648["rich_jupyter_widget.RichJupyterWidget"]
  1473572112224["rich_jupyter_widget.RichIPythonWidget"]
  1473572108320["jupyter_widget.JupyterWidget"]
  1473572131744["jupyter_widget.IPythonWidget"]
  1473572128816["frontend_widget.FrontendWidget"]
  1473572122960["history_console_widget.HistoryConsoleWidget"]
  1473572121008["console_widget.ConsoleWidget"]
  1473572116128["util.NewBase"]
  1473369459712["configurable.LoggingConfigurable"]
  1473369470448["configurable.Configurable"]
  1473369367968["traitlets.HasTraits"]
  1473369366992["traitlets.HasDescriptors"]
  140713234304496["builtins.object"]
  1473572123936["util.SuperQClass"]
  1473290849680["QtWidgets.QWidget"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473300082368["QtGui.QPaintDevice"]
  1473572040000["base_frontend_mixin.BaseFrontendMixin"]
  1473293688240["widgets.WidgetMixin"]
  1473299815024["core.ObjectMixin"]
  1473245548480["gui.PaintDeviceMixin"]
  1473572135648 --> 1473572106368
  1473572112224 --> 1473572135648
  1473572108320 --> 1473572112224
  1473572131744 --> 1473572108320
  1473572128816 --> 1473572131744
  1473572122960 --> 1473572128816
  1473572121008 --> 1473572122960
  1473572116128 --> 1473572121008
  1473369459712 --> 1473572116128
  1473369470448 --> 1473369459712
  1473369367968 --> 1473369470448
  1473369366992 --> 1473369367968
  140713234304496 --> 1473369366992
  1473572123936 --> 1473572116128
  1473290849680 --> 1473572123936
  1473288842240 --> 1473290849680
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208
  1473300082368 --> 1473290849680
  1473291690208 --> 1473300082368
  1473572040000 --> 1473572128816
  140713234304496 --> 1473572040000
  1473293688240 --> 1473572106368
  1473299815024 --> 1473293688240
  140713234304496 --> 1473299815024
  1473245548480 --> 1473293688240
  140713234304496 --> 1473245548480

🛈 DocStrings

Bases: RichJupyterWidget, WidgetMixin

Convenience class for a live IPython console widget running out-of-process.

Source code in prettyqt\ipython\baseipythonwidget.py
class BaseIPythonWidget(RichJupyterWidget, widgets.WidgetMixin):
    """Convenience class for a live IPython console widget running out-of-process."""

    code_executed = core.Signal(str)

    def __init__(self, *args, **kwargs):
        super().__init__(
            gui_completion="droplist",  # 'plain', 'droplist', 'ncurses'
            kind="rich",  # 'plain', 'rich', only applies when no custom control set.
            paging="vsplit",  # h  'inside', 'hsplit', 'vsplit', 'custom', 'none'
            # custom_control=widgets.TextEdit,
            # custom_page_control=widgets.TextEdit,
        )
        self.banner = "IPython Console"
        self.font_size = 6
        # self.gui_completion = "droplist"
        widgets.Application.call_on_exit(self.shutdown)
        # self.exit_requested.connect(self.shutdown)

        widgets.Application.styleHints().colorSchemeChanged.connect(
            self.adjust_style_to_palette
        )
        self.adjust_style_to_palette()

    @classmethod
    def setup_example(cls):
        return None

    def set_completion_mode(self, mode: CompletionmodeStr):
        self._set_completion_widget(mode)

    def get_completion_mode(self) -> CompletionmodeStr:
        return self.gui_completion

    @property
    def texteditor(self) -> widgets.QPlainTextEdit:  # to shut up linters
        return self._control

    def adjust_style_to_palette(self):
        """Adjust coloring of the terminal to current palette."""
        pal = widgets.Application.get_palette()
        style = "linux" if pal.is_dark() else "lightbg"
        self.set_default_style(style)

    def shutdown(self):
        """Stop IPython and cleanup."""
        return NotImplemented

    def buffer(self) -> str:
        """Get current code block."""
        return self.input_buffer

    def set_buffer(self, code: str) -> None:
        """Set code string to Jupyter QtConsole buffer."""
        self.input_buffer = ""
        if not isinstance(code, str):
            raise ValueError(f"Cannot set {type(code)}.")
        cursor = self.texteditor.textCursor()
        lines = code.split("\n")
        for line in lines[:-1]:
            cursor.insertText(line + "\n")
            self._insert_continuation_prompt(cursor)  # insert "...:"
        cursor.insertText(lines[-1])
        return None

    def insert_text(self, text: str) -> None:
        cursor = self.texteditor.textCursor()
        cursor.insertText(text)
        return None

    def set_temp_text(self, text: str) -> None:
        cursor = self.texteditor.textCursor()
        cursor.removeSelectedText()
        pos = cursor.position()
        cursor.insertText(text)
        cursor.setPosition(pos)
        cursor.setPosition(pos + len(text), gui.TextCursor.MoveMode.KeepAnchor)
        self.texteditor.setTextCursor(cursor)
        return None

    def get_selected_text(self) -> str:
        """Return the selected text."""
        cursor = self.texteditor.textCursor()
        return cursor.selection().toPlainText()

    def clear(self):
        """Clear the terminal."""
        self.texteditor.clear()

    def print_text(self, text: str, before_prompt: bool = False):
        """Print some plain text to the console."""
        self._append_plain_text(text)

    def execute(
        self,
        source: str | None = None,
        hidden: bool = False,
        interactive: bool = False,
    ):
        """Execute a command in the frame of the console widget."""
        if source is None:
            source = self.input_buffer
        super().execute(source=source, hidden=hidden, interactive=interactive)
        self.code_executed.emit(source)
        return None

    completion_mode = core.Property(
        str,
        get_completion_mode,
        set_completion_mode,
        doc="Completion mode for the widget",
    )

adjust_style_to_palette()

Adjust coloring of the terminal to current palette.

Source code in prettyqt\ipython\baseipythonwidget.py
def adjust_style_to_palette(self):
    """Adjust coloring of the terminal to current palette."""
    pal = widgets.Application.get_palette()
    style = "linux" if pal.is_dark() else "lightbg"
    self.set_default_style(style)

buffer() -> str

Get current code block.

Source code in prettyqt\ipython\baseipythonwidget.py
def buffer(self) -> str:
    """Get current code block."""
    return self.input_buffer

clear()

Clear the terminal.

Source code in prettyqt\ipython\baseipythonwidget.py
def clear(self):
    """Clear the terminal."""
    self.texteditor.clear()

execute(source: str | None = None, hidden: bool = False, interactive: bool = False)

Execute a command in the frame of the console widget.

Source code in prettyqt\ipython\baseipythonwidget.py
def execute(
    self,
    source: str | None = None,
    hidden: bool = False,
    interactive: bool = False,
):
    """Execute a command in the frame of the console widget."""
    if source is None:
        source = self.input_buffer
    super().execute(source=source, hidden=hidden, interactive=interactive)
    self.code_executed.emit(source)
    return None

get_selected_text() -> str

Return the selected text.

Source code in prettyqt\ipython\baseipythonwidget.py
def get_selected_text(self) -> str:
    """Return the selected text."""
    cursor = self.texteditor.textCursor()
    return cursor.selection().toPlainText()

print_text(text: str, before_prompt: bool = False)

Print some plain text to the console.

Source code in prettyqt\ipython\baseipythonwidget.py
def print_text(self, text: str, before_prompt: bool = False):
    """Print some plain text to the console."""
    self._append_plain_text(text)

set_buffer(code: str) -> None

Set code string to Jupyter QtConsole buffer.

Source code in prettyqt\ipython\baseipythonwidget.py
def set_buffer(self, code: str) -> None:
    """Set code string to Jupyter QtConsole buffer."""
    self.input_buffer = ""
    if not isinstance(code, str):
        raise ValueError(f"Cannot set {type(code)}.")
    cursor = self.texteditor.textCursor()
    lines = code.split("\n")
    for line in lines[:-1]:
        cursor.insertText(line + "\n")
        self._insert_continuation_prompt(cursor)  # insert "...:"
    cursor.insertText(lines[-1])
    return None

shutdown()

Stop IPython and cleanup.

Source code in prettyqt\ipython\baseipythonwidget.py
def shutdown(self):
    """Stop IPython and cleanup."""
    return NotImplemented

⌗ Property table

Qt Property Type Doc
objectName QString
modal bool
windowModality Qt::WindowModality
enabled bool
geometry QRect
frameGeometry QRect
normalGeometry QRect
x int
y int
pos QPoint
frameSize QSize
size QSize
width int
height int
rect QRect
childrenRect QRect
childrenRegion QRegion
sizePolicy QSizePolicy
minimumSize QSize
maximumSize QSize
minimumWidth int
minimumHeight int
maximumWidth int
maximumHeight int
sizeIncrement QSize
baseSize QSize
palette QPalette
font QFont
cursor QCursor
mouseTracking bool
tabletTracking bool
isActiveWindow bool
focusPolicy Qt::FocusPolicy
focus bool
contextMenuPolicy Qt::ContextMenuPolicy
updatesEnabled bool
visible bool
minimized bool
maximized bool
fullScreen bool
sizeHint QSize
minimumSizeHint QSize
acceptDrops bool
windowTitle QString
windowIcon QIcon
windowIconText QString
windowOpacity double
windowModified bool
toolTip QString
toolTipDuration int
statusTip QString
whatsThis QString
accessibleName QString
accessibleDescription QString
layoutDirection Qt::LayoutDirection
autoFillBackground bool
styleSheet QString
locale QLocale
windowFilePath QString
inputMethodHints QFlags
completion_mode QString Completion mode for the widget

🖼 Screenshot