Skip to content

Commit 396f8da

Browse files
committed
Improve client/server wrappers
1 parent accfdc0 commit 396f8da

10 files changed

Lines changed: 372 additions & 273 deletions

File tree

examples/basic_pwa.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
print(app.version)
88

99

10-
@app.route("/", pwa=True)
10+
@app.view("/", pwa=True)
1111
def index():
1212
doc = Document(title=f"Auto PWA")
1313
doc.body.extend(

examples/broadcast.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
# --- 1. Client Side (Updates the UI) ---
11-
@app.client
11+
@app.client.realtime
1212
async def update_counter(count: int):
1313
# This runs in the User's Browser
1414
from violetear.dom import Document
@@ -18,12 +18,12 @@ async def update_counter(count: int):
1818
el.style(color="red" if count % 2 == 0 else "blue")
1919

2020

21-
@app.connect
21+
@app.server.on("connect")
2222
async def enter(id: str):
2323
print(f"Client connected {id}")
2424

2525

26-
@app.disconnect
26+
@app.server.on("disconnect")
2727
async def exit(id: str):
2828
print(f"Client disconnected {id}")
2929

@@ -41,21 +41,13 @@ async def background_pinger():
4141

4242

4343
# --- Lifecycle Management ---
44-
@asynccontextmanager
45-
async def lifespan(api):
46-
# Startup
47-
task = asyncio.create_task(background_pinger())
48-
yield
49-
# Shutdown (optional cleanup)
50-
task.cancel()
51-
52-
53-
# Hook into the internal FastAPI router to register lifespan
54-
app.api.router.lifespan_context = lifespan
44+
@app.server.on("startup")
45+
async def start_pinger():
46+
asyncio.create_task(background_pinger())
5547

5648

5749
# --- 3. The UI (HTML) ---
58-
@app.route("/")
50+
@app.view("/")
5951
def home():
6052
# 1. Create the Document
6153
doc = Document(title="Live Counter")

examples/full_pwa.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ async def change_color(event: Event):
6161

6262
# --- 3. Define Route ---
6363
# pwa=True enables the Service Worker
64-
@app.route("/", pwa=True)
64+
@app.view("/", pwa=True)
6565
def index():
6666
doc = Document(title="Cached PWA")
6767

examples/hello_world.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
# 3. Define your Routes (The "Controller")
18-
@app.route("/")
18+
@app.view("/")
1919
def index():
2020
doc = Document(title="My Styled App")
2121

examples/quickstart.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030

3131
# --- 2. THE SERVER (FastAPI RPC) ---
32-
@app.server
32+
@app.server.rpc
3333
async def report_count(current_count: int, action: str):
3434
"""
3535
This runs on the server.
@@ -42,7 +42,7 @@ async def report_count(current_count: int, action: str):
4242
# --- 3. THE CLIENT (Pyodide Browser) ---
4343

4444

45-
@app.client
45+
@app.client.callback
4646
async def handle_change(event: Event):
4747
"""
4848
Runs in the browser.
@@ -69,7 +69,7 @@ async def handle_change(event: Event):
6969
await report_count(current_count=new_value, action=action)
7070

7171

72-
@app.startup
72+
@app.client.on("ready")
7373
async def init_counter():
7474
"""
7575
Runs automatically when the page loads (Client-Side).
@@ -89,7 +89,7 @@ async def init_counter():
8989
# --- 4. THE UI (Server-Side Rendered) ---
9090

9191

92-
@app.route("/")
92+
@app.view("/")
9393
def index():
9494
doc = Document(title="Violetear Counter")
9595
doc.style(style, href="/style.css") # Link our style

examples/rpc_call.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class UserData(BaseModel):
1313

1414

1515
# 2. Define the Server Logic
16-
@app.server
16+
@app.server.rpc
1717
async def create_user(name: str, age: int) -> UserData:
1818
# This runs on the SERVER.
1919
# Pydantic validation happens automatically here!
@@ -24,7 +24,7 @@ async def create_user(name: str, age: int) -> UserData:
2424

2525

2626
# 3. Define the Client Logic
27-
@app.client
27+
@app.client.callback
2828
async def on_submit(event: Event):
2929
from violetear.dom import Document
3030

@@ -42,7 +42,7 @@ async def on_submit(event: Event):
4242

4343

4444
# 4. The UI
45-
@app.route("/")
45+
@app.view("/")
4646
def index():
4747
doc = Document(title="RPC Demo")
4848

examples/simple_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
# 2. Define Client-Side Code
99
# This function is compiled into the bundle and sent to the browser.
10-
@app.client
11-
def on_button_click(event):
10+
@app.client.callback
11+
async def on_button_click(event):
1212
# This print shows up in the Browser DevTools Console!
1313
print("Hello from Client-Side Python!")
1414

@@ -19,7 +19,7 @@ def on_button_click(event):
1919

2020

2121
# 3. Define Server-Side Route
22-
@app.route("/")
22+
@app.view("/")
2323
def home():
2424
doc = Document(title="Client-Side Demo")
2525

0 commit comments

Comments
 (0)