Jupyter QtConsole

repository·main·Indexed 19 days ago

https://github.com/spyder-ide/qtconsole

A lightweight, GUI-enhanced terminal for interacting with Jupyter kernels. It supports rich media output, inline figures, multiline editing with syntax highlighting, and graphical calltips. The tool allows connecting to local or remote kernels via SSH tunnels and can be embedded into Qt applications using the RichJupyterWidget or QtKernelClient.

Tokens
3.6K
Snippets
18
Records
25
Agent score
64%

What's inside qtconsole

  1. Overview of Jupyter QtConsole

    main

    Jupyter QtConsole is a rich, lightweight, Qt-based console designed for working with Jupyter kernels. It provides a terminal-like experience enhanced by GUI capabilities, including:

    • Inline figures and rich media output
    • Proper multiline editing with syntax highlighting
    • Graphical calltips
    • Session export capabilities
  2. Understand Kernel and Console shutdown rules

    main

    Because multiple consoles can be connected to a single kernel, shutdown behavior depends on whether the console is local or remote:

    • Restarting the kernel: Automatically clears all local Consoles and prompts remote Consoles about the reset.
    • Shutdown: Closes all local Consoles and notifies remotes that the Kernel has been shut down.
    • Remote Consoles: Are not allowed to restart or shutdown the kernel. Shutdowns do not close remote consoles (to allow for saving work).
  3. Managing Qt object lifecycle in the REPL

    main

    When writing Qt code within the QtConsole REPL, remember that the user code (running in the kernel) is in a different process than the QtConsole frontend. Consequently, the kernel typically does not have a running QApplication instance.

    A common issue is Python's garbage collection destroying Qt objects (like windows) as soon as they lose their local Python reference. To prevent this, you must maintain a persistent reference to the object. A reliable pattern is to attach these references to a QApplication instance.

    If no application exists in the kernel, you must first create one.

    from PyQt4 import QtGui, QtCore
    
    # Initialize or retrieve the application instance
    app = QtCore.QCoreApplication.instance()
    if not app:
        # If we are in the kernel, we must create a GUI APP
        app = QtGui.QApplication([])
    
    # Create a set on the app instance to hold references
    app.references = set()
    
    def make_window():
        win = QtGui.QMainWindow()
        # Prevent garbage collection by adding to the app's reference set
        app.references.add(win)
        return win
  4. Manage multi-line input in QtConsole

    main

    By default, the Qt console executes single lines of input as soon as they are complete. To control multi-line input:

    • Force a new line: Press Ctrl-Enter at the end of a line instead of Enter. This opens a new line for input without executing the current block.
    • Force execution: At any point within a multi-line block, press Shift-Enter to execute the entire block immediately.
  5. Connect to a remote kernel via SSH tunnels

    main

    You can use SSH tunnels to securely connect to kernels on remote machines or across networks that do not permit open ports.

    Pattern 1: Kernel on a local LAN machine

    If the kernel is running on a machine (worker) listening on loopback, use the --ssh argument with the hostname of that machine and the --existing flag pointing to the connection file.

    user@client $> jupyter qtconsole --ssh=worker --existing /path/to/kernel-12345.json

    Pattern 2: Kernel behind a firewall (via a login node)

    If the kernel is on a worker machine that is only visible to a login node, the kernel must be started listening on an external interface (e.g., 0.0.0.0). Then, connect from your client via the login node:

    user@client $> jupyter qtconsole --ssh=login --ip=192.168.1.123 --existing /path/to/kernel-12345.json

    Note: The --ip should be the address of the worker as seen from the login node.

    # Example for Pattern 1
    user@client $> jupyter qtconsole --ssh=worker --existing /path/to/kernel-12345.json
  6. Start the Jupyter QtConsole

    main

    To launch the Qt console from your terminal, use the jupyter qtconsole command. The Qt console is a lightweight GUI application that provides terminal-like behavior with enhancements such as inline figures, multi-line syntax highlighting, and graphical calltips. It can connect to any Jupyter kernel.

    $ jupyter qtconsole
  7. Restart a kernel using Ctrl+.

    main
    Because of the two-process ZMQ model, the QtConsole frontend does not block input during kernel execution. You can take actions in the frontend while the Kernel is executing or even if it has crashed. To restart a kernel (including during a blocking execution), use the keyboard shortcut Ctrl+..
  8. Install Qtconsole using pip

    main

    To install Qtconsole using pip, run the following command.

    Important: Unlike conda, pip does not automatically install the required Qt bindings. You must ensure that a Qt binding (such as PyQt5, PyQt6, PySide2, or PySide6) is installed separately after installing qtconsole via pip.

    pip install qtconsole
  9. Install Qtconsole using conda

    main

    To install Qtconsole using conda, run the following command. Using conda is recommended because it automatically handles the installation of the required Qt dependencies (like PyQt5).

    conda install qtconsole
  10. Install Qt bindings for pip users

    main

    If you installed qtconsole via pip, you must manually install Qt bindings. You can install PyQt5 using pip, or use a system package manager on Linux, or use PyQt binary packages on Windows.

    pip install pyqt5
  11. Create manual SSH tunnels for Jupyter

    main

    If the built-in Jupyter SSH helpers do not work, you can manually create tunnels using standard SSH port forwarding. Use the -f -N flags to run the tunnel in the background without executing a remote command.

    To forward a local port to a remote machine: ssh <server> <localport>:<remoteip>:<remoteport> -f -N

    Note: remoteip is interpreted relative to the server.

    # Forward local port 12345 to remote port 54321 on a machine you can see
    [client] $> ssh machine 12345:127.0.0.1:54321 -f -N
    
    # Forward local port 12345 to a target on a LAN (192.168.1.16) via a login node
    [client] $> ssh login 12345:192.168.1.16:54321 -f -N