Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

signals

primitives for transparent reactive programming in python

install

pip install signals

usage

from signals import Signal, computed, effect

a = Signal(0)
b = Signal(2)
c = computed(lambda: a() + b())

print(c()) # 2

a.set(1)
print(c()) # 3

b.set(3)
print(c()) # 4

# Log the values of a, b, c whenever one changes
@effect
def log_abc():
    print(a(), b(), c())

a.set(2) # prints (2, 3, 5)

cell magic

we also provide a ipython cell magic %%effect, which offers a convenient way re-execute cells that use signals.

In[1]:

%load_ext signals
from signals import Signal

a = Signal(0)
b = Signal(2)

In[2]:

%%effect
a() + b() # re-evaluates the cell whenever a or b changes

In[3]:

a.set(1)

what

Signals are a declarative programming model for updating based on fine-grained changes. With signals, application state is represented as a directed graph of relationships between other signals. However, the most important part of signals is that you don't need to manage the graph yourself.

Instead, you declare signals and their relationships, and the signal system automatically tracks dependencies and executes necessary computations when values change. Singal-like constructs have been adopted by popular UI libraries and non-UI contexts (e.g., build systems to avoid uneccessary rebuilds).

why

Signals are an easier way to manage state. We need something in Python other than callbacks and events. This repo is a playground to explore patterns for using a signal-based system in Python.

ideas

Signals for widgets

@anywidget.dataclass
class Counter:
    count: int

a = Counter(count=0) # creates a signal internally

shared_count = Signal(0)
b = Counter(count=shared_count) # creates a signal internally
c = Counter(count=shared_count) # creates a signal internally

# behind the scenes, creating Counter creates an effect to update the frontend view
# when the count changes. E.g.,
# @effect
# def update():
#     self.comm.send('update', self.count)
#
# Updates b/c views because they share a signal, and have separate effects
shared_count.set(shared_count() + 1)

development

this project uses rye for development.

rye lint # lints code
rye fmt  # formats code
rye test # runs tests

Download files

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

Source Distribution

signals-0.1.0a2.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

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

signals-0.1.0a2-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file signals-0.1.0a2.tar.gz.

File metadata

  • Download URL: signals-0.1.0a2.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for signals-0.1.0a2.tar.gz
Algorithm Hash digest
SHA256 57b9977c5e3b4077df1803deffaf1196622a6358249ba79d6249c01a30d913b3
MD5 be64a09e4654562c723c2d6e037c643c
BLAKE2b-256 d221ca654f93bb55e85edf572bc2ed56f6083b59ccc0e65bbb15da9df49c10fd

See more details on using hashes here.

File details

Details for the file signals-0.1.0a2-py3-none-any.whl.

File metadata

  • Download URL: signals-0.1.0a2-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for signals-0.1.0a2-py3-none-any.whl
Algorithm Hash digest
SHA256 3c99ab3925a9328c285d20584a87d64ad0859f4c0009aa4c66f99cceb6e6347a
MD5 3fe834e76ec6557f592f9eed0bac9a9b
BLAKE2b-256 79a1ec77c71167a77f03f08a728dc576f00f99875dcb04310df759ad5a8c0d9a

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.0

2 files

This release

0.1.0a2 This release

2 files

0.0.2

1 file

0.0.1

1 file

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