Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 58 additions & 3 deletions runestone/activecode/js/activecode_brython.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export default class BrythonActiveCode extends ActiveCode {
super(opts);
opts.alignVertical = true;
this.python3_interpreter = $(orig).data("python3_interpreter");
$(this.runButton).text("Render");
this.editor.setValue(this.code);
}

Expand All @@ -24,11 +23,67 @@ export default class BrythonActiveCode extends ActiveCode {
prog = `
<html>
<head>
<script type='text/javascript' src='https://cdn.jsdelivr.net/npm/brython@3.9.0/brython.min.js'></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/brython@3.9.4/brython.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/brython@3.9.4/brython_stdlib.min.js"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.0.1/styles/default.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.0.1/highlight.min.js"></script>
<style>
pre {
position: absolute; bottom: 5px; font-size: 13px; width: 94%; padding: 9.5px; line-height: 1.42857143;
}
code{
border: 1px solid #ccc; border-radius: 4px;
}
</style>
</head>
<body onload='brython()'>
<script type='text/python'>${prog}
<pre id="consolePre">
<code id="consoleCode"></code>
</pre>
<script type=text/javascript>
if (typeof console != "undefined"){
if (typeof console.log != 'undefined'){
console.olog = console.log;
}else{
console.olog = function() {};
}
if (typeof console.log != 'undefined'){
console.oerr = console.error;
}else{
console.oerr = function() {};
}
}

var logger = document.getElementById('consoleCode');
var preElem = document.getElementById('consolePre');
console.log = function(message) {
console.olog(message);
preElem.style.visibility = "visible";
preElem.style.bottom = "5px";
logger.classList.add("plaintext");
hljs.highlightElement(logger);
if (typeof message == 'object') {
logger.innerHTML += (JSON && JSON.stringify ? JSON.stringify(message) : message) + '<br />';
} else {
logger.innerHTML += message + '</br>';
}
};

console.error = function(message) {
console.oerr(message);
preElem.style.visibility = "visible";
preElem.style.top = "5px";
logger.classList.add("python");
hljs.highlightElement(logger);
if (typeof message == 'object') {
logger.innerHTML += (JSON && JSON.stringify ? JSON.stringify(message) : message) + '<br />';
} else {
logger.innerHTML += message + '</br>';
}
};
console.info = console.debug = console.log
</script>
<script type='text/python'>${prog}</script>
</body>
</html>
`;
Expand Down
5 changes: 3 additions & 2 deletions runestone/activecode/test/_sources/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2830,13 +2830,14 @@ Trying Brython as Python 3 interpreter
--------------------------------------
.. activecode:: test_activecode_python3
:language: python3
:python3_interpreter: brython
:python3_interpreter: brython

print("You can see this print on the browser console")

from browser import document, alert, html

def hello(ev):
alert("Hello! I'm using Brython :D")
print("You can see this print on the browser console and on the console inside the iframe")

document <= html.BUTTON("My button", id="button_alert")
document["button_alert"].bind("click", hello)