Skip to main content

Light-weight python interpreter, easy to embed into Qt applications

Project description

Python PyPI Conda-Forge GitHub CI Status GitHub CI Status

About

Qonsole is a lightweight Python console for Qt applications. It's made to be easy to embed in other PySide/PyQt applications and comes with some examples that show how this can be done. The interpreter can run in a separate thread, in the UI main thread or in a gevent task. Qonsole is a fork of pyqtconsole. The two diverged at pyqtconsole v1.3.0, and it is not quite a drop-in replacement, but does implement new features and has various improvements.

Simple usage

The following snippet shows how to create a console that will execute user input in a separate thread. Be aware that long running tasks will still block the main thread due to the GIL. See the examples directory for more examples.

import sys
from threading import Thread
from PyQt5.QtWidgets import QApplication

from qonsole import PythonConsole

app = QApplication([])
console = PythonConsole()
console.show()
console.eval_in_thread()

sys.exit(app.exec_())

Embedding

  • Separate thread - Runs the interpreter in a separate thread, see the example threaded.py. Running the interpreter in a separate thread obviously limits the interaction with the Qt application. The parts of Qt that needs to be called from the main thread will not work properly, but is excellent way for having a 'plain' python console in your Qt app.

  • main thread - Runs the interpreter in the main thread, see the example inuithread.py. Makes full interaction with Qt possible, lenghty operations will of course freeze the UI (as any lenghty operation that is called from the main thread). This is a great alternative for people who does not want to use the gevent based approach but still wants full interactivity with Qt.

  • gevent - Runs the interpreter in a gevent task, see the example _gevent.py. Allows for full interactivity with Qt without special consideration (at least to some extent) for longer running processes. The best method if you want to use pyQtgraph, Matplotlib, PyMca or similar.

Features

Syntax highlighting

Syntax highlighting is provided by the https://pygments.org library. Simply pass the Pygments style string to the PythonConsole constructer like so:

console = PythonConsole(pygments_style='github-dark')

Magic commands

Commands that start with % are magic commands. This features provides IPython-like magic commands such as:

  • %pwd -- Print current working directory
  • %cd -- Change directory
  • %ls -- List directory contents
  • %who -- List variable names in the current namespace
  • %whos -- Display detailed variable information
  • %timeit -- Time the execution of a Python statement
  • %run -- Execute a Python script file
  • %clear -- Clear the console display
  • %help -- Display help message for magic commands

In addition, custom magic commands can be defined by using the add_magic_command() method. Example:

def version(args=None):
    return '3.0.1'

console = PythonConsole()
console.add_magic_command("version", version)

Which can be used like so:

   IN [0]: %version
           3.0.1

Clear console

A local method, named clear(), is available to clear the input screen and reset the line numbering. Enable it by pushing the method into the available namespace in the console:

   console.interpreter.locals["clear"] = console.clear

Shell commands

Commands entered in the console that start with ! will be executed as shell commands. The output of the command will be printed in the console. For example, on a Linux or macOS system, entering !ls -l will list the files in the current directory. Example:

   IN [0]: !ls -l
   OUT[0]: total 16546
           -rw-r--r-- 1 user user      18741 Fen  6  2026  file1.txt
           -rw-r--r-- 1 user user      18741 Feb  6  2026  file2.txt

Prompt String

By default IN [n]: and OUT [n]: are displayed before each input and output line. You can customize this through constructor arguments:

# Including the line numbers:
console = PythonConsole(inprompt="%d >", outprompt="%d <")
# Or just static:
console = PythonConsole(inprompt=">>>", outprompt="<<<")

Credits

This module depends on QtPy which provides a compatibility layer for Qt. The console is tested under both Qt5 and Qt6.

Development

  • Repository at GitHub
  • To generate coverage information for the unit tests, run pixi run "coverage run -m pytest tests/ && coverage report -m && coverage html"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qonsole-3.0.1.tar.gz (135.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

qonsole-3.0.1-py3-none-any.whl (32.5 kB view details)

Uploaded Python 3

File details

Details for the file qonsole-3.0.1.tar.gz.

File metadata

  • Download URL: qonsole-3.0.1.tar.gz
  • Upload date:
  • Size: 135.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for qonsole-3.0.1.tar.gz
Algorithm Hash digest
SHA256 31746a14c462fe01a9341396c72774cabf8d0d6fd15daf9fb52fdc8ef86cceae
MD5 232b84cea961aebaaf3e706f0da63b9f
BLAKE2b-256 4d9ead9a00f8df9eb66c5744f7c8400d0a739114fd17d24c853c07941b1ef864

See more details on using hashes here.

Provenance

The following attestation bundles were made for qonsole-3.0.1.tar.gz:

Publisher: publish.yml on jacobwilliams/qonsole

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file qonsole-3.0.1-py3-none-any.whl.

File metadata

  • Download URL: qonsole-3.0.1-py3-none-any.whl
  • Upload date:
  • Size: 32.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for qonsole-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b92532cc5c2819b2f975a0168ba3547e3370dcace3ea9121f3e67c9411604ef3
MD5 63aed4b0f30e821c591645de978e8e9e
BLAKE2b-256 d0bd4111472c7ff42a9cee848c8c66cd21c14920304681285e3a9277fc35bbe3

See more details on using hashes here.

Provenance

The following attestation bundles were made for qonsole-3.0.1-py3-none-any.whl:

Publisher: publish.yml on jacobwilliams/qonsole

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page