k-l-lambda commited on
Commit
ba48acb
·
1 Parent(s): 764d3da

fixed DOM incompleted bug

Browse files
Files changed (1) hide show
  1. app.py +13 -15
app.py CHANGED
@@ -249,7 +249,7 @@ def run_generation (prompt, measures, temperature, max_patches, seed, store, top
249
  elapsed, n_patches, per_patch, (n_patches / elapsed if elapsed else 0.0))
250
 
251
  # finished: persist the document, refresh the file list, select the new entry
252
- label = OUTPUT_PREFIX + time.strftime('%H%M%S') + ('_m%d' % meas if meas else '') + '_s%d' % int(seed)
253
  store[label] = pretty
254
  os.makedirs(OUTPUT_DIR, exist_ok=True)
255
  out_path = os.path.join(OUTPUT_DIR, label.replace(OUTPUT_PREFIX, '') + '.lyl')
@@ -322,19 +322,15 @@ function () {
322
  }
323
  '''
324
 
325
- # Read the live editor text and render it to SVG. gr.Code renders a CodeMirror
326
- # editor; its current text lives in the `.cm-content` element (textContent).
 
 
 
327
  _JS_RENDER = '''
328
- function () {
329
- if (!window.LilyScore) return;
330
- const root = document.querySelector('#ls-editor');
331
- let text = '';
332
- if (root) {
333
- const cm = root.querySelector('.cm-content');
334
- if (cm) text = cm.innerText;
335
- else { const ta = root.querySelector('textarea'); if (ta) text = ta.value; }
336
- }
337
- window.LilyScore.render(text);
338
  }
339
  '''
340
 
@@ -452,8 +448,10 @@ def build_ui ():
452
  )
453
  gen_event.then(None, None, None, js=_JS_GEN_END)
454
 
455
- # every editor change (streaming syncs + manual edits + file loads) -> re-render SVG
456
- editor.change(None, None, None, js=_JS_RENDER)
 
 
457
 
458
  # Stop: cancel generation, then lift the gate so the player returns
459
  stop_btn.click(None, None, None, cancels=[gen_event]).then(None, None, None, js=_JS_GEN_END)
 
249
  elapsed, n_patches, per_patch, (n_patches / elapsed if elapsed else 0.0))
250
 
251
  # finished: persist the document, refresh the file list, select the new entry
252
+ label = OUTPUT_PREFIX + time.strftime('%Y%m%d_%H%M%S') + ('_m%d' % meas if meas else '') + '_s%d' % int(seed)
253
  store[label] = pretty
254
  os.makedirs(OUTPUT_DIR, exist_ok=True)
255
  out_path = os.path.join(OUTPUT_DIR, label.replace(OUTPUT_PREFIX, '') + '.lyl')
 
322
  }
323
  '''
324
 
325
+ # Render the editor text to SVG. The text is passed in by Gradio as the event's
326
+ # input value we must NOT scrape it from the DOM: gr.Code is a CodeMirror editor
327
+ # that virtualises long documents (only the rows near the viewport exist in the
328
+ # DOM), so `.cm-content`.innerText is truncated for long scores and the render
329
+ # comes out incomplete. Taking the value as an argument gives the full text.
330
  _JS_RENDER = '''
331
+ function (text) {
332
+ if (window.LilyScore) window.LilyScore.render(text || '');
333
+ return [];
 
 
 
 
 
 
 
334
  }
335
  '''
336
 
 
448
  )
449
  gen_event.then(None, None, None, js=_JS_GEN_END)
450
 
451
+ # every editor change (streaming syncs + manual edits + file loads) -> re-render
452
+ # SVG. Pass the editor value as input so the js receives the FULL text (gr.Code
453
+ # virtualises long docs in the DOM, so scraping it client-side truncates).
454
+ editor.change(None, inputs=[editor], outputs=None, js=_JS_RENDER)
455
 
456
  # Stop: cancel generation, then lift the gate so the player returns
457
  stop_btn.click(None, None, None, cancels=[gen_event]).then(None, None, None, js=_JS_GEN_END)