Skip to main content

egui-states

crates.io docs.rs

egui-states synchronizes typed application state between an egui UI and a server. The UI is the Rust client; the server can be written in Python or Rust. Both native and WebAssembly egui clients use the same state API and communicate with the server over a WebSocket connection.

The project is useful when an egui application is primarily a view and control surface for work performed elsewhere—for example, a Python data-processing application, an instrument controller, or a separate Rust service. Application code reads and writes typed state handles instead of defining its own messages, serialization, and synchronization logic.

How it works

  1. Define the UI's state tree as Rust structs whose fields are egui-states handles.
  2. Derive State so the client can construct the tree and calculate its stable layout hash.
  3. Generate matching Python or Rust server bindings from that same Rust type in a build script.
  4. Start the server and connect the egui client. During the handshake, both peers compare the wire-protocol version and any configured application version or token. Updates are then serialized, type-checked, and applied to the matching state on the other side.

The main state types describe both the stored data and its direction:

  • Value<T> is stored on both peers and can be changed in either direction.
  • Static<T> is controlled by the server and read by the client. It cannot be written from the client; use Value<T> when both sides may write.
  • ValueAtomic<T> and StaticAtomic<T> provide low-overhead access for small, copyable values.
  • Signal<T> is a transient client-to-server event. Signals can coalesce pending values or queue every value.
  • ValueTake<T> and DataTake<T> are one-shot server-to-client transfers.
  • VecState<T> and MapState<K, V> synchronize collections.
  • Data<T> and DataMulti<T> efficiently synchronize numeric buffers; Python exposes them as NumPy arrays.
  • DataMultiTake<T> provides independent one-shot numeric transfers keyed by u32.
  • Image synchronizes complete images or rectangular updates to an egui texture; ImageMulti provides a sparse, server-controlled collection of textures indexed by u32 keys.

State paths follow the Rust field hierarchy. A field named counter on the root state is registered as root.counter; a field inside controls becomes root.controls.<field>.

Feature flags

Feature Default Enables
client yes egui client state handles and ClientBuilder
server no Native Rust server API in egui_states::server
python no PyO3 support used to build the Python extension module
build_scripts no generate_python and generate_rust binding generators

Minimal Python-server workflow

Server generation must import the same Rust state type as the UI. In a real workspace, put that type in a small shared library crate and use it as both a UI dependency and a build dependency. The complete arrangement is demonstrated in example/; the essential pieces are shown below.

Define the state shared by the UI and generator:

use egui_states::Value;

#[derive(egui_states::State)]
pub struct AppState {
    pub counter: Value<i32>,
}

Generate a Python package from it in the UI application's build.rs:

use egui_states::build_scripts::generate_python;
use ui_state::AppState;

fn main() {
    generate_python::<AppState>("python/states_server").unwrap();
}

The build script needs egui_states with the build_scripts feature and the shared state crate as build dependencies:

[build-dependencies]
egui_states = { version = "0.15", features = ["build_scripts"] }
ui_state = { path = "../ui-state" }

Construct and connect the client from the egui application:

use egui_states::ClientBuilder;
use ui_state::AppState;

let (states, client) = ClientBuilder::<AppState>::new()
    .context(egui_context.clone())
    .build(8091);
client.connect();

// Inside the UI:
if ui.button("Increment").clicked() {
    states.counter.set_signal(states.counter.get() + 1);
}

After building the UI crate, add the generated package to Python's import path and run the server:

from states_server import StatesServer

server = StatesServer()
server.states.counter.connect(lambda value: print("counter:", value))
server.start(8091)

server.states.counter.set(41, update=True)
input("Server is running; press Enter to stop.\n")
server.stop()

set_signal on the client updates the value and invokes connected server callbacks. The server's update=True asks egui to repaint after applying its new value.

To generate a native Rust server instead, use egui_states::build_scripts::generate_rust and enable the server feature in the server crate:

[dependencies]
egui_states = { version = "0.15", default-features = false, features = ["server"] }

[build-dependencies]
egui_states = { version = "0.15", features = ["build_scripts"] }
ui_state = { path = "../ui-state" }

Generate the module in build.rs:

use egui_states::build_scripts::generate_rust;
use ui_state::AppState;

fn main() {
    println!("cargo:rerun-if-changed=../ui-state/src/");
    generate_rust::<AppState>("src/states_server").unwrap();
}

Then declare mod states_server;, construct states_server::StatesServer, and call start(port, ip_addr, token). See example/rust for a complete server.

Custom types

Use #[egui_states::typed] to make a struct or fieldless enum available to egui-states. The attribute implements Typed and derives Serde serialization through the library's Serde re-export, so the consuming crate does not need a direct serde dependency.

