Skip to main content

⚛️ pyreaction

License: MIT Python 3.11+ Typed with mypy Status: pre-release 0.1

pyreaction brings React's component model to Python. Components run on the server; the browser gets server-rendered HTML for the first paint, then small DOM updates over a WebSocket. No JavaScript to write, no build step, no Node.

from reaction import component, html, run, use_state

@component
def Counter():
    count, set_count = use_state(0)
    return html.button(f"Clicked {count} times", on_click=lambda: set_count(count + 1))

run(Counter)  # http://127.0.0.1:8000

The package is pyreaction, the import is reaction, and the command is reaction (pyreaction works too).

✨ Key Features

  • The React model in Python Components, use_state, use_effect, context, keys, error boundaries, Suspense, portals, refs, the rules of hooks, StrictMode, and React 19's form actions, use_optimistic and ViewTransition.

  • Server-rendered first Every URL returns real HTML with its data in it, good for search engines and slow devices. Suspense content streams into the same response, and the live page adopts the HTML instead of redrawing it.

  • Small updates The server diffs the tree and sends only DOM operations, such as "set the text of node 12". A click on a small page is a round trip of about a third of a millisecond on the server.

  • Resilient connections Pages reconnect by themselves and resume with their state, and typing is never undone by a slow round trip.

  • Large pages stay fast VirtualList renders only the rows in view, and per-page stores with selectors re-render only the components whose part changed.

  • Fits your stack Run it alone, mount it in FastAPI (with Depends), put it in front of Django or Flask, or drop components into Jinja templates.

  • Escape hatches Wrap any JavaScript or React widget, run code in the browser with js(), call element methods from Python, and read any event property on demand.

  • Testable and typed A fake browser for unit tests, a live server helper for Playwright, and typed HTML tags that editors autocomplete.

🛠️ Installation

Prerequisites

  • Python 3.11 or newer
  • uv (recommended) or pip
uv add pyreaction
# or
pip install pyreaction

Optional extras are added the same way, as in pyreaction[redis,jinja]:

Extra For
redis stores shared between worker processes (RedisBackend)
wsgi putting pages in front of a WSGI app such as Flask
jinja the {% component %} template tag
browser Playwright, for tests in a real browser

Start a project

uvx pyreaction init my-app
cd my-app
uv run dev        # http://127.0.0.1:8000, restarts on every save
my-app/
  pyproject.toml     settings under [tool.pyreaction]
  app.py             the App component
  components/        your components
  public/            served at /static; style.css is linked from the page

Use it with your framework

FastAPI

Serve the pages next to your API with mount, or turn a route into a page with page, whose function can use Depends:

from fastapi import Depends, FastAPI
from reaction import mount, page

api = FastAPI()
mount(api, App, "/ui")                          # a whole app under /ui

@page(api, "/orders/{order_id}", title="Order")
def order(order_id: int, user=Depends(current_user)):
    return OrderPage(order_id=order_id, user=user)

Run it as usual with uvicorn main:api. See Integrations.

Django

Put the pages in front of Django's ASGI app, in asgi.py:

from django.core.asgi import get_asgi_application
from reaction import wrap

application = wrap(get_asgi_application(), Dashboard, "/dashboard")

Run it with uvicorn mysite.asgi:application. See Integrations.

Flask

Install the wsgi extra, then wrap the Flask app:

from reaction import wrap

app = wrap(flask_app, Dashboard, "/dashboard")

Run it with uvicorn main:app. See Integrations.

Jinja templates

Install the jinja extra, then add a {% component %} tag to your templates:

from reaction import jinja_components

api.mount("/components", jinja_components(templates.env, [Counter, Chart], secret=SECRET))
{% component "myapp.widgets.Counter" %}
{% component "myapp.widgets.Chart", values=[1, 2, 3] %}

See Integrations.

🖥️ Running pyreaction

Command
python app.py a single file that calls run(App)
uv run dev a project, with reload on save and errors shown in the page
uv run pro a project, for production
uvicorn main:app any app built with create_app, mount or wrap

reaction run and reaction dev take --host and --port, which default to PYREACTION_HOST/127.0.0.1 and PYREACTION_PORT/PORT/8000. Behind a proxy, pass the WebSocket upgrade through; Deployment has an Nginx example and notes on workers and sticky sessions.

⚙️ Configuration

Options go to create_app (and run, mount, page, wrap), into a project's [tool.pyreaction] table, or into PYREACTION_* environment variables. The ones most often changed:

Option Default Environment variable
debug False PYREACTION_DEBUG errors and warnings shown in the page
allowed_origins () PYREACTION_ALLOWED_ORIGINS extra origins allowed to open the WebSocket
compress True PYREACTION_COMPRESS gzip pages; turn off when a proxy compresses
resume_timeout 30.0 PYREACTION_RESUME_TIMEOUT seconds a dropped page's state is kept
max_upload_size 10 MiB PYREACTION_MAX_UPLOAD_SIZE bytes per uploaded file
reconnect_attempts 0 (no limit) PYREACTION_RECONNECT_ATTEMPTS failed attempts before a page gives up

Configuration lists all of them.

📚 Documentation

