Metadata-Version: 2.4
Name: jupyter-web-widgets
Version: 0.1.0
Summary: Framework-agnostic notebook widgets for Jupyter
Requires-Python: >=3.9
Description-Content-Type: text/markdown
Requires-Dist: jupyter
Requires-Dist: notebook
Requires-Dist: ipykernel
Requires-Dist: traitlets
Requires-Dist: comm
Requires-Dist: ipywidgets

# jupyter-web-widgets

`jupyter-web-widgets` is a Python package for building live widgets inside Jupyter Notebook cells.

The goal is simple:

- show interactive controls in each cell output
- bind those controls to real Python variables
- automatically re-run or refresh the cell when a value changes
- keep the widget layer framework-agnostic so it can work with web components, React, Angular, or Vue

This project is designed to make notebook UIs feel native, while still letting you choose the frontend technology you want.

---

## What this package is for

If you have a Python variable in a notebook, you should be able to attach a widget to it and edit it directly in the output area.

Example use cases:

- sliders for model parameters
- dropdowns for selecting datasets
- toggles for enabling features
- forms for live exploration
- dashboards built directly in notebook cells

When a user changes a widget, the notebook should react immediately by updating the linked Python state and reloading the cell output.

---

## Easy setup

> This package is intended to be installed with `pip` and attached to Jupyter Notebook automatically.

### Install

```bash
pip install jupyter-web-widgets
```

### Enable the extension

```python
from jupyter_web_widgets import extension

extension.connect()
print(extension.load())
```

### Use in a notebook

```python
from jupyter_web_widgets import bridge, install

install()
bridge.connect()
binding = bridge.bind("learning_rate", value=0.01, min=0.0001, max=1.0, step=0.0001)
print(binding.widget)
```

When the widget changes, the notebook state should update through the shared bridge.

---

## How it works

The package is built around a framework-agnostic widget runtime.

### Core idea

- Python owns the source of truth for notebook variables
- the browser renders widgets in the cell output area
- events flow back from the browser to Python
- changing a widget updates state and triggers a rerun or refresh

### Frontend strategy

The core implementation should use **web components** as the default widget format.

That gives you:

- a stable UI contract
- easy embedding in Jupyter output areas
- compatibility with any browser-based widget implementation

### Framework adapters

The same widget contract should also support wrappers for popular frameworks:

- React
- Angular
- Vue

These frameworks should be treated as adapters on top of the same core widget protocol, not as the core itself.

That means:

- the widget system stays framework-agnostic
- the Python API stays consistent
- framework-specific widgets can still be plugged in later

---

## How to interface Angular, React, and Vue widgets

Use one shared widget contract and keep framework code on the frontend side.

### Recommended pattern

1. Define a core widget schema in Python.
2. Serialize widget state into a framework-neutral payload.
3. Render that payload as a web component in Jupyter output.
4. Let React, Angular, or Vue act as wrapper layers around the same payload.
5. Send change events back through the same protocol regardless of framework.

### What this means in practice

- **React** widgets should mount into a shared widget host and emit updates through the common event channel.
- **Angular** widgets should be compiled as custom elements or wrapped inside a custom element host.
- **Vue** widgets should follow the same host contract and expose input/output values through the shared event bridge.

The important rule is that the notebook only sees one widget protocol. The frontend framework is an implementation detail behind that protocol.

### Suggested interface shape

- `widget.name` identifies the binding target
- `widget.value` carries the current state
- `widget.metadata` carries framework-specific config
- update events push new values back to Python

This keeps the core framework-agnostic while still allowing framework-specific UI components.

---

## Notebook bridge

The initial Python bridge is exposed as `jupyter_web_widgets.bridge`.

### What it provides

- `bridge.bind(name, value=..., **metadata)` to create a live binding
- `bridge.update(name, value)` to push a new value into the session
- `bridge.render(name)` to produce the framework-neutral widget payload
- `bridge.connect()` to attach the comm message listener
- `install()` to install the notebook reload controller

### Browser message format

The browser side uses a minimal message contract:

- `open` to create or hydrate a widget
- `update` to send a new value
- `close` to remove a widget
- `rerun` to request a notebook refresh

### Example

```python
from jupyter_web_widgets import bridge, channel, install

install()
bridge.connect()
binding = bridge.bind("epochs", value=10, min=1, max=100, step=1)
channel.update_widget(binding.widget)
print(bridge.render("epochs"))
```

This is the first step toward a real notebook integration layer and a browser communication channel.

---

## Browser renderer

The frontend asset included in the package is a minimal web component host.

It:

- renders a widget container
- displays the current widget name and value
- emits a `jww-change` event when the input changes
- emits a `jww-rerun` event when the widget requests a refresh

Framework widgets can mount inside the same host and reuse the same event contract.

---

## Notebook reload behavior

The Python side includes a reload controller for the notebook lifecycle.

### Usage

```python
from jupyter_web_widgets import controller, install

install()
controller.on_reload(lambda name: print(f"rerun requested for {name}"))
```

### What it does

- listens for `rerun` messages from the widget bridge
- lets notebook code register rerun handlers
- keeps the rerun hook separate from the widget state model

This is the scaffold for a real notebook rerun/reload integration.

---

## Demo

A runnable Jupyter notebook is available at `demos/showcase.ipynb`.

You can also run the quick demo directly in Python:

```python
from jupyter_web_widgets import demo

demo()
```

---

## Design goals

- **Easy to install**: `pip install` should be enough for most users.
- **Notebook-native**: widgets should appear directly in cell outputs.
- **Python-linked**: widgets should map to live Python variables.
- **Framework-agnostic**: the core should not depend on React, Angular, or Vue.
- **Extensible**: new widgets should be easy to add.
- **Simple user experience**: minimal setup, minimal boilerplate.

---

## Example workflow

1. Install the package.
2. Import a widget helper in a notebook cell.
3. Bind a widget to a Python variable.
4. Interact with the widget in the output area.
5. The linked cell reloads or updates automatically.

---

## Intended architecture

A clean implementation would likely include:

- **Python package**: exposes the notebook-facing API
- **Jupyter integration layer**: attaches widgets to notebook outputs
- **Widget transport**: syncs state between Python and browser
- **Web component layer**: renders the default UI widgets
- **Adapter layer**: optional React / Angular / Vue integrations

This separation keeps the project maintainable and makes the widget system reusable outside a single frontend framework.

---

## GitHub and release automation

Add this repository to GitHub, then configure a GitHub Actions workflow that publishes a new package version when you create a release or push a version tag.

Suggested release flow:

- bump the version in `pyproject.toml`
- create a Git tag such as `v0.1.0`
- let GitHub Actions build and publish the package

---

## Roadmap

Possible next steps for the project:

- create the Python package scaffold
- define the notebook integration protocol
- build the first web component widgets
- add variable binding and cell refresh support
- add framework adapters
- document advanced widget creation

---

## Contributing

Contributions are welcome once the package scaffold exists.

If you are building a widget, try to keep it:

- reusable
- framework-neutral at the core level
- easy to embed in a notebook output cell

---

## License

Add your preferred license here when the project is ready.
