breditor

Dynamic Python edition/execution in the browser
git clone git://git.vgx.fr/breditor
Log | Files | Refs

breditor.py (1496B)


      1 # Requires Brython and Ace editor
      2 
      3 from browser import document, window, html
      4 from sys import stderr, settrace
      5 
      6 def make_max_line_tracer(maxlines):
      7     lines = 0
      8     def tracer(frame, event, arg):
      9         nonlocal lines
     10         if event == 'line':
     11             lines += 1
     12             if lines >= maxlines:
     13                 raise TimeoutError
     14         return tracer
     15     return tracer
     16 
     17 def exec_code(editor, id):
     18     stderr_frame = document[id + "_stderr"]
     19     stderr_frame.clear()
     20     stderr.write = lambda data : stderr_target(data, stderr_frame)
     21 
     22     code = editor.getValue()
     23     compiled = compile(code, "<" + id + ">", "exec")
     24 
     25     settrace(make_max_line_tracer(10000)) # increase to allow longer execution
     26     try:
     27         exec(compiled)
     28     except TimeoutError:
     29         settrace(None)
     30         print("L'exécution prend trop de temps, abandon.", file=stderr)
     31     finally:
     32 	settrace(None)
     33 
     34 def stderr_target(data, elt):
     35     elt <= data
     36 
     37 breditors = document.select(".breditor")
     38 
     39 for ed_elt in breditors:
     40     editor = window.ace.edit(ed_elt.id)
     41     editor.session.setMode("ace/mode/python")
     42     #editor.setOption('fontSize', '14px')
     43     editor.setOption('maxLines', 5)
     44 
     45     stderr_frame = html.PRE(Class="stderr")
     46     stderr_frame.id = ed_elt.id + "_stderr"
     47 
     48     ed_elt.insertAdjacentElement('afterend', stderr_frame)
     49 
     50     exec_button = html.BUTTON("Exécuter →")
     51     exec_button.bind("click", lambda ev : exec_code(editor, ed_elt.id))
     52     ed_elt.insertAdjacentElement('afterend', exec_button)
     53