-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathshowcase.py
More file actions
262 lines (222 loc) · 7.54 KB
/
showcase.py
File metadata and controls
262 lines (222 loc) · 7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
"""
Crank.py Showcase - Demonstrates all major features
"""
import time
from js import document
from crank import component, h
from crank.dom import renderer
# 1. Simple static component (0 params)
@component
def Logo():
return h.div(className="logo")[
h.h1["🔧 Crank.py"],
h.p["Python Frontend Framework with Async/Generators, Powered by Crank.js"]
]
# 2. Component with context only (1 param)
@component
def Clock(ctx):
"""Updates every second to show current time"""
import asyncio
async def update_time():
while True:
await asyncio.sleep(1)
ctx.refresh()
# Start the async update loop
asyncio.create_task(update_time())
for _ in ctx:
current_time = time.strftime("%H:%M:%S")
yield h.div(className="clock")[
h.span["Current time: "],
h.strong[current_time]
]
# 3. Component with context and props (2 params)
@component
def TodoItem(ctx, props):
"""Individual todo item with toggle functionality"""
for props in ctx:
todo = props["todo"]
on_toggle = props["on_toggle"]
yield h.li(className="completed" if todo["done"] else "")[
h.input(
type="checkbox",
checked=todo["done"],
onchange=lambda: on_toggle(todo["id"])
),
h.span[todo["text"]],
h.small[f" (ID: {todo['id']})"]
]
# 4. Complex stateful component demonstrating all features
@component
def TodoApp(ctx):
"""Main todo application with full CRUD operations"""
todos = [
{"id": 1, "text": "Learn Crank.py", "done": True},
{"id": 2, "text": "Build awesome components", "done": False},
{"id": 3, "text": "Master generators", "done": False}
]
next_id = 4
new_todo_text = ""
filter_mode = "all" # all, active, completed
@ctx.refresh
def add_todo():
nonlocal todos, next_id, new_todo_text
if new_todo_text.strip():
todos.append({
"id": next_id,
"text": new_todo_text.strip(),
"done": False
})
next_id += 1
new_todo_text = ""
@ctx.refresh
def toggle_todo(todo_id):
nonlocal todos
for todo in todos:
if todo["id"] == todo_id:
todo["done"] = not todo["done"]
break
@ctx.refresh
def update_input(event):
nonlocal new_todo_text
new_todo_text = event.target.value
def handle_submit(event):
event.preventDefault()
add_todo()
@ctx.refresh
def set_filter(mode):
nonlocal filter_mode
filter_mode = mode
@ctx.refresh
def clear_completed():
nonlocal todos
todos = [t for t in todos if not t["done"]]
for _ in ctx:
# Filter todos based on current mode
if filter_mode == "active":
visible_todos = [t for t in todos if not t["done"]]
elif filter_mode == "completed":
visible_todos = [t for t in todos if t["done"]]
else:
visible_todos = todos
completed_count = sum(1 for t in todos if t["done"])
active_count = len(todos) - completed_count
yield h.div(className="todo-app")[
h.h2["Todo List"],
# Add todo form
h.form(onSubmit=handle_submit, className="add-form")[
h.input(
type="text",
placeholder="What needs to be done?",
value=new_todo_text,
oninput=update_input,
className="todo-input"
),
h.button(type="submit")["Add"]
],
# Filter buttons
h.div(className="filters")[
h.button(
className="active" if filter_mode == "all" else "",
onclick=lambda: set_filter("all")
)["All"],
h.button(
className="active" if filter_mode == "active" else "",
onclick=lambda: set_filter("active")
)[f"Active ({active_count})"],
h.button(
className="active" if filter_mode == "completed" else "",
onclick=lambda: set_filter("completed")
)[f"Completed ({completed_count})"]
],
# Todo list
h.ul(className="todo-list")[
[h(TodoItem,
todo=todo,
on_toggle=toggle_todo,
key=todo["id"]
) for todo in visible_todos]
],
# Footer
h.div(className="todo-footer")[
h.span[f"{active_count} item{'s' if active_count != 1 else ''} left"],
h.button(
onclick=clear_completed,
disabled=completed_count == 0
)["Clear completed"]
] if todos else None
]
# 5. Component demonstrating hyperscript syntax variations
@component
def SyntaxShowcase(ctx):
"""Shows different ways to use the h function"""
for _ in ctx:
yield h.div(className="syntax-showcase")[
h.h3["Hyperscript Syntax Examples"],
# Basic element with text
h.p["Simple paragraph with text"],
# Element with props
h.p(className="styled", id="my-paragraph")["Paragraph with props"],
# Nested elements
h.ul[
h.li["First item"],
h.li["Second item"],
h.li[
"Third item with ",
h.strong["nested"],
" content"
]
],
# Element with style object
h.div(style={
"background-color": "#f0f0f0",
"padding": "10px",
"border-radius": "5px"
})[
"Styled div with object notation"
],
# Fragment with key prop (when you need fragment properties)
h("", key="fragment-example")[
h.span["Fragment "],
h.span["with "],
h.span["multiple "],
h.span["children"]
],
# Component composition
h.div(className="composition")[
"Nested component: ",
h(Logo)
]
]
# 6. Main showcase app that combines everything
@component
def ShowcaseApp(ctx):
"""Main application demonstrating all Crank.py features"""
for _ in ctx:
yield h.div(className="showcase-container")[
h(Logo),
h.div(className="features")[
h.section[
h.h2["⏰ Real-time Updates"],
h(Clock)
],
h.section[
h.h2["📝 Stateful Components"],
h(TodoApp)
],
h.section[
h.h2["🎨 Syntax Flexibility"],
h(SyntaxShowcase)
]
],
h.footer[
h.p["Built with ", h.strong["Crank.py"], " - Python Components for the Web"],
h.p[
h.a(href="https://crank.js.org", target="_blank")["Crank.js"],
" | ",
h.a(href="https://github.com/bikeshaving/crankpy", target="_blank")["GitHub"]
]
]
]
# Render the showcase app
if __name__ == "__main__":
renderer.render(h(ShowcaseApp), document.body)