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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
---
title: Pomodoro Timer
emoji: ⏲️🌳
colorFrom: red
colorTo: red
sdk: gradio
sdk_version: 6.5.1
app_file: pomodoro_forest.py
pinned: false
license: mit
short_description: 'Gamified Pomodoro Timer: a pixel-art tree grows as you focus'
---

# 🍅 PomodoroTimer Component Documentation

A gamified Pomodoro timer built with Gradio 6's `gr.HTML` component. Watch pixel-art trees grow as you stay focused, and build a forest over time!

---

## Table of Contents

- [Installation](#installation)
- [Quick Start](#quick-start)
- [Parameters](#parameters)
- [Value Schema](#value-schema)
- [Events](#events)
- [Tree Themes](#tree-themes)
- [Examples](#examples)
  - [Basic Usage](#basic-usage)
  - [Custom Durations](#custom-durations)
  - [Listening to Events](#listening-to-events)
  - [Updating the Timer Programmatically](#updating-the-timer-programmatically)
- [API Usage](#api-usage)
- [MCP (Model Context Protocol) Usage](#mcp-usage)
- [Customization](#customization)
- [Best Practices](#best-practices)

---

## Installation

The component is a single Python file. Copy `pomodoro_forest.py` into your project or import the `PomodoroTimer` class directly.

**Requirements:**
- Gradio 6.0+
- Python 3.9+

```bash
pip install "gradio>=6.0"
```

---

## Quick Start

```python
import gradio as gr
from pomodoro_forest import PomodoroTimer

with gr.Blocks() as demo:
    timer = PomodoroTimer()

demo.launch()
```

---

## Parameters

| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `value` | `dict` | See below | Timer state (elapsed time, sessions, etc.) |
| `duration` | `int` | `25` | Duration of the current mode in minutes |
| `mode` | `str` | `"focus"` | Current mode: `"focus"`, `"short_break"`, or `"long_break"` |
| `tree_theme` | `str` | `"classic"` | Visual theme for the tree (see [Tree Themes](#tree-themes)) |
| `mode_color` | `str` | Auto | Override the mode's accent color (hex) |
| `trunk_color` | `str` | Auto | Override trunk color (hex) |
| `crown_color` | `str` | Auto | Override crown/leaves color (hex) |
| `crown_top_color` | `str` | Auto | Override crown top color (hex) |
| `fruit_color` | `str` | Auto | Override fruit color (hex) |

---

## Value Schema

The `value` parameter is a dictionary with the following structure:

```python
{
    "elapsed": int,        # Seconds elapsed in current session (0 to duration*60)
    "running": bool,       # Whether the timer is currently running
    "sessions": int,       # Number of completed focus sessions (trees grown)
    "total_minutes": int   # Total minutes spent in focus mode
}
```

**Default value:**
```python
{"elapsed": 0, "running": False, "sessions": 0, "total_minutes": 0}
```

**API Schema (JSON):**
```json
{
    "type": "object",
    "properties": {
        "elapsed": {"type": "integer"},
        "running": {"type": "boolean"},
        "sessions": {"type": "integer"},
        "total_minutes": {"type": "integer"}
    }
}
```

---

## Events

The PomodoroTimer component emits the following events:

| Event | Trigger | Event Data | Description |
|-------|---------|------------|-------------|
| `.submit()` | Session completes | None | Fired when a focus/break session reaches 100% |
| `.select()` | Mode button clicked | `{"mode": str}` | Fired when user clicks Focus/Short Break/Long Break |
| `.change()` | Value changes | None | Fired on any value change (every second while running) |

### Accessing Event Data

```python
def handle_mode_select(evt: gr.EventData, timer_val):
    # Safely access the mode from event data
    new_mode = evt._data.get("mode", "focus") if evt._data else "focus"
    return new_mode

timer.select(fn=handle_mode_select, inputs=[timer], outputs=[...])
```

---

## Tree Themes

Five built-in themes are available:

| Theme | Trunk | Crown | Fruit | Best For |
|-------|-------|-------|-------|----------|
| `classic` | Brown | Green | Red | Default look |
| `cherry` | Dark brown | Pink | Hot pink | Spring vibes |
| `autumn` | Brown | Orange | Deep orange | Fall season |
| `winter` | Gray | Silver | Light blue | Winter/holidays |
| `sakura` | Dark brown | Light pink | Pink | Japanese aesthetic |

**Theme colors (for reference):**
```python
TREE_THEMES = {
    "classic": {"trunk": "#8B5E3C", "crown": "#2ecc71", "crown_top": "#00b894", "fruit": "#e74c3c"},
    "cherry":  {"trunk": "#5D4037", "crown": "#F8BBD9", "crown_top": "#F48FB1", "fruit": "#E91E63"},
    "autumn":  {"trunk": "#6D4C41", "crown": "#FF9800", "crown_top": "#FFC107", "fruit": "#FF5722"},
    "winter":  {"trunk": "#455A64", "crown": "#B0BEC5", "crown_top": "#ECEFF1", "fruit": "#81D4FA"},
    "sakura":  {"trunk": "#4E342E", "crown": "#FCE4EC", "crown_top": "#F8BBD0", "fruit": "#EC407A"},
}
```

---

## Examples

### Basic Usage

```python
import gradio as gr
from pomodoro_forest import PomodoroTimer

with gr.Blocks() as demo:
    gr.Markdown("# My Pomodoro App")
    timer = PomodoroTimer(duration=25, mode="focus", tree_theme="classic")

demo.launch()
```

### Custom Durations

```python
import gradio as gr
from pomodoro_forest import PomodoroTimer, _update_timer

with gr.Blocks() as demo:
    timer = PomodoroTimer(duration=50, mode="focus")  # 50-minute focus
    
    # Quick preset buttons
    with gr.Row():
        btn_25 = gr.Button("25 min")
        btn_50 = gr.Button("50 min")
    
    btn_25.click(
        fn=lambda v: _update_timer({**v, "elapsed": 0}, 25, "focus", "classic"),
        inputs=[timer],
        outputs=[timer]
    )
    btn_50.click(
        fn=lambda v: _update_timer({**v, "elapsed": 0}, 50, "focus", "classic"),
        inputs=[timer],
        outputs=[timer]
    )

demo.launch()
```

### Listening to Events

```python
import gradio as gr
from pomodoro_forest import PomodoroTimer, _update_timer

with gr.Blocks() as demo:
    timer = PomodoroTimer()
    status = gr.Textbox(label="Status")
    
    # When a session completes
    def on_complete(timer_val):
        sessions = timer_val.get("sessions", 0)
        return f"🎉 Congratulations! You've grown {sessions} trees!"
    
    timer.submit(fn=on_complete, inputs=[timer], outputs=[status])

demo.launch()
```

### Updating the Timer Programmatically

> ⚠️ **Important:** Always use `gr.HTML(...)` to update props, never return a new `PomodoroTimer()` instance.

```python
import gradio as gr
from pomodoro_forest import PomodoroTimer, _update_timer, TREE_THEMES, MODE_COLORS

with gr.Blocks() as demo:
    timer = PomodoroTimer()
    
    # Add 5 sessions programmatically (for testing)
    def add_sessions(timer_val):
        new_val = {
            **timer_val,
            "sessions": timer_val.get("sessions", 0) + 5,
            "total_minutes": timer_val.get("total_minutes", 0) + 125
        }
        return _update_timer(new_val, 25, "focus", "classic")
    
    btn = gr.Button("Add 5 Sessions (Demo)")
    btn.click(fn=add_sessions, inputs=[timer], outputs=[timer])

demo.launch()
```

---

## API Usage

When your Gradio app is running, you can interact with the PomodoroTimer via the API.

### Get Current State

```python
from gradio_client import Client

client = Client("http://localhost:7860")

# If your timer is an input to an API endpoint
result = client.predict(
    {"elapsed": 0, "running": False, "sessions": 3, "total_minutes": 75},
    api_name="/your_endpoint"
)
```

### Python Client Example

```python
from gradio_client import Client

client = Client("http://localhost:7860")

# Start a session with pre-existing data
timer_state = {
    "elapsed": 0,
    "running": False,
    "sessions": 5,
    "total_minutes": 125
}

# Call your function that takes timer as input
result = client.predict(timer_state, api_name="/process_timer")
print(result)
```

### REST API

```bash
curl -X POST http://localhost:7860/api/your_endpoint \
  -H "Content-Type: application/json" \
  -d '{
    "data": [{
      "elapsed": 0,
      "running": false,
      "sessions": 10,
      "total_minutes": 250
    }]
  }'
```

---

## MCP Usage

The PomodoroTimer component works with Gradio's MCP (Model Context Protocol) support, allowing AI assistants to interact with it.

### Exposing via MCP

```python
import gradio as gr
from pomodoro_forest import PomodoroTimer, _update_timer

with gr.Blocks() as demo:
    timer = PomodoroTimer()
    output = gr.JSON(label="Timer State")
    
    def get_timer_state(timer_val):
        """Get the current Pomodoro timer state.
        
        Returns the timer's current state including elapsed time,
        running status, completed sessions, and total focus minutes.
        """
        return timer_val
    
    def set_timer_sessions(timer_val, sessions: int, total_minutes: int):
        """Set the Pomodoro timer's session count.
        
        Args:
            sessions: Number of completed focus sessions
            total_minutes: Total minutes spent focusing
        """
        new_val = {**timer_val, "sessions": sessions, "total_minutes": total_minutes}
        return _update_timer(new_val, 25, "focus", "classic"), new_val
    
    # Expose as API endpoints for MCP
    get_btn = gr.Button("Get State")
    get_btn.click(
        fn=get_timer_state,
        inputs=[timer],
        outputs=[output],
        api_name="get_pomodoro_state"  # MCP-accessible endpoint
    )
    
    with gr.Row():
        sessions_input = gr.Number(label="Sessions", value=0)
        minutes_input = gr.Number(label="Total Minutes", value=0)
    
    set_btn = gr.Button("Set Sessions")
    set_btn.click(
        fn=set_timer_sessions,
        inputs=[timer, sessions_input, minutes_input],
        outputs=[timer, output],
        api_name="set_pomodoro_sessions"  # MCP-accessible endpoint
    )

demo.launch()
```

### MCP Tool Definitions

When used with MCP, the following tools become available:

**`get_pomodoro_state`**
```json
{
    "name": "get_pomodoro_state",
    "description": "Get the current Pomodoro timer state",
    "parameters": {
        "timer_val": {
            "type": "object",
            "properties": {
                "elapsed": {"type": "integer"},
                "running": {"type": "boolean"},
                "sessions": {"type": "integer"},
                "total_minutes": {"type": "integer"}
            }
        }
    }
}
```

**`set_pomodoro_sessions`**
```json
{
    "name": "set_pomodoro_sessions",
    "description": "Set the Pomodoro timer's session count",
    "parameters": {
        "sessions": {"type": "integer", "description": "Number of completed sessions"},
        "total_minutes": {"type": "integer", "description": "Total focus minutes"}
    }
}
```

### Using with Claude or Other MCP Clients

```python
# Example: AI assistant querying your Pomodoro app via MCP
# The assistant can call these tools to interact with the timer

# Get current state
state = mcp_client.call_tool("get_pomodoro_state", {})
# Returns: {"elapsed": 300, "running": true, "sessions": 3, "total_minutes": 75}

# Set sessions (e.g., restore from saved data)
mcp_client.call_tool("set_pomodoro_sessions", {
    "sessions": 10,
    "total_minutes": 250
})
```

---

## Customization

### Adding Custom Themes

```python
from pomodoro_forest import TREE_THEMES

# Add your own theme
TREE_THEMES["ocean"] = {
    "trunk": "#1565C0",
    "crown": "#4FC3F7", 
    "crown_top": "#81D4FA",
    "fruit": "#00BCD4"
}

# Use it
timer = PomodoroTimer(tree_theme="ocean")
```

### Override Individual Colors

```python
timer = PomodoroTimer(
    tree_theme="classic",
    crown_color="#9C27B0",  # Purple crown
    fruit_color="#FFEB3B",  # Yellow fruit
)
```

### Custom Mode Colors

```python
from pomodoro_forest import MODE_COLORS

MODE_COLORS["focus"] = "#9C27B0"       # Purple for focus
MODE_COLORS["short_break"] = "#00BCD4" # Cyan for short break
MODE_COLORS["long_break"] = "#FF9800"  # Orange for long break
```

---

## Best Practices

### 1. Always Use `gr.HTML()` for Updates

```python
# ✅ Correct
def update_timer(timer_val):
    return gr.HTML(value=new_val, duration=25, mode="focus", ...)

# ❌ Wrong - causes issues
def update_timer(timer_val):
    return PomodoroTimer(value=new_val, duration=25, mode="focus")
```

### 2. Use Helper Functions

Import and use the provided helper functions:

```python
from pomodoro_forest import _update_timer, _update_theme_only

# Full update
timer_output = _update_timer(value, duration, mode, theme)

# Theme-only update
timer_output = _update_theme_only(theme, mode)
```

### 3. Handle Events Safely

```python
def handle_event(evt: gr.EventData, timer_val):
    # Always check if _data exists
    try:
        data = evt._data.get("key", "default") if evt._data else "default"
    except:
        data = "default"
    return data
```

### 4. Preserve State Across Updates

```python
def my_handler(timer_val, new_duration):
    # Spread existing state, only change what's needed
    new_val = {**timer_val, "elapsed": 0}
    return _update_timer(new_val, new_duration, "focus", "classic")
```

---

## Component Architecture

```
PomodoroTimer (extends gr.HTML)
├── html_template    → Renders timer ring, tree scene, controls, stats
├── css_template     → Styles with ${prop} placeholders for dynamic colors
├── js_on_load       → Handles start/pause, reset, mode switching
└── value            → Dict holding timer state
```

**File Structure:**
```
pomodoro_forest.py
├── Constants (DURATIONS, MODE_COLORS, TREE_THEMES)
├── Templates (HTML_TEMPLATE, CSS_TEMPLATE, JS_ON_LOAD)
├── PomodoroTimer class
├── Helper functions (_update_timer, _update_theme_only)
└── Demo app (with gr.Blocks)
```

---

## Troubleshooting

| Issue | Cause | Solution |
|-------|-------|----------|
| Colors don't update | Using Python string formatting instead of `${prop}` | Use template syntax: `${mode_color}` |
| "Multiple values for argument" error | Returning subclass instance | Return `gr.HTML(...)` instead |
| Event data is None | Accessing wrong event or wrong data key | Check `evt._data` exists before accessing |
| Timer doesn't re-render | Mutating value in place | Always spread: `{...timer_val, key: newVal}` |

---

## License

MIT License - Feel free to use, modify, and distribute!

---

## Credits

Built with [Gradio 6](https://gradio.app) and the new `gr.HTML` custom component system.

Created as a demo for the Gradio HTML component capabilities.