This directory contains examples demonstrating all the features of Crank.py. Each example showcases different patterns and capabilities of the framework.
-
Start a local server:
python -m http.server 8000
-
Open examples in your browser:
- Visit
http://localhost:8000/examples/ - Click on any
.htmlfile to run the example
- Visit
greeting.py - Hello World
Basic component rendering
- Simple component with no state
- Demonstrates
h.div["content"]syntax - Shows basic component rendering
counter.py - Interactive Counter
State management with lifecycle decorators
- Uses
@ctx.refreshdecorators for clean event handling - Demonstrates internal component state
- Shows button click handling
animated_letters.py - Dynamic Animations
Advanced lifecycle methods and animations
- Uses
@ctx.afterfor DOM manipulation - Uses
@ctx.cleanupfor resource cleanup - Demonstrates interval-based updates
- Shows CSS transitions and transforms
props_example.py - Props Patterns
Different component signature styles
- Shows all three component signatures (0, 1, 2 params)
- Demonstrates props reassignment pattern
- Examples of
ctx.propsvs direct props parameter
showcase.py - Complete Demo
Full-featured application
- Real-time clock component
- Complete TodoMVC implementation
- Hyperscript syntax showcase
- Demonstrates all major framework features
# Static component (0 params)
@component
def Logo():
return h.div["Crank.py"]
# Context only (1 param)
@component
def Timer(ctx):
for _ in ctx:
yield h.div[f"Time: {time.time()}"]
# Context + Props (2 params)
@component
def UserCard(ctx, props):
for props in ctx: # Props reassignment!
yield h.div[f"Hello, {props.name}"]@component
def MyComponent(ctx):
@ctx.refresh
def handle_click():
# Automatically triggers re-render
pass
@ctx.after
def after_render(node):
# Runs after DOM update
node.style.color = "blue"
@ctx.cleanup
def cleanup():
# Runs when component unmounts
pass# HTML elements
h.div["content"]
h.input(type="text", value="hello")
h.div(className="styled")["content"]
# Components
h(MyComponent)
h(MyComponent, prop="value")
# Fragments (just use lists!)
["child1", "child2"]
[h.span["Item 1"], h.span["Item 2"]]@component
def DynamicComponent(ctx, props):
for props in ctx: # New props each iteration
# Component updates when parent changes props
yield h.div[f"Current value: {props.value}"]Each example includes:
.pyfile - The Python component code.htmlfile - PyScript setup and styling- Styling - CSS for visual presentation
All examples are covered by the test suite in ../tests/:
- Browser tests using Playwright
- Component pattern validation
- Hyperscript syntax verification
- Lifecycle decorator testing
Recommended order for learning:
greeting.py- Start here for basic conceptscounter.py- Learn state and event handlingprops_example.py- Understand props patternsanimated_letters.py- Explore advanced lifecycleshowcase.py- See everything together
- Main README - Installation and setup
- Crank.js Examples - Original JavaScript versions
- PyScript Documentation - Browser Python environment
Built with Crank.py - Python Frontend Framework with Async/Generators, Powered by Crank.js