commit 284083ed6ef4c47377aa16a6ec3220d36cdf1816
parent 6ae6896601417291e696ad91c682c176d98610c6
Author: Léo Villeveygoux <l@vgx.fr>
Date: Fri, 18 Dec 2020 02:32:28 +0100
Première version : edition, execution, stderr
- On rend les éléments de la classe breditor éditables avec Ace
- On les rend exécutables avec Brython
- On affiche les erreurs dans la page, en rouge
Diffstat:
3 files changed, 62 insertions(+), 0 deletions(-)
diff --git a/lib/breditor.py b/lib/breditor.py
@@ -0,0 +1,31 @@
+# Requires Brython and Ace editor
+
+from browser import document, window, html
+from sys import stderr
+
+def exec_code(editor, id):
+ stderr_frame = document[id + "_stderr"]
+ stderr_frame.clear()
+ stderr.write = lambda data : stderr_target(data, stderr_frame)
+ exec(editor.getValue())
+
+def stderr_target(data, elt):
+ elt <= data
+
+breditors = document.select(".breditor")
+
+for ed_elt in breditors:
+ editor = window.ace.edit(ed_elt.id)
+ editor.session.setMode("ace/mode/python")
+ #editor.setOption('fontSize', '14px')
+ editor.setOption('maxLines', 5)
+
+ stderr_frame = html.PRE(Class="stderr")
+ stderr_frame.id = ed_elt.id + "_stderr"
+
+ ed_elt.insertAdjacentElement('afterend', stderr_frame)
+
+ exec_button = html.BUTTON("Exécuter →")
+ exec_button.bind("click", lambda ev : exec_code(editor, ed_elt.id))
+ ed_elt.insertAdjacentElement('afterend', exec_button)
+
diff --git a/styles/breditor.css b/styles/breditor.css
@@ -0,0 +1,4 @@
+.stderr {
+ background-color: #fee;
+ color: #900
+}
diff --git a/test.html b/test.html
@@ -0,0 +1,27 @@
+<!doctype html>
+<html>
+<head>
+<meta charset="UTF-8">
+<link rel="stylesheet" type="text/css" href="styles/breditor.css" />
+<script src="lib/brython.js"></script>
+<script src="lib/brython_stdlib.js"></script>
+<script src="lib/ace/ace.js"></script>
+<script src="lib/ace/mode-python.js"></script>
+<script type="text/python" src="lib/breditor.py"></script>
+</head>
+
+<body lang='fr' onload="brython();">
+
+<h1> Test d'éditeur python </h1>
+
+<p>
+Ça devrait être ici :
+</p>
+
+<pre id="ed1" class="breditor">
+<code>from browser import alert
+alert("Hello !")
+</code></pre>
+
+</body>
+</html>