CodePlayground/python.html

78 lines
2.2 KiB
HTML

<!DOCTYPE html>
<html lang="fr">
<head>
<title>Python Playground</title>
<meta charset="utf-8" />
<script src="lib/codeflask.min.js"></script>
<script src="lib/prism-python.min.js"></script>
<script src="lib/brython.min.js"></script>
<script src="lib/brython_stdlib.min.js"></script>
<link rel="stylesheet" href="assets/css/main.css" type="text/css" />
</head>
<body>
<header>
<h1>Python Playground</h1>
</header>
<div id="container">
<div id="codeContainer"><div id="code"></div></div><!--
--><div id="console">
</div>
<input type="button" onclick="example()" value="Exemple" id="exBtn" /><!--
--><input type="button" onclick="run()" value="Exécuter !" id="runBtn" /><!--
--><input type="button" onclick="clearCode()" value="Effacer" id="clearBtn" />
</div>
<footer>
Romain de Laage - CC0
</footer>
<script type="text/python" id="pyinit">
import sys
from browser import document
# Define new class to bind html element with std files
class Stdfile() :
def __init__(self, elmtId, error = False):
self.elmt = document[elmtId]
self.error = error
def write(self, text):
if self.error == True:
self.elmt.html = self.elmt.html + '<div class="error">' + text.replace('\n', '<br />') + '</div>'
else:
self.elmt.html = self.elmt.html + '<div class="message">' + text.replace('\n', '<br />') + '</div>'
# Bind stderr and stdout with console element of page
sys.stderr = Stdfile('console', True)
sys.stdout = Stdfile('console')
</script>
<script type="text/python" id="pyclear">
from browser import document
# Clear console
document['console'].html = ''
</script>
<script type="text/python" id="pycode"></script>
<script src="assets/js/editor.js"></script>
<script>
const flask = getEditor("#code", "py")
brython({ids:["pyinit"]})
function run() {
document.getElementById("pycode").innerHTML = flask.getCode()
brython({ids:["pyclear"]})
brython({
debug:1,
ids:["pycode"]
})
}
function example() {
flask.updateCode("def foo(n):\n for i in range(n):\n print('passe ' + str(i))\n return n*n\n\nprint(foo(5))")
}
function clearCode() {
flask.updateCode("")
}
</script>
</body>
</html>