#[egui_states::typed]
#[derive(Clone, Debug, PartialEq)]
struct Settings {
    enabled: bool,
    label: String,
}

Use rust_derive(...) to add derives to the corresponding custom type in generated Rust server bindings:

#[egui_states::typed(rust_derive(Debug, PartialEq, Eq))]
#[derive(Clone, Debug, PartialEq, Eq)]
struct Settings {
    enabled: bool,
    label: String,
}

This option affects only generated Rust server types and adds to, rather than replaces, their automatic derives. Generated enums automatically derive Clone, Copy, Debug, PartialEq, Eq, and Hash; generated structs automatically derive only Clone. Requests for those automatic derives, or for Serialize, Deserialize, and Typed already supplied by typed, are ignored. Other qualified derive paths are preserved as distinct macros.

Arbitrary derive paths are accepted, but their macros must be available to the server crate. Required prerequisite derives and field trait implementations are not inferred.

Add egui_states::InitialValue when the type is used by a Value or Static field whose default must be emitted into generated server bindings:

#[egui_states::typed]
#[derive(Clone, Debug, Default, PartialEq, egui_states::InitialValue)]
struct Settings {
    enabled: bool,
    label: String,
}

The typed attribute replaces the former #[derive(serde::Serialize, serde::Deserialize, egui_states::Typed)] syntax.

Compatibility and connection settings

The handshake can reject a connection for three reasons:

  • The internal wire-protocol version is always checked. It changes when the serialized protocol changes incompatibly.
  • An optional application version can be configured with ClientBuilder::version and the matching ServerOptions or generated Python-server argument.
  • An optional authentication token is passed to the client builder and to each server start call.

The generated state-layout hash is exposed for use as the application version, but it is not enforced automatically. To reject mismatched state trees, use the client builder's hash and the generated server's VERSION_HASH:

let builder = ClientBuilder::<AppState>::new();
let layout_version = builder.get_version_hash();
let (states, client) = builder.version(layout_version).build(8091);

For Python, construct the generated server with StatesServer(version=StatesServer.VERSION_HASH) and then call start(8091). For Rust, set ServerOptions::version to Some(StatesServer::VERSION_HASH) before calling StatesServer::with_options.

Renaming, reordering, adding, or changing state fields changes the layout hash. Generate bindings from the same state type and rebuild both sides after any such change. Handshake failures are reported through server diagnostics and the client connection state.

Native and WebAssembly clients

The state API is the same on native and WASM targets. On native targets, ClientBuilder::build creates a background thread with its own Tokio runtime; the application does not need to provide one. On WASM, the synchronization task runs on the browser executor.

The included web example uses Trunk:

trunk serve

This serves the UI on port 8090; run either example server separately on port 8091.

Troubleshooting

  • Call Image::initialize or ImageMulti::initialize, as applicable, on the client before connecting or before the server sends image updates. Updates received without an initialized texture are acknowledged but cannot become visible.
  • Keep Rust CallbackHandle values alive. Dropping a handle unregisters its callback.
  • Add an appropriate cargo:rerun-if-changed line to generator build scripts, or Cargo may not regenerate bindings after the shared state definition changes.
  • Value messages have a serialized size limit. Client set rejects an oversized value; an in-place write keeps the local edit but cannot send it, leaving the peers out of sync until a later successful update.
  • Passing None for ip_addr when starting a server binds all IPv4 interfaces. Pass a specific address when the server should only be reachable through that interface.

See example/README.md for commands to run the complete examples and docs/architecture.md for the protocol and synchronization model.

Metadata

Release files for egui-states 0.16.0

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

Built distributions (wheels)

Table of built distributions (wheels) for egui-states 0.16.0
File Actions
egui_states-0.16.0-cp314-cp314t-win_amd64.whl CPython 3.14 CPython 3.14 free-threading Windows x86-64 Details for egui_states-0.16.0-cp314-cp314t-win_amd64.whl
egui_states-0.16.0-cp314-cp314t-manylinux_2_28_x86_64.whl CPython 3.14 CPython 3.14 free-threading Linux glibc 2.28+ x86-64 Details for egui_states-0.16.0-cp314-cp314t-manylinux_2_28_x86_64.whl
egui_states-0.16.0-cp314-cp314-win_amd64.whl CPython 3.14 CPython 3.14 Windows x86-64 Details for egui_states-0.16.0-cp314-cp314-win_amd64.whl
egui_states-0.16.0-cp314-cp314-manylinux_2_28_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.28+ x86-64 Details for egui_states-0.16.0-cp314-cp314-manylinux_2_28_x86_64.whl
egui_states-0.16.0-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details for egui_states-0.16.0-cp313-cp313-win_amd64.whl
egui_states-0.16.0-cp313-cp313-manylinux_2_28_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.28+ x86-64 Details for egui_states-0.16.0-cp313-cp313-manylinux_2_28_x86_64.whl
egui_states-0.16.0-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details for egui_states-0.16.0-cp312-cp312-win_amd64.whl
egui_states-0.16.0-cp312-cp312-manylinux_2_28_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.28+ x86-64 Details for egui_states-0.16.0-cp312-cp312-manylinux_2_28_x86_64.whl

