Skip to main content

pgwidgets-python logo

pgwidgets — Python Bindings

Python bindings for the pgwidgets JavaScript widget library. Build desktop-style browser UIs from Python with a familiar Qt/GTK-style API.

Documentation

Full documentation is available at pgwidgets-python.readthedocs.io.

Installation

pip install pgwidgets-python

This will also install pgwidgets-js (the JavaScript assets) and websockets as dependencies.

Quick Start

from pgwidgets.sync import Application

app = Application()

@app.on_connect
def setup(session):
    Widgets = session.get_widgets()

    top = Widgets.TopLevel(title="Hello", resizable=True)
    top.resize(400, 300)

    vbox = Widgets.VBox(spacing=8, padding=10)
    btn = Widgets.Button("Click me")
    label = Widgets.Label("Ready")

    btn.on("activated", lambda: label.set_text("Clicked!"))

    vbox.add_widget(btn, 0)
    vbox.add_widget(label, 1)
    top.set_widget(vbox)
    top.show()

app.run()

Run the script, then open the printed URL in your browser.

Sync vs Async

Both APIs provide the same widget classes and methods.

Synchronous (recommended for most use cases):

from pgwidgets.sync import Application
app = Application()

@app.on_connect
def setup(session):
    Widgets = session.get_widgets()
    btn = Widgets.Button("Click")      # blocking call
    btn.set_text("New text")           # blocking call

app.run()

Asynchronous (for asyncio applications):

from pgwidgets.async_ import Application
app = Application()

@app.on_connect
async def setup(session):
    Widgets = session.get_widgets()
    btn = await Widgets.Button("Click")    # awaitable
    await btn.set_text("New text")         # awaitable

await app.run()

How It Works

The Application class starts two servers:

  • An HTTP server (default port 9501) that serves the pgwidgets JS/CSS and a connector page
  • A WebSocket server (default port 9500) for the JSON command protocol

When you open the URL in a browser, the page loads pgwidgets and connects back over WebSocket. Python widget constructors and method calls are translated to JSON messages and executed in the browser. Callbacks are forwarded back to Python.

Sessions and Reconnection

Sessions persist independently of browser connections. When a browser disconnects (page refresh, network drop, tab close), the session and its widget tree remain alive on the Python side. When the browser reconnects, the entire UI is automatically reconstructed.

app = Application(max_sessions=4, logger=logger)

@app.on_connect
def setup(session):
    Widgets = session.get_widgets()
    # Build your UI...
    # If the browser refreshes, this UI is reconstructed automatically.

Key features:

  • Automatic reconstruction -- refresh the browser and the UI reappears in its current state (widget positions, text, slider values, etc.).
  • Multi-browser support -- open the same session URL in a second browser tab or window. Both browsers show the same UI and stay synchronized. Widget state changes (slider moves, tab switches, tree expand/collapse) are pushed to all connected browsers in real time.
  • Headless sessions -- create sessions without a browser using app.create_session(), build the widget tree, then connect a browser later to see the pre-built UI.

License

BSD 3-Clause

Download files

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

Source Distribution

pgwidgets_python-0.3.4.tar.gz (2.6 MB view details)

Uploaded Source

Built Distribution

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

pgwidgets_python-0.3.4-py3-none-any.whl (2.5 MB view details)

Uploaded Python 3

File details

Details for the file pgwidgets_python-0.3.4.tar.gz.

File metadata

  • Download URL: pgwidgets_python-0.3.4.tar.gz
  • Upload date:
  • Size: 2.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for pgwidgets_python-0.3.4.tar.gz
Algorithm Hash digest
SHA256 8e619f6e79a96368fde770d4491a8c8e57be02859fbe013d0ecbfabd65509b0d
MD5 a04ee10354b43c9e6308657dfdb38cbd
BLAKE2b-256 8273cff3ea595af91544e844e26ccf9c181969b9c65cf1b2484b2a5b8fd682b9

See more details on using hashes here.

File details

Details for the file pgwidgets_python-0.3.4-py3-none-any.whl.

File metadata

File hashes

Hashes for pgwidgets_python-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e144e36ec2f15c8e370c180e7f6a9fbd6299943453b569fb2e480090b2dbaf60
MD5 44077c19e00bed979cd837b1f83e3e40
BLAKE2b-256 462daaadf421d21a133bd03db975bc553779d0ebf0adf59fc4dcd7694d8f4243

See more details on using hashes here.

Release history Release notifications | RSS feed

0.4.1

2 files

0.4.0

2 files

0.3.5

2 files

This release

0.3.4 This release

2 files

0.3.3

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

0.2.3

2 files

0.2.1

2 files

0.2.0

2 files

0.1.3

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page