remotestate 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ # SCM syntax highlighting & preventing 3-way merges
2
+ pixi.lock merge=binary linguist-language=YAML linguist-generated=true
@@ -0,0 +1,8 @@
1
+ .pixi/
2
+ *.egg-info
3
+ __pycache__/
4
+ .idea/
5
+ .ipynb_checkpoints/
6
+ .venv/
7
+ uv.lock
8
+ dist/
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Norman Fomferra
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,59 @@
1
+ Metadata-Version: 2.4
2
+ Name: remotestate
3
+ Version: 0.1.0
4
+ Summary: Python state, React UI.
5
+ Project-URL: Homepage, https://github.com/bcdev/remotestate
6
+ Project-URL: Issues, https://github.com/bcdev/remotestate/issues
7
+ Project-URL: Repository, https://github.com/bcdev/remotestate
8
+ Author: forman
9
+ License: MIT
10
+ License-File: LICENSE
11
+ Keywords: jupyter,python,react,state,ui
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
18
+ Requires-Python: >=3.12
19
+ Requires-Dist: fastapi<0.137,>=0.136
20
+ Requires-Dist: pydantic<3,>=2
21
+ Requires-Dist: uvicorn<0.47,>=0.46
22
+ Description-Content-Type: text/markdown
23
+
24
+ # RemoteState - Python
25
+
26
+ `remotestate` is the Python runtime for the _RemoteState_ library.
27
+
28
+ It gives you:
29
+
30
+ - `Store` for application state
31
+ - `Service` for defining actions and queries
32
+ - `action` and `query` decorators
33
+ - `serve()` for exposing the backend to the React frontend
34
+
35
+ ## Install
36
+
37
+ ```bash
38
+ pip install remotestate
39
+ ```
40
+
41
+ ## Quick Start
42
+
43
+ ```python
44
+ import remotestate as rs
45
+
46
+ store = rs.Store({"count": 0})
47
+
48
+
49
+ class MyService(rs.Service):
50
+ @rs.action
51
+ async def increment(self):
52
+ self.store.set("count", self.store.get("count") + 1)
53
+
54
+
55
+ rs.serve(MyService(store), dist_dir="my-ui/dist")
56
+ ```
57
+
58
+ For the full project overview, see the repository root README:
59
+ [Remote State](https://github.com/bcdev/remotestate)
@@ -0,0 +1,36 @@
1
+ # RemoteState - Python
2
+
3
+ `remotestate` is the Python runtime for the _RemoteState_ library.
4
+
5
+ It gives you:
6
+
7
+ - `Store` for application state
8
+ - `Service` for defining actions and queries
9
+ - `action` and `query` decorators
10
+ - `serve()` for exposing the backend to the React frontend
11
+
12
+ ## Install
13
+
14
+ ```bash
15
+ pip install remotestate
16
+ ```
17
+
18
+ ## Quick Start
19
+
20
+ ```python
21
+ import remotestate as rs
22
+
23
+ store = rs.Store({"count": 0})
24
+
25
+
26
+ class MyService(rs.Service):
27
+ @rs.action
28
+ async def increment(self):
29
+ self.store.set("count", self.store.get("count") + 1)
30
+
31
+
32
+ rs.serve(MyService(store), dist_dir="my-ui/dist")
33
+ ```
34
+
35
+ For the full project overview, see the repository root README:
36
+ [Remote State](https://github.com/bcdev/remotestate)
@@ -0,0 +1,89 @@
1
+ # remotestate-py To-Dos
2
+
3
+ ## New Features
4
+
5
+ - [x] Allow serving the UI app from the known URL when running the HTTP dev server
6
+ serving the Reaact/TypeScript dev app.
7
+ - `ui_dist` passed to `serve()` can be a URL.
8
+ - Include WebSocket URL as query parameter `ws` in opened IFRAME / browser tab.
9
+
10
+ - [x] Allow enhancing the FastAPI apps by new (HTTP) routes, e.g., to allow for
11
+ adding an extra REST API.
12
+
13
+ - [ ] We currently require a user's `Service` query and action methods to
14
+ accept and return JSON data only. Since we use Pydantic, `pydantic.BaseClass`
15
+ arguments and return values should be handled by default. Allow for custom
16
+ serielizer/deserializers later (per-service and per-method).
17
+
18
+ - [ ] Allow calling user `Service` methods on the created `Service` instance.
19
+ but including their reactive behavior.
20
+ For this to work, `@action` and `@query` decorators must return wrapped
21
+ versions of the function that invoke them like if the `action` or `query`
22
+ came from the frontend.
23
+ (Nice, even `task_id` would work with a little effort!)
24
+ Care: If an action calls actions or queries or a query calls queries the
25
+ original method must be called, not the wrapped, reactive version.
26
+
27
+ ## Bugs
28
+
29
+ - [x] If `ui_dist` passed to `serve()` is a URL, the UI won't work although the
30
+ correct WebSocket URL is passed as query parameter `ws` in opened IFRAME.
31
+
32
+ ## Refactorings
33
+
34
+ - [x] Rename protocol `id` to `call_id`
35
+ - [x] Rename protocol `tid` to `task_id`
36
+ - [x] Rename `InvalidateMessage` to `ActionResultMessage`
37
+ - [x] Rename `"invalidate"` message to `"action_result"`
38
+ - [x] Rename `Service.process` to `Service.update_task`
39
+ - [x] Rename `"task_update"` to `"update_task"`
40
+
41
+ ## Improve error handling
42
+
43
+ - [ ] Review Server and Service classes
44
+ - [ ] Visit all critical paths: log or raise or both?
45
+ - [ ] Include traceback in `ErrorMessage`
46
+
47
+ ## State equality
48
+
49
+ - [ ] Only include values in TaskUpdateMessage that changed. Use `==` by default.
50
+ Ensure `InvalidatetMessage` is always sent, even if there are no updates.
51
+ - [ ] Allow passing custom equality checks, register `f(a, b): bool`
52
+ using state path as key.
53
+
54
+ ## State serialization
55
+
56
+ - [ ] Register JSON codec or Pydantic class using state path as key
57
+
58
+ ## State validation
59
+
60
+ - [ ] Register OpenAPI/JSON schema or Pydantic class using state path as key.
61
+ - [ ] Use schema validate before setting a value
62
+
63
+ ## Performance
64
+
65
+ - [ ] Check code for obvious optimization options
66
+ - [ ] Check code for potential performance limitation issues
67
+ - [ ] Check if we can compress request/response sizes, e.g.,
68
+ have a special binary format for (numpy-like) array data
69
+ and (pandas-like) data frames.
70
+ - [ ] Maybe throttle number emitted TaskUpdateMessage / time
71
+ - [ ] Was using WebSockets + JSON the right decision?
72
+
73
+ ## CI
74
+
75
+ - [x] Enhance quality checks, e.g., use mypy or similar
76
+ - [x] Create and configure GitHub actions
77
+
78
+ # remotestate-py Ideas
79
+
80
+ ## Add-on project: TS interface generation
81
+
82
+ - [ ] A CLI tool to generate a typescript interface and service factory
83
+ from a given Python `Service` implementation. The service factory creates
84
+ a 1:1 TS version of the Python service.
85
+
86
+ ## Add-on project: UI generation
87
+
88
+ - [ ] Allow for UI generation from OpenAPI/JSON schema.
89
+ FieldFactory interface: Neutral w.r.t. UI lib
@@ -0,0 +1,59 @@
1
+ <html>
2
+ <body>
3
+ <div id="root"></div>
4
+ <script type="module">
5
+ import React, { useState, useEffect } from "https://esm.sh/react@19";
6
+ import { createRoot } from "https://esm.sh/react-dom@19/client";
7
+
8
+ const RE = React.createElement;
9
+
10
+ // remotestate client inline:
11
+ // here we use a raw WebSocket without the
12
+ // remotestate JavaScript library.
13
+
14
+ const ws = new WebSocket("ws://localhost:9753/ws");
15
+
16
+ ws.onopen = () => {
17
+ // get initial count
18
+ ws.send(JSON.stringify({
19
+ type: "get",
20
+ id: "1",
21
+ path: "count",
22
+ }));
23
+ };
24
+
25
+ function App() {
26
+ const [count, setCount] = useState(null);
27
+
28
+ useEffect(() => {
29
+ ws.onmessage = (e) => {
30
+ const msg = JSON.parse(e.data);
31
+ if (msg.path === "count" || (msg.updates && "count" in msg.updates)) {
32
+ setCount(msg.value ?? msg.updates.count);
33
+ }
34
+ };
35
+ }, []);
36
+
37
+ function handleIncrementClick() {
38
+ console.log("handleIncrementClick! ws:", ws)
39
+ ws.send(JSON.stringify({
40
+ type: "action",
41
+ call_id: crypto.randomUUID(),
42
+ task_id: "my-action-call",
43
+ method: "increment",
44
+ args: [],
45
+ kwargs: {},
46
+ }));
47
+ }
48
+
49
+ return RE("div", null,
50
+ RE("p", null, `Count: ${count ?? "..."}`),
51
+ RE("button", { onClick: handleIncrementClick }, "Increment"),
52
+ );
53
+ }
54
+
55
+ const rootElem = document.getElementById("root");
56
+ createRoot(rootElem).render(RE(App));
57
+ </script>
58
+ </body>
59
+ </html>
@@ -0,0 +1,112 @@
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "d67529d6-1236-41f5-b690-2cb90a71953a",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import remotestate as rs"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "code",
15
+ "execution_count": 2,
16
+ "id": "fc0f8848-1407-4aec-94e2-451112bcfc87",
17
+ "metadata": {},
18
+ "outputs": [],
19
+ "source": [
20
+ "store = rs.Store({\"count\": 42, \"user\": {\"name\": \"Norman\"}})"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "execution_count": 3,
26
+ "id": "b6f59991-d812-4bd1-ae96-bca1d4ed6e46",
27
+ "metadata": {},
28
+ "outputs": [],
29
+ "source": [
30
+ "class MyService(rs.Service):\n",
31
+ " @rs.action\n",
32
+ " async def increment(self):\n",
33
+ " self.store.set(\"count\", self.store.get(\"count\") + 1)\n",
34
+ "\n",
35
+ " @rs.query\n",
36
+ " async def get_count(self) -> int:\n",
37
+ " return self.store.get(\"count\")"
38
+ ]
39
+ },
40
+ {
41
+ "cell_type": "code",
42
+ "execution_count": 4,
43
+ "id": "979719a8-99c0-484e-ba5a-7f41a9f9c0a4",
44
+ "metadata": {},
45
+ "outputs": [
46
+ {
47
+ "name": "stderr",
48
+ "output_type": "stream",
49
+ "text": [
50
+ "INFO: Started server process [17236]\n",
51
+ "INFO: Waiting for application startup.\n",
52
+ "INFO: Application startup complete.\n",
53
+ "INFO: Uvicorn running on http://localhost:9753 (Press CTRL+C to quit)\n"
54
+ ]
55
+ },
56
+ {
57
+ "data": {
58
+ "text/html": [
59
+ "\n",
60
+ " <iframe\n",
61
+ " width=\"100%\"\n",
62
+ " height=\"100\"\n",
63
+ " src=\"http://localhost:9753?t=1780732089&ws=ws%3A%2F%2Flocalhost%3A9753%2Fws\"\n",
64
+ " frameborder=\"0\"\n",
65
+ " allowfullscreen\n",
66
+ " \n",
67
+ " ></iframe>\n",
68
+ " "
69
+ ],
70
+ "text/plain": [
71
+ "<IPython.lib.display.IFrame at 0x2253a765550>"
72
+ ]
73
+ },
74
+ "metadata": {},
75
+ "output_type": "display_data"
76
+ }
77
+ ],
78
+ "source": [
79
+ "rs.serve(MyService(store), ui_dist=\"./basic-usage\", iframe_height=100)"
80
+ ]
81
+ },
82
+ {
83
+ "cell_type": "code",
84
+ "execution_count": null,
85
+ "id": "22033adc-9d19-415a-89ef-b8b383e6c862",
86
+ "metadata": {},
87
+ "outputs": [],
88
+ "source": []
89
+ }
90
+ ],
91
+ "metadata": {
92
+ "kernelspec": {
93
+ "display_name": "Python 3 (ipykernel)",
94
+ "language": "python",
95
+ "name": "python3"
96
+ },
97
+ "language_info": {
98
+ "codemirror_mode": {
99
+ "name": "ipython",
100
+ "version": 3
101
+ },
102
+ "file_extension": ".py",
103
+ "mimetype": "text/x-python",
104
+ "name": "python",
105
+ "nbconvert_exporter": "python",
106
+ "pygments_lexer": "ipython3",
107
+ "version": "3.14.4"
108
+ }
109
+ },
110
+ "nbformat": 4,
111
+ "nbformat_minor": 5
112
+ }
@@ -0,0 +1,139 @@
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 3,
6
+ "id": "d67529d6-1236-41f5-b690-2cb90a71953a",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import remotestate as rs"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "markdown",
15
+ "id": "b5855fdc-6ffc-486e-8172-8202e56f7778",
16
+ "metadata": {},
17
+ "source": [
18
+ "Assumes you have checked out https://github.com/bcdev/remotestate-demo next to this project\n",
19
+ "and called `npm run build`. "
20
+ ]
21
+ },
22
+ {
23
+ "cell_type": "code",
24
+ "execution_count": 4,
25
+ "id": "fc0f8848-1407-4aec-94e2-451112bcfc87",
26
+ "metadata": {},
27
+ "outputs": [],
28
+ "source": [
29
+ "store = rs.Store({\"count\": 42})"
30
+ ]
31
+ },
32
+ {
33
+ "cell_type": "code",
34
+ "execution_count": 6,
35
+ "id": "b6f59991-d812-4bd1-ae96-bca1d4ed6e46",
36
+ "metadata": {},
37
+ "outputs": [],
38
+ "source": [
39
+ "class MyService(rs.Service):\n",
40
+ " @rs.action\n",
41
+ " async def increment(self):\n",
42
+ " self.store.set(\"count\", self.store.get(\"count\") + 1)\n"
43
+ ]
44
+ },
45
+ {
46
+ "cell_type": "code",
47
+ "execution_count": 7,
48
+ "id": "979719a8-99c0-484e-ba5a-7f41a9f9c0a4",
49
+ "metadata": {},
50
+ "outputs": [
51
+ {
52
+ "name": "stderr",
53
+ "output_type": "stream",
54
+ "text": [
55
+ "INFO: Started server process [33104]\n",
56
+ "INFO: Waiting for application startup.\n",
57
+ "INFO: Application startup complete.\n",
58
+ "INFO: Uvicorn running on http://localhost:9753 (Press CTRL+C to quit)\n"
59
+ ]
60
+ },
61
+ {
62
+ "data": {
63
+ "text/html": [
64
+ "\n",
65
+ " <iframe\n",
66
+ " width=\"100%\"\n",
67
+ " height=\"300\"\n",
68
+ " src=\"http://localhost:9753?t=1780731968&ws=ws%3A%2F%2Flocalhost%3A9753%2Fws\"\n",
69
+ " frameborder=\"0\"\n",
70
+ " allowfullscreen\n",
71
+ " \n",
72
+ " ></iframe>\n",
73
+ " "
74
+ ],
75
+ "text/plain": [
76
+ "<IPython.lib.display.IFrame at 0x289361323c0>"
77
+ ]
78
+ },
79
+ "metadata": {},
80
+ "output_type": "display_data"
81
+ },
82
+ {
83
+ "name": "stdout",
84
+ "output_type": "stream",
85
+ "text": [
86
+ "INFO: ::1:49256 - \"GET /?t=1780731968&ws=ws%3A%2F%2Flocalhost%3A9753%2Fws HTTP/1.1\" 200 OK\n",
87
+ "INFO: ::1:49256 - \"GET /assets/index-VwyzReuI.js HTTP/1.1\" 200 OK\n",
88
+ "INFO: ::1:60640 - \"GET /assets/index-D64VDMd1.css HTTP/1.1\" 200 OK\n"
89
+ ]
90
+ },
91
+ {
92
+ "name": "stderr",
93
+ "output_type": "stream",
94
+ "text": [
95
+ "INFO: ::1:51081 - \"WebSocket /ws\" [accepted]\n",
96
+ "INFO: connection open\n"
97
+ ]
98
+ }
99
+ ],
100
+ "source": [
101
+ "rs.serve(\n",
102
+ " MyService(store), \n",
103
+ " # ui_dist=\"http://localhost:5173/\",\n",
104
+ " ui_dist=\"../../../remotestate-demo/dist\",\n",
105
+ " iframe_height=300,\n",
106
+ ")"
107
+ ]
108
+ },
109
+ {
110
+ "cell_type": "code",
111
+ "execution_count": null,
112
+ "id": "5a8d1487-8f9f-492b-bc77-69ac1320cd18",
113
+ "metadata": {},
114
+ "outputs": [],
115
+ "source": []
116
+ }
117
+ ],
118
+ "metadata": {
119
+ "kernelspec": {
120
+ "display_name": "Python 3 (ipykernel)",
121
+ "language": "python",
122
+ "name": "python3"
123
+ },
124
+ "language_info": {
125
+ "codemirror_mode": {
126
+ "name": "ipython",
127
+ "version": 3
128
+ },
129
+ "file_extension": ".py",
130
+ "mimetype": "text/x-python",
131
+ "name": "python",
132
+ "nbconvert_exporter": "python",
133
+ "pygments_lexer": "ipython3",
134
+ "version": "3.14.4"
135
+ }
136
+ },
137
+ "nbformat": 4,
138
+ "nbformat_minor": 5
139
+ }
@@ -0,0 +1,48 @@
1
+ <html>
2
+ <body>
3
+ <div id="root"></div>
4
+ <script type="importmap">
5
+ {
6
+ "imports": {
7
+ "react": "https://esm.sh/react@19",
8
+ "react-dom/client": "https://esm.sh/react-dom@19/client"
9
+ }
10
+ }
11
+ </script>
12
+ <script type="module">
13
+ import React from "react";
14
+ import { createRoot } from "react-dom/client";
15
+ import { RemoteStateProvider, useRemoteStateClient, useRemoteState } from "/dist/remotestate.js";
16
+
17
+ const RE = React.createElement;
18
+
19
+ function App() {
20
+ const client = useRemoteStateClient();
21
+ const [count, setCount] = useRemoteState("count", 0);
22
+
23
+ function handleIncrementClick() {
24
+ client.action("increment");
25
+ }
26
+
27
+ function handleAddTwoClick() {
28
+ setCount(count + 2);
29
+ }
30
+
31
+ return RE("div", null,
32
+ RE("p", null, `Count: ${count}`),
33
+ RE("button", { onClick: handleIncrementClick }, "Increment"),
34
+ RE("button", { onClick: handleAddTwoClick }, "Add 2"),
35
+ );
36
+ }
37
+
38
+ function Main() {
39
+ return RE(RemoteStateProvider, { url: "ws://localhost:9755/ws" }, RE(App));
40
+ }
41
+
42
+ console.log("App rendering")
43
+
44
+ const rootElem = document.getElementById("root");
45
+ createRoot(rootElem).render(RE(Main));
46
+ </script>
47
+ </body>
48
+ </html>