Total release size: 6.8 MB

Release files / egui_states-0.16.0-cp314-cp314t-win_amd64.whl

Download URL egui_states-0.16.0-cp314-cp314t-win_amd64.whl
Size 757.7 kB
Tags CPython 3.14 CPython 3.14 free-threading Windows x86-64
SHA-256 checksum
How to use checksums
0bb2710d29460b41a83ab425b1b6467302efe9fd9fea19c9314c96c4eba504af
BLAKE2b-256 checksum
How to use checksums
083be0a5835b508490e6343872c5c5b864bed1c37d7a6c5e6b4282e2a83074cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / egui_states-0.16.0-cp314-cp314t-manylinux_2_28_x86_64.whl

Download URL egui_states-0.16.0-cp314-cp314t-manylinux_2_28_x86_64.whl
Size 947.3 kB
Tags CPython 3.14 CPython 3.14 free-threading Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
9345bfac7cca2a9dd489547fe76f5cf1e7586ed4388fc3982b2499b0710994b9
BLAKE2b-256 checksum
How to use checksums
33379bec647e8dae534dbbd0c540d09cb32130e6041b28a6c8dfb0ba7a3b2de2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / egui_states-0.16.0-cp314-cp314-win_amd64.whl

Download URL egui_states-0.16.0-cp314-cp314-win_amd64.whl
Size 757.4 kB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
222b6376798c99a29e6fa967265d68e3bcbca7250c87ae6c7e3094a3550d08de
BLAKE2b-256 checksum
How to use checksums
037bafdd8df6540b4559138a8ea11795e29e0d65a632321b7eb55363a7c9580c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / egui_states-0.16.0-cp314-cp314-manylinux_2_28_x86_64.whl

Download URL egui_states-0.16.0-cp314-cp314-manylinux_2_28_x86_64.whl
Size 949.5 kB
Tags CPython 3.14 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
03feab9af1f86d1de9bcdf8bab6bdff8242a3194d13686f9418833ed5abb515e
BLAKE2b-256 checksum
How to use checksums
5554f585280c68d554701f3cefd6ca03f33cc46d53794eea49941a6fd02274fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / egui_states-0.16.0-cp313-cp313-win_amd64.whl

Download URL egui_states-0.16.0-cp313-cp313-win_amd64.whl
Size 757.8 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
33567911fe05ba8a0f0aec49a6addba8cd360749dc8e44b85fa5f4ea6b0ef6fd
BLAKE2b-256 checksum
How to use checksums
529d3928bbbdbde3fb1816ae43d8b11bafb84989a459785aec9e1cd34ed42b57
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / egui_states-0.16.0-cp313-cp313-manylinux_2_28_x86_64.whl

Download URL egui_states-0.16.0-cp313-cp313-manylinux_2_28_x86_64.whl
Size 947.8 kB
Tags CPython 3.13 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
c4432471273946fe930a9b64dc17ade5808eb533b908545227b496ffd0f0f6af
BLAKE2b-256 checksum
How to use checksums
c0b006d71cad044c4f5dfaa3c93f6b8bee06fb28259dff94477e19d0ca847c84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / egui_states-0.16.0-cp312-cp312-win_amd64.whl

Download URL egui_states-0.16.0-cp312-cp312-win_amd64.whl
Size 757.9 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
f5124c155a15aa4a898e58d582eafbccd1b9f39f03335f12737c95b7bec03433
BLAKE2b-256 checksum
How to use checksums
e545edf3c7cd22e676bbd2970e67a662d236d9703b5c1885fcc887d97f5a7a07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / egui_states-0.16.0-cp312-cp312-manylinux_2_28_x86_64.whl

Download URL egui_states-0.16.0-cp312-cp312-manylinux_2_28_x86_64.whl
Size 947.8 kB
Tags CPython 3.12 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
64e74b15a5c94fa85865aa583e02e15bf1ddb4941924ce56171c2f638b44f3af
BLAKE2b-256 checksum
How to use checksums
5aeecc760113ececed252844ed9851b467b0bf44acc6e4574c1b9b91ea7c4b8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for egui-states 0.16.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.

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