pyweb-stack 0.1.0__py3-none-any.whl
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.
- pyweb/__init__.py +71 -0
- pyweb/app.py +37 -0
- pyweb/app_loader.py +139 -0
- pyweb/asgi.py +71 -0
- pyweb/auth.py +701 -0
- pyweb/bench.py +101 -0
- pyweb/browser.py +232 -0
- pyweb/build.py +295 -0
- pyweb/cache.py +201 -0
- pyweb/cli/__init__.py +465 -0
- pyweb/cli/__main__.py +3 -0
- pyweb/compiler/__init__.py +5 -0
- pyweb/compiler/ast.py +45 -0
- pyweb/compiler/codegen/__init__.py +9 -0
- pyweb/compiler/codegen/ir.py +50 -0
- pyweb/compiler/errors.py +29 -0
- pyweb/compiler/lower.py +733 -0
- pyweb/compiler/parser.py +532 -0
- pyweb/compiler/pipeline.py +158 -0
- pyweb/compiler/pyjs.py +1087 -0
- pyweb/compiler/rpc.py +35 -0
- pyweb/context.py +193 -0
- pyweb/css.py +115 -0
- pyweb/db/__init__.py +652 -0
- pyweb/db/migrate.py +143 -0
- pyweb/decorators.py +49 -0
- pyweb/deploy.py +102 -0
- pyweb/forms.py +108 -0
- pyweb/hosting.py +139 -0
- pyweb/jobs.py +248 -0
- pyweb/live.py +88 -0
- pyweb/lsp.py +214 -0
- pyweb/models.py +269 -0
- pyweb/npm.py +270 -0
- pyweb/observability.py +313 -0
- pyweb/platform.py +69 -0
- pyweb/plugins.py +112 -0
- pyweb/py.typed +0 -0
- pyweb/realtime.py +189 -0
- pyweb/rpc.py +137 -0
- pyweb/runtime/browser/runtime.js +1151 -0
- pyweb/runtime/server/__init__.py +445 -0
- pyweb/security.py +162 -0
- pyweb/serve.py +335 -0
- pyweb/ssr.py +260 -0
- pyweb/sync.py +118 -0
- pyweb/testing.py +154 -0
- pyweb/uploads.py +94 -0
- pyweb_stack-0.1.0.dist-info/METADATA +198 -0
- pyweb_stack-0.1.0.dist-info/RECORD +54 -0
- pyweb_stack-0.1.0.dist-info/WHEEL +5 -0
- pyweb_stack-0.1.0.dist-info/entry_points.txt +2 -0
- pyweb_stack-0.1.0.dist-info/licenses/LICENSE +21 -0
- pyweb_stack-0.1.0.dist-info/top_level.txt +1 -0
pyweb/sync.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"""Offline sync: queued mutations, LWW + version conflicts, IndexedDB-shaped log.
|
|
2
|
+
|
|
3
|
+
The server holds authoritative rows ``{id: (version, data)}``. Each client
|
|
4
|
+
keeps an op log while offline; on reconnect the log replays in order:
|
|
5
|
+
- ``set`` with a higher version wins (last-writer-wins per field set).
|
|
6
|
+
- ``delete`` tombstones a row unless a newer ``set`` arrives after it.
|
|
7
|
+
- concurrent ``set``/``set`` on the same version applies the resolver
|
|
8
|
+
(default: server value wins, client op reported as conflict).
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import time
|
|
14
|
+
import uuid
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Op:
|
|
18
|
+
def __init__(self, kind, row_id, data=None, *, version=0, actor="client", ts=None):
|
|
19
|
+
self.id = uuid.uuid4().hex[:12]
|
|
20
|
+
self.kind = kind # set | delete
|
|
21
|
+
self.row_id = row_id
|
|
22
|
+
self.data = data or {}
|
|
23
|
+
self.version = version
|
|
24
|
+
self.actor = actor
|
|
25
|
+
self.ts = ts if ts is not None else time.time()
|
|
26
|
+
|
|
27
|
+
def to_dict(self):
|
|
28
|
+
return {"id": self.id, "kind": self.kind, "row": self.row_id,
|
|
29
|
+
"data": self.data, "version": self.version, "actor": self.actor}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class SyncServer:
|
|
33
|
+
def __init__(self, resolver=None):
|
|
34
|
+
self.rows: dict[str, tuple[int, dict]] = {}
|
|
35
|
+
self.tombstones: dict[str, int] = {}
|
|
36
|
+
self.resolver = resolver or (lambda server, client: ("server", server))
|
|
37
|
+
self.conflicts: list[dict] = []
|
|
38
|
+
|
|
39
|
+
def seed(self, row_id, data, version=1):
|
|
40
|
+
self.rows[row_id] = (version, dict(data))
|
|
41
|
+
|
|
42
|
+
def apply(self, op):
|
|
43
|
+
if op.kind == "set":
|
|
44
|
+
cur = self.rows.get(op.row_id)
|
|
45
|
+
tomb_v = self.tombstones.get(op.row_id, -1)
|
|
46
|
+
if cur is None:
|
|
47
|
+
if op.version >= tomb_v:
|
|
48
|
+
self.rows[op.row_id] = (op.version, dict(op.data))
|
|
49
|
+
self.tombstones.pop(op.row_id, None)
|
|
50
|
+
return "applied"
|
|
51
|
+
return "stale"
|
|
52
|
+
cur_v, cur_data = cur
|
|
53
|
+
if op.version > cur_v:
|
|
54
|
+
self.rows[op.row_id] = (op.version, dict(op.data))
|
|
55
|
+
return "applied"
|
|
56
|
+
if op.version == cur_v and op.data != cur_data:
|
|
57
|
+
winner, merged = self.resolver(cur_data, op.data)
|
|
58
|
+
self.rows[op.row_id] = (cur_v + 1, dict(merged))
|
|
59
|
+
self.conflicts.append({"row": op.row_id, "winner": winner,
|
|
60
|
+
"server": cur_data, "client": op.data})
|
|
61
|
+
return "conflict"
|
|
62
|
+
return "stale"
|
|
63
|
+
if op.kind == "delete":
|
|
64
|
+
cur = self.rows.get(op.row_id)
|
|
65
|
+
if cur is None:
|
|
66
|
+
return "stale"
|
|
67
|
+
cur_v, _ = cur
|
|
68
|
+
if op.version >= cur_v:
|
|
69
|
+
del self.rows[op.row_id]
|
|
70
|
+
self.tombstones[op.row_id] = op.version
|
|
71
|
+
return "applied"
|
|
72
|
+
return "stale"
|
|
73
|
+
raise ValueError(f"unknown op {op.kind!r}")
|
|
74
|
+
|
|
75
|
+
def snapshot(self):
|
|
76
|
+
return {k: {"version": v, "data": d} for k, (v, d) in self.rows.items()}
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class SyncClient:
|
|
80
|
+
"""Local store with an offline queue; mirrors the IndexedDB side."""
|
|
81
|
+
|
|
82
|
+
def __init__(self, server=None):
|
|
83
|
+
self.server = server
|
|
84
|
+
self.online = True
|
|
85
|
+
self.local: dict[str, tuple[int, dict]] = {}
|
|
86
|
+
self.queue: list[Op] = []
|
|
87
|
+
self.applied: list[str] = []
|
|
88
|
+
|
|
89
|
+
def set(self, row_id, data, version=1):
|
|
90
|
+
op = Op("set", row_id, data, version=version)
|
|
91
|
+
if self.online and self.server is not None:
|
|
92
|
+
self.applied.append(self.server.apply(op))
|
|
93
|
+
self.local[row_id] = (version, dict(data))
|
|
94
|
+
else:
|
|
95
|
+
self.queue.append(op)
|
|
96
|
+
self.local[row_id] = (version, dict(data))
|
|
97
|
+
return op
|
|
98
|
+
|
|
99
|
+
def delete(self, row_id, version=1):
|
|
100
|
+
op = Op("delete", row_id, version=version)
|
|
101
|
+
if self.online and self.server is not None:
|
|
102
|
+
self.applied.append(self.server.apply(op))
|
|
103
|
+
self.local.pop(row_id, None)
|
|
104
|
+
else:
|
|
105
|
+
self.queue.append(op)
|
|
106
|
+
self.local.pop(row_id, None)
|
|
107
|
+
return op
|
|
108
|
+
|
|
109
|
+
def reconnect(self):
|
|
110
|
+
results = []
|
|
111
|
+
for op in self.queue:
|
|
112
|
+
results.append(self.server.apply(op) if self.server else "no-server")
|
|
113
|
+
n = len(self.queue)
|
|
114
|
+
self.queue.clear()
|
|
115
|
+
if self.server is not None:
|
|
116
|
+
for row_id, snap in self.server.snapshot().items():
|
|
117
|
+
self.local[row_id] = (snap["version"], dict(snap["data"]))
|
|
118
|
+
return {"replayed": n, "results": results}
|
pyweb/testing.py
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"""Testing helpers.
|
|
2
|
+
|
|
3
|
+
* :class:`TestClient` — in-process HTTP client for a `.pyweb` app. Pages
|
|
4
|
+
render exactly as in production, ``rpc()`` goes through the real RPC
|
|
5
|
+
endpoint (validation, errors, cookies), and cookies persist across
|
|
6
|
+
calls so sessions work::
|
|
7
|
+
|
|
8
|
+
from pyweb.testing import TestClient
|
|
9
|
+
|
|
10
|
+
client = TestClient("app.pyweb")
|
|
11
|
+
assert "Todos" in client.get("/").text
|
|
12
|
+
client.rpc("login", email="a@b.c", password="secret123")
|
|
13
|
+
assert client.get("/account").status == 200
|
|
14
|
+
|
|
15
|
+
* :func:`serve` — run the app on a free local port for real-browser tests
|
|
16
|
+
(e.g. Playwright)::
|
|
17
|
+
|
|
18
|
+
with serve("app.pyweb") as url:
|
|
19
|
+
page.goto(url)
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
import contextlib
|
|
25
|
+
import json
|
|
26
|
+
import threading
|
|
27
|
+
|
|
28
|
+
from .rpc import RPCError
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class TestResponse:
|
|
32
|
+
def __init__(self, status, headers, body):
|
|
33
|
+
self.status = status
|
|
34
|
+
self.headers = headers
|
|
35
|
+
self.body = body
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def text(self):
|
|
39
|
+
return self.body.decode("utf-8", "replace")
|
|
40
|
+
|
|
41
|
+
def json(self):
|
|
42
|
+
return json.loads(self.body)
|
|
43
|
+
|
|
44
|
+
def header(self, name):
|
|
45
|
+
name = name.lower()
|
|
46
|
+
return next((v for k, v in self.headers if k.lower() == name), None)
|
|
47
|
+
|
|
48
|
+
def __repr__(self):
|
|
49
|
+
return f"<TestResponse {self.status}>"
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class TestClient:
|
|
53
|
+
__test__ = False # not a pytest test class
|
|
54
|
+
|
|
55
|
+
def __init__(self, app="app.pyweb", *, source=None, **server_kwargs):
|
|
56
|
+
from .hosting import Site
|
|
57
|
+
if source is not None:
|
|
58
|
+
import os
|
|
59
|
+
import tempfile
|
|
60
|
+
tmp = tempfile.mkdtemp(prefix="pyweb-test-")
|
|
61
|
+
app = os.path.join(tmp, "app.pyweb")
|
|
62
|
+
with open(app, "w", encoding="utf-8") as fh:
|
|
63
|
+
fh.write(source)
|
|
64
|
+
self.site = Site(app, debug=True, **server_kwargs)
|
|
65
|
+
self.cookies = {}
|
|
66
|
+
|
|
67
|
+
@property
|
|
68
|
+
def app(self):
|
|
69
|
+
return self.site.app
|
|
70
|
+
|
|
71
|
+
def _headers(self, extra=None):
|
|
72
|
+
h = {"Host": "testserver"}
|
|
73
|
+
if self.cookies:
|
|
74
|
+
h["Cookie"] = "; ".join(f"{k}={v}" for k, v in self.cookies.items())
|
|
75
|
+
h.update(extra or {})
|
|
76
|
+
return h
|
|
77
|
+
|
|
78
|
+
def _store_cookies(self, headers):
|
|
79
|
+
for k, v in headers:
|
|
80
|
+
if k.lower() != "set-cookie":
|
|
81
|
+
continue
|
|
82
|
+
pair = v.split(";", 1)[0]
|
|
83
|
+
name, _, value = pair.partition("=")
|
|
84
|
+
if "Max-Age=0" in v or not value:
|
|
85
|
+
self.cookies.pop(name.strip(), None)
|
|
86
|
+
else:
|
|
87
|
+
self.cookies[name.strip()] = value.strip()
|
|
88
|
+
|
|
89
|
+
def request(self, method, path, body=b"", headers=None):
|
|
90
|
+
status, hdrs, raw = self.site.respond(method, path, self._headers(headers), body)
|
|
91
|
+
self._store_cookies(hdrs)
|
|
92
|
+
return TestResponse(status, hdrs, raw)
|
|
93
|
+
|
|
94
|
+
def get(self, path):
|
|
95
|
+
return self.request("GET", path)
|
|
96
|
+
|
|
97
|
+
def post(self, path, data=None, headers=None):
|
|
98
|
+
body = json.dumps(data).encode() if data is not None else b""
|
|
99
|
+
return self.request("POST", path, body, {"Content-Type": "application/json", **(headers or {})})
|
|
100
|
+
|
|
101
|
+
def rpc(self, function, /, **args):
|
|
102
|
+
"""Call a server function like the browser does; raise RPCError on failure."""
|
|
103
|
+
resp = self.post(f"/__pyweb/rpc/{function}", {"args": args})
|
|
104
|
+
payload = resp.json() if resp.body else {}
|
|
105
|
+
if resp.status != 200 or "error" in payload:
|
|
106
|
+
err = payload.get("error") or {}
|
|
107
|
+
raise RPCError(err.get("code", f"http_{resp.status}"), err.get("message", ""),
|
|
108
|
+
status=resp.status, details=err.get("details"))
|
|
109
|
+
return payload.get("result")
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
@contextlib.contextmanager
|
|
113
|
+
def serve(app="app.pyweb", *, host="127.0.0.1", **site_kwargs):
|
|
114
|
+
"""Serve ``app`` (a `.pyweb` file or built dist) on a free port; yield its base URL."""
|
|
115
|
+
import http.server
|
|
116
|
+
|
|
117
|
+
from .hosting import Site
|
|
118
|
+
from .serve import ThreadedServer
|
|
119
|
+
|
|
120
|
+
site = Site(app, **site_kwargs)
|
|
121
|
+
|
|
122
|
+
class Handler(http.server.BaseHTTPRequestHandler):
|
|
123
|
+
def _go(self, method, body=b""):
|
|
124
|
+
status, headers, raw = site.respond(method, self.path, dict(self.headers), body)
|
|
125
|
+
self.send_response(status)
|
|
126
|
+
for k, v in headers:
|
|
127
|
+
if k.lower() != "content-length":
|
|
128
|
+
self.send_header(k, v)
|
|
129
|
+
self.send_header("Content-Length", str(len(raw)))
|
|
130
|
+
self.end_headers()
|
|
131
|
+
if method != "HEAD":
|
|
132
|
+
self.wfile.write(raw)
|
|
133
|
+
|
|
134
|
+
def do_GET(self): # noqa: N802
|
|
135
|
+
self._go("GET")
|
|
136
|
+
|
|
137
|
+
def do_HEAD(self): # noqa: N802
|
|
138
|
+
self._go("HEAD")
|
|
139
|
+
|
|
140
|
+
def do_POST(self): # noqa: N802
|
|
141
|
+
n = int(self.headers.get("Content-Length", 0) or 0)
|
|
142
|
+
self._go("POST", self.rfile.read(n) if n else b"")
|
|
143
|
+
|
|
144
|
+
def log_message(self, *a):
|
|
145
|
+
pass
|
|
146
|
+
|
|
147
|
+
httpd = ThreadedServer((host, 0), Handler)
|
|
148
|
+
thread = threading.Thread(target=httpd.serve_forever, daemon=True)
|
|
149
|
+
thread.start()
|
|
150
|
+
try:
|
|
151
|
+
yield f"http://{host}:{httpd.server_address[1]}"
|
|
152
|
+
finally:
|
|
153
|
+
httpd.shutdown()
|
|
154
|
+
httpd.server_close()
|
pyweb/uploads.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"""Uploads: filename sanitizing, size/type validation, magic-byte
|
|
2
|
+
allowlist, storage paths."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import os
|
|
7
|
+
import re
|
|
8
|
+
import uuid
|
|
9
|
+
|
|
10
|
+
_UNSAFE = re.compile(r"[^A-Za-z0-9._-]+")
|
|
11
|
+
|
|
12
|
+
MAX_BYTES = 5 * 1024 * 1024
|
|
13
|
+
|
|
14
|
+
ALLOWED_EXTENSIONS = {".png", ".jpg", ".jpeg", ".gif", ".webp", ".pdf",
|
|
15
|
+
".txt", ".csv"}
|
|
16
|
+
|
|
17
|
+
_MAGIC_BYTES = (
|
|
18
|
+
(b"\x89PNG\r\n\x1a\n", {".png"}),
|
|
19
|
+
(b"\xff\xd8\xff", {".jpg", ".jpeg"}),
|
|
20
|
+
(b"GIF87a", {".gif"}),
|
|
21
|
+
(b"GIF89a", {".gif"}),
|
|
22
|
+
(b"RIFF", {".webp"}),
|
|
23
|
+
(b"%PDF-", {".pdf"}),
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class UploadRejected(Exception):
|
|
28
|
+
"""Raised when an upload fails allowlist validation."""
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class ValidatedUpload:
|
|
32
|
+
def __init__(self, filename, content):
|
|
33
|
+
self.filename = filename
|
|
34
|
+
self.content = content
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def size(self):
|
|
38
|
+
return len(self.content)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _is_textual(ext, content):
|
|
42
|
+
if ext not in {".txt", ".csv"}:
|
|
43
|
+
return False
|
|
44
|
+
try:
|
|
45
|
+
content.decode("utf-8")
|
|
46
|
+
except UnicodeDecodeError:
|
|
47
|
+
return False
|
|
48
|
+
return b"\x00" not in content
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def validate_content(filename, content, max_bytes=MAX_BYTES):
|
|
52
|
+
"""Allowlist uploads by extension AND magic bytes; enforce size caps."""
|
|
53
|
+
if len(content) > max_bytes:
|
|
54
|
+
raise UploadRejected(
|
|
55
|
+
f"upload too large: {len(content)} > {max_bytes} bytes")
|
|
56
|
+
base = os.path.basename(filename)
|
|
57
|
+
if not base or base != filename or "/" in filename or "\\" in filename:
|
|
58
|
+
raise UploadRejected("invalid filename")
|
|
59
|
+
_, ext = os.path.splitext(base)
|
|
60
|
+
ext = ext.lower()
|
|
61
|
+
if ext not in ALLOWED_EXTENSIONS:
|
|
62
|
+
raise UploadRejected(f"extension not allowed: {ext!r}")
|
|
63
|
+
if _is_textual(ext, content):
|
|
64
|
+
return ValidatedUpload(base, content)
|
|
65
|
+
for magic, exts in _MAGIC_BYTES:
|
|
66
|
+
if ext in exts and content.startswith(magic):
|
|
67
|
+
if ext == ".webp" and b"WEBP" not in content[:16]:
|
|
68
|
+
break
|
|
69
|
+
return ValidatedUpload(base, content)
|
|
70
|
+
raise UploadRejected("content does not match extension allowlist")
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def safe_filename(name):
|
|
74
|
+
base = os.path.basename((name or "").strip())
|
|
75
|
+
base = _UNSAFE.sub("_", base).strip("._")
|
|
76
|
+
return base or "file"
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def validate_upload(*, filename, size, content_type, allowed_types=(), max_bytes=10 * 1024 * 1024):
|
|
80
|
+
errors = []
|
|
81
|
+
clean = safe_filename(filename)
|
|
82
|
+
if clean != os.path.basename(filename or "") or ".." in (filename or ""):
|
|
83
|
+
errors.append("unsafe filename")
|
|
84
|
+
if size is None or size < 0:
|
|
85
|
+
errors.append("unknown size")
|
|
86
|
+
elif size > max_bytes:
|
|
87
|
+
errors.append(f"too large (max {max_bytes} bytes)")
|
|
88
|
+
if allowed_types and content_type not in allowed_types:
|
|
89
|
+
errors.append(f"unsupported type {content_type!r}")
|
|
90
|
+
return clean, errors
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def storage_path(filename, *, prefix="uploads"):
|
|
94
|
+
return f"{prefix}/{uuid.uuid4().hex[:12]}-{safe_filename(filename)}"
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyweb-stack
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Full-stack web apps in one Python file: server-rendered pages, reactive browser UI, typed RPC.
|
|
5
|
+
Author-email: MaanavKrishna <67054795+MaanavKrishna@users.noreply.github.com>, Claude <noreply@anthropic.com>
|
|
6
|
+
Maintainer-email: MaanavKrishna <67054795+MaanavKrishna@users.noreply.github.com>
|
|
7
|
+
License: MIT
|
|
8
|
+
Project-URL: Homepage, https://maanavkrishna.github.io/PyWeb/
|
|
9
|
+
Project-URL: Documentation, https://maanavkrishna.github.io/PyWeb/guide.html
|
|
10
|
+
Project-URL: Source, https://github.com/MaanavKrishna/PyWeb
|
|
11
|
+
Project-URL: Changelog, https://github.com/MaanavKrishna/PyWeb/blob/main/CHANGELOG.md
|
|
12
|
+
Project-URL: Issues, https://github.com/MaanavKrishna/PyWeb/issues
|
|
13
|
+
Keywords: web,framework,full-stack,reactive,rpc,ssr,compiler
|
|
14
|
+
Classifier: Development Status :: 4 - Beta
|
|
15
|
+
Classifier: Environment :: Web Environment
|
|
16
|
+
Classifier: Framework :: AsyncIO
|
|
17
|
+
Classifier: Intended Audience :: Developers
|
|
18
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
19
|
+
Classifier: Operating System :: OS Independent
|
|
20
|
+
Classifier: Programming Language :: JavaScript
|
|
21
|
+
Classifier: Programming Language :: Python :: 3
|
|
22
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
26
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
27
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
|
|
28
|
+
Classifier: Topic :: Software Development :: Compilers
|
|
29
|
+
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
|
30
|
+
Requires-Python: >=3.10
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
License-File: LICENSE
|
|
33
|
+
Provides-Extra: postgres
|
|
34
|
+
Requires-Dist: psycopg[binary]>=3.1; extra == "postgres"
|
|
35
|
+
Provides-Extra: mysql
|
|
36
|
+
Requires-Dist: PyMySQL>=1.1; extra == "mysql"
|
|
37
|
+
Provides-Extra: redis
|
|
38
|
+
Requires-Dist: redis>=5; extra == "redis"
|
|
39
|
+
Provides-Extra: crypto
|
|
40
|
+
Requires-Dist: cryptography>=42; extra == "crypto"
|
|
41
|
+
Provides-Extra: asgi
|
|
42
|
+
Requires-Dist: uvicorn>=0.30; extra == "asgi"
|
|
43
|
+
Provides-Extra: all
|
|
44
|
+
Requires-Dist: psycopg[binary]>=3.1; extra == "all"
|
|
45
|
+
Requires-Dist: PyMySQL>=1.1; extra == "all"
|
|
46
|
+
Requires-Dist: redis>=5; extra == "all"
|
|
47
|
+
Requires-Dist: cryptography>=42; extra == "all"
|
|
48
|
+
Requires-Dist: uvicorn>=0.30; extra == "all"
|
|
49
|
+
Provides-Extra: test
|
|
50
|
+
Requires-Dist: pytest>=8; extra == "test"
|
|
51
|
+
Requires-Dist: playwright>=1.45; extra == "test"
|
|
52
|
+
Dynamic: license-file
|
|
53
|
+
|
|
54
|
+
# PyWeb
|
|
55
|
+
|
|
56
|
+
**Full-stack web apps in one Python file.** Server-rendered pages,
|
|
57
|
+
reactive browser UI compiled from Python, and typed calls to server
|
|
58
|
+
functions, without a JavaScript toolchain.
|
|
59
|
+
|
|
60
|
+
[](https://github.com/MaanavKrishna/PyWeb/actions/workflows/ci.yml)
|
|
61
|
+
[](https://maanavkrishna.github.io/PyWeb/)
|
|
62
|
+

|
|
63
|
+

|
|
64
|
+

|
|
65
|
+
|
|
66
|
+
```pyweb
|
|
67
|
+
from pyweb import App, server
|
|
68
|
+
|
|
69
|
+
app = App(title="Guestbook")
|
|
70
|
+
ENTRIES = []
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@server
|
|
74
|
+
def sign(name: str) -> list:
|
|
75
|
+
ENTRIES.append(name.strip() or "anonymous")
|
|
76
|
+
return ENTRIES
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
@app.page("/")
|
|
80
|
+
def Home():
|
|
81
|
+
entries = list(ENTRIES) # computed on the server per request
|
|
82
|
+
name = "" # bound to the input: browser state
|
|
83
|
+
|
|
84
|
+
def submit(): # compiled to JavaScript
|
|
85
|
+
entries = sign(name) # typed RPC to the server
|
|
86
|
+
name = ""
|
|
87
|
+
|
|
88
|
+
<main>
|
|
89
|
+
<h1>Guestbook ({len(entries)})</h1>
|
|
90
|
+
<form onsubmit={submit}>
|
|
91
|
+
<input bind={name} placeholder="Your name" />
|
|
92
|
+
<button>Sign</button>
|
|
93
|
+
</form>
|
|
94
|
+
<ul>
|
|
95
|
+
for entry in entries:
|
|
96
|
+
<li>{entry}</li>
|
|
97
|
+
</ul>
|
|
98
|
+
</main>
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
pip install pyweb-stack # imported as `pyweb`
|
|
103
|
+
pyweb dev app.pyweb # http://localhost:8000
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Why PyWeb
|
|
107
|
+
|
|
108
|
+
- **Interactions run in the browser.** Handlers and expressions are
|
|
109
|
+
compiled to small JavaScript modules; the server is only contacted when
|
|
110
|
+
your code calls a `@server` function. No WebSocket per user, no
|
|
111
|
+
multi-megabyte Python runtime in the browser.
|
|
112
|
+
- **Every page is server-rendered.** Complete HTML on first paint, real
|
|
113
|
+
data from your database, good for SEO and slow devices.
|
|
114
|
+
- **You never write an API layer.** `@server` functions get endpoints,
|
|
115
|
+
argument validation, typed errors and generated browser calls.
|
|
116
|
+
- **Plain variables are state.** The compiler sees which variables your
|
|
117
|
+
handlers change and makes exactly those reactive; updates touch only
|
|
118
|
+
the DOM nodes that read them.
|
|
119
|
+
- **Boundaries are checked.** Database handles, imports and secrets can't
|
|
120
|
+
leak into browser code: it's a compile error with a line number.
|
|
121
|
+
`pyweb inspect` explains where every name runs and why.
|
|
122
|
+
- **Stateless servers.** Signed-cookie sessions and plain HTTP RPC scale
|
|
123
|
+
horizontally behind any load balancer. Deploy with `pyweb serve`,
|
|
124
|
+
uvicorn/gunicorn (ASGI) or the generated Dockerfile.
|
|
125
|
+
|
|
126
|
+
A typical interactive page ships under 1 KB of page code plus a ~10 KB
|
|
127
|
+
(gzip) runtime that's cached across pages. Pages without interactivity
|
|
128
|
+
ship no JavaScript.
|
|
129
|
+
|
|
130
|
+
## Is it for you?
|
|
131
|
+
|
|
132
|
+
**Good fit:** internal tools, admin panels, dashboards, CRUD apps,
|
|
133
|
+
small SaaS products and content sites with interactive parts, built by
|
|
134
|
+
people who'd rather stay in Python.
|
|
135
|
+
|
|
136
|
+
**Not a fit:** large client-heavy single-page apps that need the npm
|
|
137
|
+
ecosystem (use React/Svelte/Vue), or running scientific Python in the
|
|
138
|
+
browser (use Pyodide/PyScript). See the
|
|
139
|
+
[comparison](https://maanavkrishna.github.io/PyWeb/introduction.html)
|
|
140
|
+
and [current limitations](docs/16-limitations-roadmap.md).
|
|
141
|
+
|
|
142
|
+
## Documentation
|
|
143
|
+
|
|
144
|
+
| | |
|
|
145
|
+
|---|---|
|
|
146
|
+
| Start | [Introduction](docs/01-introduction.md) · [Quickstart](docs/02-quickstart.md) · [Tutorial](docs/03-tutorial.md) |
|
|
147
|
+
| Language | [`.pyweb` files](docs/04-pyweb-files.md) · [State & reactivity](docs/05-reactivity.md) · [Python in the browser](docs/07-browser-python.md) |
|
|
148
|
+
| Server | [Server functions & RPC](docs/06-server-functions.md) · [Pages & routing](docs/08-pages-routing-assets.md) · [Data](docs/09-data.md) · [Auth](docs/10-auth.md) |
|
|
149
|
+
| Ship | [Testing](docs/11-testing.md) · [Deployment](docs/12-deployment.md) · [Security](docs/13-security.md) · [CLI](docs/14-cli.md) |
|
|
150
|
+
| Reference | [Toolkit & stability](docs/15-toolkit.md) · [Limitations & roadmap](docs/16-limitations-roadmap.md) · [Architecture](ARCHITECTURE.md) · [Changelog](CHANGELOG.md) |
|
|
151
|
+
|
|
152
|
+
The same docs are published at
|
|
153
|
+
**[maanavkrishna.github.io/PyWeb](https://maanavkrishna.github.io/PyWeb/)**,
|
|
154
|
+
with compiler output shown next to each example.
|
|
155
|
+
|
|
156
|
+
## Examples
|
|
157
|
+
|
|
158
|
+
Each runs with `pyweb dev examples/<name>/app.pyweb` and is exercised in
|
|
159
|
+
a real browser by the test suite.
|
|
160
|
+
|
|
161
|
+
| Example | Shows |
|
|
162
|
+
|---|---|
|
|
163
|
+
| [`counter`](examples/counter/app.pyweb) | signals, computed values, binding a number input |
|
|
164
|
+
| [`todo`](examples/todo/app.pyweb) | components, list mutation, filters, keyed lists |
|
|
165
|
+
| [`blog`](examples/blog/app.pyweb) | SQL database, server functions, route params, 404s, validation errors |
|
|
166
|
+
| [`auth`](examples/auth/app.pyweb) | registration, password hashing, sessions, protected pages |
|
|
167
|
+
| [`chat`](examples/chat/app.pyweb) | route params, shared server state, polling with `on_mount` |
|
|
168
|
+
| [`showcase`](examples/showcase/app.pyweb) | everything on one page, with a stylesheet |
|
|
169
|
+
|
|
170
|
+
## Command line
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
pyweb new myapp # scaffold
|
|
174
|
+
pyweb dev app.pyweb # dev server: live reload + error overlay
|
|
175
|
+
pyweb inspect app.pyweb # where each name runs, and why
|
|
176
|
+
pyweb check app.pyweb # compile + security checks for CI
|
|
177
|
+
pyweb build app.pyweb --out dist --production # self-contained, hashed, minified dist/
|
|
178
|
+
pyweb serve dist # production server (/healthz, CSP, graceful shutdown)
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Status
|
|
182
|
+
|
|
183
|
+
PyWeb 0.1 is the first public release (beta). The language, server API,
|
|
184
|
+
RPC protocol and CLI are documented and tested, and changes to them are
|
|
185
|
+
announced in the changelog (see [stability](docs/15-toolkit.md#stability)). The test suite covers the
|
|
186
|
+
parser, the Python→JavaScript translation (differentially, against
|
|
187
|
+
CPython), the reactive runtime, server rendering, RPC, sessions, every
|
|
188
|
+
example app in Chromium, and the database/Redis layers against real
|
|
189
|
+
Postgres, MySQL and Redis servers, on Python 3.10–3.13.
|
|
190
|
+
|
|
191
|
+
## Authors
|
|
192
|
+
|
|
193
|
+
Built by [MaanavKrishna](https://github.com/MaanavKrishna) and Claude.
|
|
194
|
+
|
|
195
|
+
## Contributing
|
|
196
|
+
|
|
197
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md). Security reports:
|
|
198
|
+
[SECURITY.md](SECURITY.md). License: MIT.
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
pyweb/__init__.py,sha256=KzWzFhfv4UGkoeYJdLGbSiDt_jH6qTfM4OgILwREQbk,1668
|
|
2
|
+
pyweb/app.py,sha256=CoUeVarF7b1M5lZ82JNu4FhgAgIFuov6Ty53JGTvkho,1292
|
|
3
|
+
pyweb/app_loader.py,sha256=llgmM4mjg_CtnK-3lOZHWXLGdpPeLnT-NIDcGeQ51xc,5735
|
|
4
|
+
pyweb/asgi.py,sha256=9ijkJlh-ZReHV56tW7eht8lXBj6gNJsGpuAQOZJs4Yg,2776
|
|
5
|
+
pyweb/auth.py,sha256=ZxBDaYExQ1BgICIVdcc2gJurhr0QU1J02jFr1J_3ePU,25878
|
|
6
|
+
pyweb/bench.py,sha256=mVphqhIK4Vzqs5bwI9u2CI0Ee3YH-w5i0e0-EwyvU2U,4266
|
|
7
|
+
pyweb/browser.py,sha256=JXUoxthuk7Rhd6TBvTCA0Y16zDgUdHx3EckpZ0YZqMQ,5217
|
|
8
|
+
pyweb/build.py,sha256=-ba44Vt0WwNkdskjC1ugZceWQJnMztnCpv0awBl0WPU,11459
|
|
9
|
+
pyweb/cache.py,sha256=unbw9-DAj9HUdH7zlbw6GeRQnBZD6vZkc20AMaY-Ohg,6222
|
|
10
|
+
pyweb/context.py,sha256=CN1OutPJXFVBQaEoK6L5kyNV2rkrRTnyJuGm49aQ6UY,5780
|
|
11
|
+
pyweb/css.py,sha256=q6t52iAzNu-Nq_qwraD7NJoeKmWUKhKvZhCk7YLOV_g,3834
|
|
12
|
+
pyweb/decorators.py,sha256=u2OKDA43EzDvVuDItOlJVxfbuUwErZDSIIVfOSehZoY,1446
|
|
13
|
+
pyweb/deploy.py,sha256=-UI4BWy0p7rH-wd6MQs_5FurKHxCu25aCcfUtDYDRZI,3340
|
|
14
|
+
pyweb/forms.py,sha256=ruf0EbdsyrDHVy_LADsYD8J9PwmQO4azwJrGxmkzhzo,3988
|
|
15
|
+
pyweb/hosting.py,sha256=ua5DhmJuO3cIE5xmakRvTgNwtU1WFafq6252KmxiZEI,6499
|
|
16
|
+
pyweb/jobs.py,sha256=NEOVWZDk_Hn3UQ58Cgf9WyE5NUn6SHjkzjdlsNPPbsI,8909
|
|
17
|
+
pyweb/live.py,sha256=U7KM7PO__sVbtm9LXAfsT8k62tx6Fuye0FHVXsrgcyM,2765
|
|
18
|
+
pyweb/lsp.py,sha256=P2bktYh2aaV4vJ1XR5N7CwkoTMVJ4v2wWlALibsTeKg,8327
|
|
19
|
+
pyweb/models.py,sha256=hEkyIZS8JMfajMuB9CdNRGiaKDcXVKbCA7_d4MOMFC0,8844
|
|
20
|
+
pyweb/npm.py,sha256=F9HVlZyrSRGfuHGTDsDt42PdGgLNjWX2gBMEdRwxlzY,9896
|
|
21
|
+
pyweb/observability.py,sha256=rGDUZrAE7fj-O25zOWWJwY5GmVX3P-OPPDZBamL2nTk,10393
|
|
22
|
+
pyweb/platform.py,sha256=-T4_NCCpsFwAE5ucJtStCNxm9q-mZSHkpzBC4BTclcY,2206
|
|
23
|
+
pyweb/plugins.py,sha256=ZqWbbdQeQ9-fdJIzwV6iQeQpa8wpkN8DmRq5W7b7vgM,3592
|
|
24
|
+
pyweb/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
25
|
+
pyweb/realtime.py,sha256=LI_NZRkhWHgsB-VLcHqnCLnsHw9zUbSkotuQgqqFAEI,6904
|
|
26
|
+
pyweb/rpc.py,sha256=wvLnrrrEdv3KnVjBSEU25_PsUbL8Ugt0fMudf8HBQRI,4944
|
|
27
|
+
pyweb/security.py,sha256=_zzZJRRVDxXxkQzYxgClZQO-7O1TrqBb2p8Cnmwufa4,5995
|
|
28
|
+
pyweb/serve.py,sha256=ivcsGeQtmX_OgHnyo8CnjkSCkh0dMevKlr5B-jgFMzU,14419
|
|
29
|
+
pyweb/ssr.py,sha256=jc1ank1-Z530tHD9sh_DMyJgXLh_e4T6NQOuw0xXBas,9956
|
|
30
|
+
pyweb/sync.py,sha256=_kkwk5zQFBjrX0OdXAmXbCtct3iTR0NIIHXa4Lnbnvc,4458
|
|
31
|
+
pyweb/testing.py,sha256=7-xrLWzn4n7OU_0waiAL95U-SVSx0fVd1hPJzyiscEw,5056
|
|
32
|
+
pyweb/uploads.py,sha256=5ThhLKbrj75zTwKa7jt3up_dEue-Nk-FsBQiTUqx5nE,2887
|
|
33
|
+
pyweb/cli/__init__.py,sha256=g2GF8xEA1csdA1f6Ns4XnpVSG1xrbQEE6ktqy8v2drs,19716
|
|
34
|
+
pyweb/cli/__main__.py,sha256=ubt6XyZvcHwdjCQ90B_U4MeP-3YJduz5GWUC9yOgkrg,27
|
|
35
|
+
pyweb/compiler/__init__.py,sha256=2WC2v4fzgDygNBpA4tvWEQtavMZAnnxaYskWvQ6MPFU,148
|
|
36
|
+
pyweb/compiler/ast.py,sha256=Wrlq_C5V-TMuQAvSK4V517V-gM0Vonq7KYFgUTOmpE4,679
|
|
37
|
+
pyweb/compiler/errors.py,sha256=ge7JsZUHNeHnP313sjUdaqxZmPnWAzQGSc03L7bt_wQ,819
|
|
38
|
+
pyweb/compiler/lower.py,sha256=vg_41mfdLamqEhzR_pQOtfKKE00dv5pNfl0oxpP9pqQ,31701
|
|
39
|
+
pyweb/compiler/parser.py,sha256=p7iX-wv5NWsCA_256xvaegljcN6BfGe3V6a1D3dolzg,18191
|
|
40
|
+
pyweb/compiler/pipeline.py,sha256=WlvvOi3tk-kwRhmIKXsfh5g01VSHm2ycflmPCz_yUyY,6885
|
|
41
|
+
pyweb/compiler/pyjs.py,sha256=tftTG6gyyqvW5RNW-2Xnc2SqMPkYHZmYAQYB_fpk32w,46312
|
|
42
|
+
pyweb/compiler/rpc.py,sha256=ErOMUZqyjQ6DYQJdjcDtRM_l3aBkYhKGth1cnYCdqZ8,1348
|
|
43
|
+
pyweb/compiler/codegen/__init__.py,sha256=JzX5LJwTLh_tBCH7JQG1RWgysIXbMxrh47hGkS1CuIY,250
|
|
44
|
+
pyweb/compiler/codegen/ir.py,sha256=s6fPk_jNVJkINdiHg2y1bru0317alR0sOKX1zcmCowk,1563
|
|
45
|
+
pyweb/db/__init__.py,sha256=YvTJYVOjY3hStIG3zztcIFOcS9SN1w08npOdUNOZOaA,22093
|
|
46
|
+
pyweb/db/migrate.py,sha256=xpujP2mX1EUAiA_hZzeFW6cRtgwS07gMeiAfrmJG2H0,5419
|
|
47
|
+
pyweb/runtime/browser/runtime.js,sha256=LQVDWUID09CacHHA8R1TvEbtUi4EkJgtbDnw-bwZS54,40808
|
|
48
|
+
pyweb/runtime/server/__init__.py,sha256=rNr05rChG_-qMD_3wyKzuIzw1t2DqytI2qOfFj_ciBM,21019
|
|
49
|
+
pyweb_stack-0.1.0.dist-info/licenses/LICENSE,sha256=r7ySlQXYhVH8rdTZibIPbmVXvvNYcHyXzndJq73l_aM,1081
|
|
50
|
+
pyweb_stack-0.1.0.dist-info/METADATA,sha256=AX4SWadUjNWUpWBFD6g72MNL7rwAfZltFwU-hxWDqkg,8712
|
|
51
|
+
pyweb_stack-0.1.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
52
|
+
pyweb_stack-0.1.0.dist-info/entry_points.txt,sha256=pJMhV5m72eoRgYpY4eoJjtNpOfpUiqYK2FAtYJXGQoA,41
|
|
53
|
+
pyweb_stack-0.1.0.dist-info/top_level.txt,sha256=e6Ok626FGJgHAn0XPcrrsZmXJMN7wPRXgMYbGE4R0S4,6
|
|
54
|
+
pyweb_stack-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 MaanavKrishna and Claude
|
|
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 @@
|
|
|
1
|
+
pyweb
|