Getting started install, a single-file app, a project
Elements and events describing the page, handlers, file uploads
Hooks state, effects and the rest
Data, forms and pending state loading data, Suspense, form actions, optimistic updates
Routing and the page head URLs, links, redirects, <title> and <meta>
Built-in components and the DOM refs, portals, long lists, error boundaries, JavaScript widgets
Shared state stores shared between pages, processes, or within one page
Integrations FastAPI, Django, Flask, Jinja templates, static pages
Testing a fake browser for unit tests, a live server for Playwright
Configuration every option, in code, pyproject.toml or the environment
Deployment and security workers, proxies, compression, origins, limits
API reference everything reaction exports
How it works the request, the live connection, and the update protocol

Runnable apps are in examples/: a todo list, routed pages, a login flow, JavaScript widgets, and a FastAPI app.

🔒 Security

  • WebSockets are accepted only from the page's own origin, plus allowed_origins.
  • Log in with ordinary HTTP routes that set an HttpOnly cookie; a WebSocket cannot set cookies. examples/login.py is a complete version.
  • Text is always escaped. dangerously_set_inner_html, js() code and js_component_from_string are sent as written: never build them from user input.
  • Uploads, queued events and pending updates are capped per page.

Deployment and security has the details. To report a vulnerability, see SECURITY.md; please do not open a public issue.

📝 Limitations

  • Every interaction is a round trip. Handlers run on the server, so latency follows the network; use js() for what must react instantly, such as selecting an input's text.
  • Each open page holds state on the server. Memory grows with open tabs, and a 10,000-row page holds about 20 MB. Long lists should use VirtualList.
  • Rendering runs on the server's event loop. A very large page (10,000 rows) takes about 80 ms to serve, during which other pages on that process wait.
  • Resuming after a drop needs the same process. With several workers, route a page's connections to the worker that served it (sticky sessions).
  • Before 1.0, the API may still change between minor versions; see the changelog.
  • The import name is reaction, which an unrelated, unmaintained PyPI package (reaction, 2020) also uses. Do not install both in one environment.

🤝 Contributing

Bug reports and pull requests are welcome. CONTRIBUTING.md explains the setup, the tests and type checks, and what a change needs. CHANGELOG.md lists what changed in each version.

🙏 References

pyreaction follows the design of these projects; their documentation is the best explanation of the ideas it borrows.

  • React (source): the component model, hooks and their rules, keys and reconciliation, Suspense, error boundaries, portals, and React 19's form actions, use_optimistic, use, cache, resource hints and ViewTransition. Where pyreaction has a React API, it follows React's behaviour, checked against React 19.3's source.
  • ReactPy (source): React's model in Python, rendered over a WebSocket. pyreaction's hook names, html element factories, to_vdom key names and JavaScript component interop follow ReactPy's lead; it differs in diffing on the server and sending DOM operations rather than VDOM trees, and in rendering every page as HTML first.

It is built on Starlette, uvicorn and orjson.

pyreaction is an independent project. It is not affiliated with, endorsed by or sponsored by Meta, the React project or the ReactPy project. React is a trademark of Meta Platforms, Inc.; the name is used only to describe what pyreaction follows.

🪪 License

pyreaction is free and open-source software, released under the MIT License. You may use, copy, modify, merge, publish, distribute, sublicense and sell it, in personal and commercial projects alike, provided the copyright notice and the license text are included in all copies or substantial portions of the software.

The software is provided "as is", without warranty of any kind; the authors are not liable for any claim or damages arising from its use. Contributions to pyreaction are accepted under the same license (see CONTRIBUTING.md).

📄 Citation

@software{pyreaction,
  author    = {jeomon},
  title     = {pyreaction: React-style UI components in pure Python},
  year      = {2026},
  publisher = {GitHub},
  url       = {https://github.com/Jeomon/pyreaction}
}

Metadata

Release files for pyreaction 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyreaction 0.1.0
File Size Uploaded Provenance Actions
pyreaction-0.1.0.tar.gz 127.6 kB Without provenance Details for pyreaction-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyreaction 0.1.0
File Interpreter ABI Platform Actions
pyreaction-0.1.0-py3-none-any.whl Python 3 none any Details for pyreaction-0.1.0-py3-none-any.whl

Total release size: 265.3 kB

Release files / pyreaction-0.1.0.tar.gz

Download URL pyreaction-0.1.0.tar.gz
Size 127.6 kB
Tags Source
SHA-256 checksum
How to use checksums
f9258f96dfb4645ed0325babc960c9cf5f88f39764aa4a124bed50bcfea1df97
BLAKE2b-256 checksum
How to use checksums
95f7ed6217d6be4f0333e3d7bdfc6529c9624370fc71bc2a97946bda8c7e8537
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.0

Release files / pyreaction-0.1.0-py3-none-any.whl

Download URL pyreaction-0.1.0-py3-none-any.whl
Size 137.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
756ba47fd10a19e9ae407ded27436ce7324ed02af87c67f3fef80e5a3076c5ce
BLAKE2b-256 checksum
How to use checksums
a35af27d528caf8fc29fc1fc68b42930e21248d420067e77578a6e9f914824e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.0

Security for pyreaction 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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