-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.py
More file actions
executable file
·754 lines (640 loc) · 24.2 KB
/
player.py
File metadata and controls
executable file
·754 lines (640 loc) · 24.2 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
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
#!/usr/bin/env python3
"""
Generate an interactive timeline player from clinical trial JSON data.
Creates an HTML page with video-player-like controls to play through events chronologically.
"""
import json
import sys
from pathlib import Path
def load_json_data(filepath):
"""Load JSON data from file."""
with open(filepath, 'r') as f:
return json.load(f)
def generate_html(data, output_path='timeline_player.html'):
"""
Generate HTML with interactive timeline player and video-like controls.
Args:
data: Dictionary containing 'nodes' list
output_path: Output HTML file path
"""
# Convert Python dict to JSON string for embedding in JavaScript
json_data = json.dumps(data, indent=2)
html_template = f"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Clinical Trial Timeline Player</title>
<!-- Bootstrap 5 CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- Bootstrap Icons -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
<style>
body {{
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px 0;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}}
.main-container {{
max-width: 1400px;
margin: 0 auto;
padding: 20px;
}}
.page-header {{
background: rgba(255, 255, 255, 0.95);
border-radius: 15px;
padding: 20px;
margin-bottom: 15px;
box-shadow: 0 10px 40px rgba(0,0,0,0.2);
}}
.page-header h1 {{
color: #2d3748;
font-weight: 700;
margin-bottom: 5px;
font-size: 28px;
}}
.page-header p {{
color: #718096;
margin-bottom: 0;
font-size: 14px;
}}
.player-container {{
background: rgba(255, 255, 255, 0.95);
border-radius: 15px;
padding: 20px;
box-shadow: 0 10px 40px rgba(0,0,0,0.2);
}}
.visualization-area {{
background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
border-radius: 12px;
padding: 20px;
margin-bottom: 15px;
min-height: 400px;
position: relative;
overflow: hidden;
}}
.timeline-display {{
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-start;
min-height: 360px;
}}
.current-event {{
text-align: center;
animation: fadeIn 0.5s ease-in;
}}
@keyframes fadeIn {{
from {{ opacity: 0; transform: translateY(20px); }}
to {{ opacity: 1; transform: translateY(0); }}
}}
.event-number {{
font-size: 12px;
color: #718096;
font-weight: 600;
margin-bottom: 5px;
}}
.event-id {{
font-size: 14px;
color: #4a5568;
font-family: 'Courier New', monospace;
margin-bottom: 8px;
}}
.event-label {{
font-size: 26px;
font-weight: 700;
color: #2d3748;
margin-bottom: 8px;
}}
.event-time {{
font-size: 16px;
color: #667eea;
font-weight: 600;
margin-bottom: 5px;
}}
.event-tick {{
font-size: 12px;
color: #a0aec0;
font-family: 'Courier New', monospace;
margin-bottom: 10px;
}}
.event-encounter {{
display: inline-block;
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
color: white;
padding: 6px 18px;
border-radius: 20px;
font-size: 14px;
font-weight: 600;
margin-bottom: 10px;
box-shadow: 0 4px 15px rgba(245, 87, 108, 0.3);
}}
.event-activities {{
max-width: 800px;
margin: 10px auto 0;
text-align: left;
max-height: 180px;
overflow-y: auto;
}}
.activities-title {{
font-size: 14px;
font-weight: 600;
color: #2d3748;
margin-bottom: 8px;
text-align: center;
}}
.activity-group {{
margin-bottom: 8px;
}}
.activity-parent {{
font-size: 12px;
font-weight: 600;
color: #667eea;
margin-bottom: 4px;
}}
.activity-item {{
background: white;
padding: 6px 10px;
border-radius: 6px;
margin-bottom: 4px;
border-left: 3px solid #667eea;
font-size: 12px;
color: #4a5568;
}}
.procedure-item {{
font-size: 11px;
color: #718096;
margin-top: 3px;
padding-left: 10px;
}}
.controls-panel {{
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 12px;
padding: 15px;
color: white;
}}
.progress-section {{
margin-bottom: 12px;
}}
.progress-info {{
display: flex;
justify-content: space-between;
margin-bottom: 6px;
font-size: 11px;
opacity: 0.9;
}}
.timeline-slider {{
width: 100%;
height: 8px;
border-radius: 4px;
background: rgba(255, 255, 255, 0.3);
outline: none;
-webkit-appearance: none;
appearance: none;
cursor: pointer;
}}
.timeline-slider::-webkit-slider-thumb {{
-webkit-appearance: none;
appearance: none;
width: 20px;
height: 20px;
border-radius: 50%;
background: white;
cursor: pointer;
box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}}
.timeline-slider::-moz-range-thumb {{
width: 20px;
height: 20px;
border-radius: 50%;
background: white;
cursor: pointer;
border: none;
box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}}
.playback-controls {{
display: flex;
justify-content: center;
align-items: center;
gap: 10px;
margin-bottom: 12px;
}}
.control-btn {{
background: rgba(255, 255, 255, 0.2);
border: 2px solid rgba(255, 255, 255, 0.5);
color: white;
width: 40px;
height: 40px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: all 0.3s ease;
font-size: 16px;
}}
.control-btn:hover {{
background: rgba(255, 255, 255, 0.3);
transform: scale(1.1);
}}
.control-btn:active {{
transform: scale(0.95);
}}
.control-btn.primary {{
width: 48px;
height: 48px;
background: white;
color: #667eea;
font-size: 20px;
}}
.control-btn.primary:hover {{
background: #f7fafc;
}}
.speed-controls {{
display: flex;
justify-content: center;
align-items: center;
gap: 8px;
}}
.speed-label {{
font-size: 11px;
opacity: 0.9;
min-width: 45px;
}}
.speed-btn {{
background: rgba(255, 255, 255, 0.2);
border: 1px solid rgba(255, 255, 255, 0.3);
color: white;
padding: 4px 10px;
border-radius: 15px;
cursor: pointer;
transition: all 0.2s ease;
font-size: 11px;
font-weight: 600;
}}
.speed-btn:hover {{
background: rgba(255, 255, 255, 0.3);
}}
.speed-btn.active {{
background: white;
color: #667eea;
}}
.no-events {{
text-align: center;
color: #a0aec0;
padding: 60px 20px;
font-size: 18px;
}}
.timeline-markers {{
display: flex;
justify-content: space-between;
margin-top: 3px;
font-size: 10px;
opacity: 0.7;
}}
</style>
</head>
<body>
<div class="main-container">
<!-- Page Header -->
<div class="page-header">
<h1><i class="bi bi-play-circle-fill"></i> Clinical Trial Timeline Player</h1>
<p>Interactive visualization with video-player controls</p>
</div>
<!-- Player Container -->
<div class="player-container">
<!-- Visualization Area -->
<div class="visualization-area">
<div class="timeline-display" id="timeline-display">
<div class="no-events">Load timeline to begin</div>
</div>
</div>
<!-- Controls Panel -->
<div class="controls-panel">
<!-- Progress Section -->
<div class="progress-section">
<div class="progress-info">
<span id="current-time">Event 0 of 0</span>
<span id="duration-time">Duration: --</span>
</div>
<input type="range" class="timeline-slider" id="timeline-slider"
min="0" max="100" value="0" step="1">
<div class="timeline-markers">
<span>Start</span>
<span id="middle-marker">--</span>
<span>End</span>
</div>
</div>
<!-- Playback Controls -->
<div class="playback-controls">
<button class="control-btn" id="first-btn" title="First Event">
<i class="bi bi-skip-start-fill"></i>
</button>
<button class="control-btn" id="prev-btn" title="Previous Event">
<i class="bi bi-chevron-left"></i>
</button>
<button class="control-btn primary" id="play-btn" title="Play/Pause">
<i class="bi bi-play-fill"></i>
</button>
<button class="control-btn" id="next-btn" title="Next Event">
<i class="bi bi-chevron-right"></i>
</button>
<button class="control-btn" id="last-btn" title="Last Event">
<i class="bi bi-skip-end-fill"></i>
</button>
</div>
<!-- Speed Controls -->
<div class="speed-controls">
<span class="speed-label">Speed:</span>
<button class="speed-btn" data-speed="0.5">0.5x</button>
<button class="speed-btn active" data-speed="1">1x</button>
<button class="speed-btn" data-speed="2">2x</button>
<button class="speed-btn" data-speed="5">5x</button>
<button class="speed-btn" data-speed="10">10x</button>
</div>
</div>
</div>
</div>
<!-- Bootstrap 5 JS Bundle -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script>
// Data from Python
const data = {json_data};
// Player state
let nodes = [];
let currentIndex = 0;
let isPlaying = false;
let playbackSpeed = 1;
let playbackInterval = null;
// Initialize player
function initPlayer() {{
nodes = data.nodes || [];
if (nodes.length === 0) {{
document.getElementById('timeline-display').innerHTML =
'<div class="no-events">No events found in data</div>';
return;
}}
// Setup slider
const slider = document.getElementById('timeline-slider');
slider.max = nodes.length - 1;
slider.value = 0;
// Update middle marker
const middleNode = nodes[Math.floor(nodes.length / 2)];
document.getElementById('middle-marker').textContent =
middleNode ? middleNode.time || '--' : '--';
// Display first event
displayEvent(0);
// Setup event listeners
setupEventListeners();
console.log(`Player initialized with ${{nodes.length}} events`);
}}
// Display event at given index
function displayEvent(index) {{
if (index < 0 || index >= nodes.length) return;
currentIndex = index;
const node = nodes[index];
// Build HTML for event display
let html = '<div class="current-event">';
// Event label
html += `<div class="event-label">${{node.label || 'Unnamed Event'}}</div>`;
// Time
html += `<div class="event-time">${{node.time || 'Time not specified'}}</div>`;
// Encounter
if (node.encounter) {{
html += `<div class="event-encounter"><i class="bi bi-calendar-event"></i> ${{node.encounter}}</div>`;
}}
// Activities
const activities = node.activities?.items || [];
if (activities.length > 0) {{
html += '<div class="event-activities">';
html += `<div class="activities-title"><i class="bi bi-list-check"></i> Activities (${{activities.length}})</div>`;
// Group activities by parent
const grouped = {{}};
activities.forEach(activity => {{
const parent = activity.parent || 'Other';
if (!grouped[parent]) grouped[parent] = [];
grouped[parent].push(activity);
}});
// Display grouped activities
Object.keys(grouped).sort().forEach(parent => {{
html += '<div class="activity-group">';
if (parent !== 'Other') {{
html += `<div class="activity-parent"><i class="bi bi-folder2-open"></i> ${{parent}}</div>`;
}}
grouped[parent].forEach(activity => {{
html += `<div class="activity-item">${{activity.label || 'Unnamed activity'}}`;
// Procedures
const procedures = activity.procedures?.filter(p => p && p.trim() !== '') || [];
if (procedures.length > 0) {{
procedures.forEach(proc => {{
html += `<div class="procedure-item"><i class="bi bi-arrow-return-right"></i> ${{proc}}</div>`;
}});
}}
html += '</div>';
}});
html += '</div>';
}});
html += '</div>';
}}
html += '</div>';
// Update display
document.getElementById('timeline-display').innerHTML = html;
// Update slider
document.getElementById('timeline-slider').value = index;
// Update progress info
document.getElementById('current-time').textContent = `Event ${{index + 1}} of ${{nodes.length}}`;
// Calculate duration info
if (nodes.length > 0) {{
const firstTick = nodes[0].tick;
const lastTick = nodes[nodes.length - 1].tick;
const totalSeconds = lastTick - firstTick;
const days = Math.floor(Math.abs(totalSeconds) / 86400);
const weeks = Math.floor(days / 7);
document.getElementById('duration-time').textContent =
`Duration: ${{weeks}} weeks (${{days}} days)`;
}}
}}
// Play/Pause toggle
function togglePlay() {{
if (isPlaying) {{
pause();
}} else {{
play();
}}
}}
// Play
function play() {{
if (currentIndex >= nodes.length - 1) {{
currentIndex = 0;
}}
isPlaying = true;
updatePlayButton();
const intervalTime = 1000 / playbackSpeed; // Base speed: 1 event per second
playbackInterval = setInterval(() => {{
if (currentIndex < nodes.length - 1) {{
displayEvent(currentIndex + 1);
}} else {{
pause();
}}
}}, intervalTime);
}}
// Pause
function pause() {{
isPlaying = false;
updatePlayButton();
if (playbackInterval) {{
clearInterval(playbackInterval);
playbackInterval = null;
}}
}}
// Update play button icon
function updatePlayButton() {{
const playBtn = document.getElementById('play-btn');
const icon = playBtn.querySelector('i');
if (isPlaying) {{
icon.className = 'bi bi-pause-fill';
playBtn.title = 'Pause';
}} else {{
icon.className = 'bi bi-play-fill';
playBtn.title = 'Play';
}}
}}
// Navigate to specific event
function goToEvent(index) {{
if (isPlaying) {{
pause();
displayEvent(index);
play();
}} else {{
displayEvent(index);
}}
}}
// Set playback speed
function setSpeed(speed) {{
playbackSpeed = speed;
// Update active button
document.querySelectorAll('.speed-btn').forEach(btn => {{
btn.classList.remove('active');
if (parseFloat(btn.dataset.speed) === speed) {{
btn.classList.add('active');
}}
}});
// Restart playback if playing
if (isPlaying) {{
pause();
play();
}}
}}
// Setup event listeners
function setupEventListeners() {{
// Play/Pause button
document.getElementById('play-btn').addEventListener('click', togglePlay);
// Previous button
document.getElementById('prev-btn').addEventListener('click', () => {{
if (currentIndex > 0) {{
goToEvent(currentIndex - 1);
}}
}});
// Next button
document.getElementById('next-btn').addEventListener('click', () => {{
if (currentIndex < nodes.length - 1) {{
goToEvent(currentIndex + 1);
}}
}});
// First button
document.getElementById('first-btn').addEventListener('click', () => {{
goToEvent(0);
}});
// Last button
document.getElementById('last-btn').addEventListener('click', () => {{
goToEvent(nodes.length - 1);
}});
// Slider
const slider = document.getElementById('timeline-slider');
slider.addEventListener('input', (e) => {{
const index = parseInt(e.target.value);
goToEvent(index);
}});
// Speed buttons
document.querySelectorAll('.speed-btn').forEach(btn => {{
btn.addEventListener('click', () => {{
const speed = parseFloat(btn.dataset.speed);
setSpeed(speed);
}});
}});
// Keyboard shortcuts
document.addEventListener('keydown', (e) => {{
switch(e.key) {{
case ' ':
case 'k':
e.preventDefault();
togglePlay();
break;
case 'ArrowLeft':
case 'j':
e.preventDefault();
if (currentIndex > 0) goToEvent(currentIndex - 1);
break;
case 'ArrowRight':
case 'l':
e.preventDefault();
if (currentIndex < nodes.length - 1) goToEvent(currentIndex + 1);
break;
case 'Home':
e.preventDefault();
goToEvent(0);
break;
case 'End':
e.preventDefault();
goToEvent(nodes.length - 1);
break;
}}
}});
}}
// Initialize on load
initPlayer();
</script>
</body>
</html>"""
# Write HTML to file
with open(output_path, 'w', encoding='utf-8') as f:
f.write(html_template)
print(f"Generated timeline player: {output_path}")
return output_path
def main():
"""Main function to generate timeline player from command line."""
if len(sys.argv) < 2:
print("Usage: python player.py <json_file> [output_file]")
print("\nExample:")
print(" python player.py expander.json")
print(" python player.py data.json player_output.html")
print("\nFeatures:")
print(" - Play/pause through timeline events")
print(" - Step forward/backward through events")
print(" - Adjustable playback speed (0.5x to 10x)")
print(" - Timeline slider for scrubbing")
print(" - Keyboard shortcuts (Space/K: play/pause, Arrow keys: navigate)")
sys.exit(1)
input_file = sys.argv[1]
output_file = sys.argv[2] if len(sys.argv) > 2 else 'timeline_player.html'
if not Path(input_file).exists():
print(f"Error: File not found: {input_file}")
sys.exit(1)
try:
data = load_json_data(input_file)
generate_html(data, output_file)
print(f"\nTimeline player generated successfully!")
print(f"Open {output_file} in a web browser to view the interactive player.")
print("\nControls:")
print(" - Click Play button or press Space/K to play/pause")
print(" - Use arrow buttons or Left/Right arrows to navigate")
print(" - Drag timeline slider to jump to any event")
print(" - Click speed buttons to adjust playback speed")
except Exception as e:
print(f"Error: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()