Spaces:
Sleeping
Sleeping
Deploy Myco from CI
Browse files- ui/interface.py +40 -45
ui/interface.py
CHANGED
|
@@ -1,14 +1,7 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Myco UI Components โ Modular Gradio layout and custom HTML/Canvas animations.
|
| 3 |
-
This file decouples the messy HTML/CSS/JS injection from your main application script.
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import gradio as gr
|
| 7 |
-
|
| 8 |
def myco_animation_card_markdown() -> str:
|
| 9 |
"""
|
| 10 |
Generates a self-contained, micro-scoped HTML/Canvas card component.
|
| 11 |
-
|
| 12 |
"""
|
| 13 |
return """
|
| 14 |
<div id="myco-card-container" class="myco-anim-card">
|
|
@@ -90,18 +83,10 @@ def myco_animation_card_markdown() -> str:
|
|
| 90 |
|
| 91 |
<script>
|
| 92 |
(function() {
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
const ctx = canvas.getContext('2d');
|
| 96 |
-
const overlay = document.getElementById('myco-card-overlay');
|
| 97 |
-
const msg = document.getElementById('myco-card-msg');
|
| 98 |
-
|
| 99 |
let audioCtx = null;
|
| 100 |
-
|
| 101 |
-
if (!audioCtx) audioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
| 102 |
-
if (audioCtx.state === 'suspended') audioCtx.resume();
|
| 103 |
-
}
|
| 104 |
-
|
| 105 |
const textures = ['m-grid', 'm-lines', 'm-noise'];
|
| 106 |
const sequence = ["๐", "MYCO", "๐โ๐ซ", "SPORE", "GILLS", "๐", "CAP", "HYPHAE", "ROOTS"];
|
| 107 |
|
|
@@ -138,8 +123,13 @@ def myco_animation_card_markdown() -> str:
|
|
| 138 |
}
|
| 139 |
}
|
| 140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
function resizeCard() {
|
| 142 |
-
if (!container.clientWidth) return false;
|
| 143 |
dpr = window.devicePixelRatio || 1;
|
| 144 |
w = container.clientWidth;
|
| 145 |
h = container.clientHeight;
|
|
@@ -215,7 +205,7 @@ def myco_animation_card_markdown() -> str:
|
|
| 215 |
|
| 216 |
function startMutationLoop() {
|
| 217 |
if (engineStarted) return;
|
| 218 |
-
if (!resizeCard()) return;
|
| 219 |
|
| 220 |
engineStarted = true;
|
| 221 |
morph(sequence[currentStep]);
|
|
@@ -235,12 +225,12 @@ def myco_animation_card_markdown() -> str:
|
|
| 235 |
}
|
| 236 |
|
| 237 |
function loop() {
|
| 238 |
-
//
|
| 239 |
-
if (!engineStarted && container.clientWidth > 0) {
|
| 240 |
startMutationLoop();
|
| 241 |
}
|
| 242 |
|
| 243 |
-
if (w > 0 && h > 0) {
|
| 244 |
ctx.fillStyle = 'rgba(5, 10, 8, 0.2)';
|
| 245 |
ctx.fillRect(0, 0, w, h);
|
| 246 |
particles.forEach(p => { p.update(); p.draw(); });
|
|
@@ -248,29 +238,34 @@ def myco_animation_card_markdown() -> str:
|
|
| 248 |
requestAnimationFrame(loop);
|
| 249 |
}
|
| 250 |
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
|
| 255 |
-
|
| 256 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
})();
|
| 258 |
</script>
|
| 259 |
</div>
|
| 260 |
"""
|
| 261 |
-
|
| 262 |
-
def render_shroom_tab():
|
| 263 |
-
"""
|
| 264 |
-
Houses layout rendering parameters within a dedicated component ecosystem.
|
| 265 |
-
Splits the interface into two sub-tabs for clean organization.
|
| 266 |
-
"""
|
| 267 |
-
with gr.Tabs():
|
| 268 |
-
# First Sub-Tab: The Animation Card
|
| 269 |
-
with gr.Tab("๐ฎ Mutation Matrix"):
|
| 270 |
-
gr.HTML(value=myco_animation_card_markdown())
|
| 271 |
-
|
| 272 |
-
# Second Sub-Tab: The Environment Controls
|
| 273 |
-
with gr.Tab("๐๏ธ Lab Controls"):
|
| 274 |
-
with gr.Group():
|
| 275 |
-
gr.Markdown("### ๐ฌ Laboratory Environment Controls")
|
| 276 |
-
gr.Markdown("Interact with the mutation matrix card in the previous tab to sync audio contexts.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def myco_animation_card_markdown() -> str:
|
| 2 |
"""
|
| 3 |
Generates a self-contained, micro-scoped HTML/Canvas card component.
|
| 4 |
+
Built with a self-healing bootloader to completely eliminate DOM timing crashes.
|
| 5 |
"""
|
| 6 |
return """
|
| 7 |
<div id="myco-card-container" class="myco-anim-card">
|
|
|
|
| 83 |
|
| 84 |
<script>
|
| 85 |
(function() {
|
| 86 |
+
// Global engine hooks initialized safely after DOM validation
|
| 87 |
+
let container, canvas, ctx, overlay, msg, dateEl;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
let audioCtx = null;
|
| 89 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
const textures = ['m-grid', 'm-lines', 'm-noise'];
|
| 91 |
const sequence = ["๐", "MYCO", "๐โ๐ซ", "SPORE", "GILLS", "๐", "CAP", "HYPHAE", "ROOTS"];
|
| 92 |
|
|
|
|
| 123 |
}
|
| 124 |
}
|
| 125 |
|
| 126 |
+
function initAudio() {
|
| 127 |
+
if (!audioCtx) audioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
| 128 |
+
if (audioCtx.state === 'suspended') audioCtx.resume();
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
function resizeCard() {
|
| 132 |
+
if (!container || !container.clientWidth) return false;
|
| 133 |
dpr = window.devicePixelRatio || 1;
|
| 134 |
w = container.clientWidth;
|
| 135 |
h = container.clientHeight;
|
|
|
|
| 205 |
|
| 206 |
function startMutationLoop() {
|
| 207 |
if (engineStarted) return;
|
| 208 |
+
if (!resizeCard()) return;
|
| 209 |
|
| 210 |
engineStarted = true;
|
| 211 |
morph(sequence[currentStep]);
|
|
|
|
| 225 |
}
|
| 226 |
|
| 227 |
function loop() {
|
| 228 |
+
// Wait until the container has actual visibility layout parameters before executing canvas clears
|
| 229 |
+
if (!engineStarted && container && container.clientWidth > 0) {
|
| 230 |
startMutationLoop();
|
| 231 |
}
|
| 232 |
|
| 233 |
+
if (w > 0 && h > 0 && ctx) {
|
| 234 |
ctx.fillStyle = 'rgba(5, 10, 8, 0.2)';
|
| 235 |
ctx.fillRect(0, 0, w, h);
|
| 236 |
particles.forEach(p => { p.update(); p.draw(); });
|
|
|
|
| 238 |
requestAnimationFrame(loop);
|
| 239 |
}
|
| 240 |
|
| 241 |
+
// --- CRASH RESISTANT BOOTLOADER ---
|
| 242 |
+
function bootstrapEngine() {
|
| 243 |
+
container = document.getElementById('myco-card-container');
|
| 244 |
+
canvas = document.getElementById('myco-card-canvas');
|
| 245 |
+
overlay = document.getElementById('myco-card-overlay');
|
| 246 |
+
msg = document.getElementById('myco-card-msg');
|
| 247 |
+
dateEl = document.getElementById('myco-card-date');
|
| 248 |
|
| 249 |
+
// If Gradio hasn't dropped the HTML elements into the DOM yet, wait 100ms and retry safely
|
| 250 |
+
if (!container || !canvas || !overlay || !msg || !dateEl) {
|
| 251 |
+
setTimeout(bootstrapEngine, 100);
|
| 252 |
+
return;
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
// Elements are guaranteed to exist now. Bind parameters.
|
| 256 |
+
ctx = canvas.getContext('2d');
|
| 257 |
+
container.addEventListener('mousedown', initAudio);
|
| 258 |
+
window.addEventListener('resize', () => { resizeCard(); morph(sequence[currentStep]); });
|
| 259 |
+
dateEl.textContent = new Date().toLocaleDateString();
|
| 260 |
+
|
| 261 |
+
// Run structural frame layout loop
|
| 262 |
+
loop();
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
+
// Safely start monitoring the DOM insertion pipeline
|
| 266 |
+
bootstrapEngine();
|
| 267 |
})();
|
| 268 |
</script>
|
| 269 |
</div>
|
| 270 |
"""
|
| 271 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|