Skip to main content

Reactive signals for Python with async support

Project description

reaktiv Python Version PyPI Version License

Reactive Signals for Python with first-class async support, inspired by Angular's reactivity model.

Why reaktiv?

If you've worked with modern frontend frameworks like React, Vue, or Angular, you're familiar with the power of reactive state management. The idea is simple but transformative: when data changes, everything that depends on it updates automatically. This is the magic behind dynamic UIs and real-time systems.

But why should Python miss out on the benefits of reactivity? reaktiv brings these reactive programming advantages to your Python projects:

  • Automatic state propagation: Change a value once, and all dependent computations update automatically.
  • Efficient updates: Only the necessary parts are recomputed.
  • Async-friendly: Seamlessly integrates with Python's asyncio for managing real-time data flows.
  • Zero external dependencies: Lightweight and easy to incorporate into any project.
  • Type-safe: Fully annotated for clarity and maintainability.

Real-World Use Cases

Reactive programming isn't just a frontend paradigm. In Python, it can simplify complex backend scenarios such as:

  • Real-time data streams: Stock prices, sensor readings, or live updates.
  • User session management: Keep track of state changes without heavy manual subscription management.
  • Complex API workflows: Automatically cascade changes across related computations.

By combining these features, reaktiv provides a robust foundation for building reactive, real-time systems - whether for data streaming, live monitoring, or even Python-powered UI frameworks.

How it Works

reaktiv provides three core primitives:

  1. Signals: Store a value and notify dependents when it changes.
  2. Computed Signals: Derive values that automatically update when dependencies change.
  3. Effects: Run side effects when signals or computed signals change.

Core Concepts

graph LR
    A[Signal] -->|Value| B[Computed Signal]
    A -->|Change| C[Effect]
    B -->|Value| C
    B -->|Change| C
    C -->|Update| D[External System]
    
    classDef signal fill:#4CAF50,color:white;
    classDef computed fill:#2196F3,color:white;
    classDef effect fill:#FF9800,color:white;
    
    class A,B signal;
    class B computed;
    class C effect;

Installation

pip install reaktiv
# or with uv
uv pip install reaktiv

Quick Start

Basic Reactivity

import asyncio
from reaktiv import Signal, Effect

async def main():
    name = Signal("Alice")

    async def greet():
        print(f"Hello, {name.get()}!")

    # Create and schedule effect
    # IMPORTANT: Assign the Effect to a variable to ensure it is not garbage collected.
    greeter = Effect(greet)
    greeter.schedule()

    name.set("Bob")  # Prints: "Hello, Bob!"
    await asyncio.sleep(0)  # Process effects

asyncio.run(main())

Computed Values

from reaktiv import Signal, ComputeSignal

# Synchronous context example
price = Signal(100)
tax_rate = Signal(0.2)
total = ComputeSignal(lambda: price.get() * (1 + tax_rate.get()))

print(total.get())  # 120.0
tax_rate.set(0.25)
print(total.get())  # 125.0

Async Effects

import asyncio
from reaktiv import Signal, Effect

async def main():
    counter = Signal(0)

    async def print_counter():
        print(f"Counter value is: {counter.get()}")

    # IMPORTANT: Assign the Effect to a variable to prevent it from being garbage collected.
    counter_effect = Effect(print_counter)
    counter_effect.schedule()

    for i in range(1, 4):
        await asyncio.sleep(1)  # Simulate an asynchronous operation or delay.
        counter.set(i)

    # Wait a bit to allow the last effect to process.
    await asyncio.sleep(1)

asyncio.run(main())

Real-Time Example: Polling System

import asyncio
from reaktiv import Signal, ComputeSignal, Effect

async def main():
    candidate_a = Signal(100)
    candidate_b = Signal(100)
    
    total_votes = ComputeSignal(lambda: candidate_a.get() + candidate_b.get())
    percent_a = ComputeSignal(lambda: (candidate_a.get() / total_votes.get()) * 100)
    percent_b = ComputeSignal(lambda: (candidate_b.get() / total_votes.get()) * 100)

    async def display_results():
        print(f"Total: {total_votes.get()} | A: {candidate_a.get()} ({percent_a.get():.1f}%) | B: {candidate_b.get()} ({percent_b.get():.1f}%)")

    async def check_dominance():
        if percent_a.get() > 60:
            print("Alert: Candidate A is dominating!")
        elif percent_b.get() > 60:
            print("Alert: Candidate B is dominating!")

    # Assign effects to variables to ensure they are retained
    display_effect = Effect(display_results)
    alert_effect = Effect(check_dominance)
    
    display_effect.schedule()
    alert_effect.schedule()

    for _ in range(3):
        await asyncio.sleep(1)
        candidate_a.set(candidate_a.get() + 40)
        candidate_b.set(candidate_b.get() + 10)
    
    await asyncio.sleep(1)

asyncio.run(main())

Sample Output:

Total: 200 | A: 100 (50.0%) | B: 100 (50.0%)
Total: 250 | A: 140 (56.0%) | B: 110 (44.0%)
Total: 300 | A: 180 (60.0%) | B: 120 (40.0%)
Total: 350 | A: 220 (62.9%) | B: 130 (37.1%)
Alert: Candidate A is dominating!

Examples

You can find example scripts in the examples folder to help you get started with using this project.


Inspired by Angular Signals • Built for Python's async-first world • Made in Hamburg

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

reaktiv-0.5.0.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

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

reaktiv-0.5.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file reaktiv-0.5.0.tar.gz.

File metadata

  • Download URL: reaktiv-0.5.0.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.8

File hashes

Hashes for reaktiv-0.5.0.tar.gz
Algorithm Hash digest
SHA256 946ac1daea217ebc38589c73e11e2de489f43fc5dde154d514c90714d1f8894c
MD5 be41b7e506e95edc9526082351fa5f1a
BLAKE2b-256 776d491cf37c2823021a35d4090f589d8590444f4ded43a7d490212e0db7c698

See more details on using hashes here.

File details

Details for the file reaktiv-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: reaktiv-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.8

File hashes

Hashes for reaktiv-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 36d45206594596b8a245d6696bf16b2c5c92ca6aee794e908df2307a2c5e2790
MD5 5e611a457a7e03f85a973ff1a29a2be5
BLAKE2b-256 c4f39de43c86441cdbc10250e5ec46df9c828d527f2d97eb982a6b20044d9d2b

See more details on using hashes here.

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