Skip to content

Commit 4a9ef5b

Browse files
Merge pull request #3 from its-ChaTTy/pyscriptinteg
PyScript Integration
2 parents 3155bf7 + 640defc commit 4a9ef5b

34 files changed

+3893
-1501
lines changed

myenv/lib64

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lib

myenv/pyvenv.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
home = /usr/bin
2+
include-system-site-packages = false
3+
version = 3.10.12

package-lock.json

Lines changed: 3533 additions & 1436 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

runestone/activecode/activecode.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ class ActiveCode(RunestoneIdDirective):
163163
:dburl: url to load database for sql mode
164164
:showlastsql: -- Only show the last sql result in output
165165
:python3_interpreter: brython (uses brython as interpreter of python3)
166+
:python3_interpreter: pyscript (uses pyscript as interpreter of python3)
166167
:output_height: 200px
167168
168169
If this is a homework problem instead of an example in the text

runestone/activecode/js/acfactory.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import JSActiveCode from "./activecode_js.js";
33
import HTMLActiveCode from "./activecode_html.js";
44
import SQLActiveCode from "./activecode_sql.js";
55
import BrythonActiveCode from "./activecode_brython.js";
6+
import PyScriptActiveCode from "./activecode_pyscript.js";
67
import LiveCode from "./livecode.js";
78
import {
89
TimedActiveCode,
@@ -11,6 +12,7 @@ import {
1112
TimedHTMLActiveCode,
1213
TimedSQLActiveCode,
1314
TimedBrythonActiveCode,
15+
TimedPyScriptActiveCode,
1416
} from "./timed_activecode";
1517
import "../../common/js/jquery.highlight.js";
1618

@@ -39,6 +41,9 @@ export default class ACFactory {
3941
if(python3_interpreter==="brython"){
4042
return new TimedBrythonActiveCode(opts);
4143
}
44+
if(python3_interpreter==="pyscript"){
45+
return new TimedPyScriptActiveCode(opts);
46+
}
4247
if (lang === "python") {
4348
return new TimedActiveCode(opts);
4449
} else if (
@@ -58,7 +63,10 @@ export default class ACFactory {
5863
return new TimedActiveCode(opts);
5964
}
6065
} else {
61-
if ((lang ==="python3") && (python3_interpreter === "brython")){
66+
if((lang ==="python3") && (python3_interpreter === "pyscript")){
67+
return new PyScriptActiveCode(opts);
68+
}
69+
else if ((lang ==="python3") && (python3_interpreter === "brython")){
6270
return new BrythonActiveCode(opts);
6371
}
6472
else if (lang === "javascript") {
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import { ActiveCode } from "./activecode.js";
2+
3+
export default class PyScriptActiveCode extends ActiveCode {
4+
constructor(opts) {
5+
super(opts);
6+
opts.alignVertical = true;
7+
this.python3_interpreter = $(orig).data("python3_interpreter");
8+
$(this.runButton).text("Render");
9+
this.editor.setValue(this.code);
10+
}
11+
12+
async runProg() {
13+
var prog = await this.buildProg(true);
14+
let saveCode = "True";
15+
this.saveCode = await this.manage_scrubber(saveCode);
16+
$(this.output).text("");
17+
if (!this.alignVertical) {
18+
$(this.codeDiv).switchClass("col-md-12", "col-md-6", {
19+
duration: 500,
20+
queue: false,
21+
});
22+
}
23+
$(this.outDiv).show({ duration: 700, queue: false });
24+
prog = `
25+
<html>
26+
<head>
27+
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
28+
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
29+
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.0.1/styles/default.min.css">
30+
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.0.1/highlight.min.js"></script>
31+
<style>
32+
pre {
33+
position: absolute; font-size: 13px; width: 94%; padding: 9.5px; line-height: 1.42857143; border: 1px ; border-radius: 4px;
34+
}
35+
code{
36+
border: 1px solid #ccc; border-radius: 4px;
37+
}
38+
</style>
39+
</head>
40+
<body>
41+
<py-config>
42+
terminal = false
43+
packages = [ "pandas", "numpy", "matplotlib"]
44+
</py-config>
45+
<pre id="consolePre">
46+
<code id="consoleCode"></code>
47+
</pre>
48+
<py-script>
49+
import sys
50+
from js import document
51+
logger = document.getElementById('consoleCode')
52+
preElem = document.getElementById('consolePre')
53+
54+
class NewOut:
55+
def write(self, data):
56+
logger.innerHTML += str(data)
57+
sys.stderr = sys.stdout = NewOut()
58+
59+
def my_exec(code):
60+
try:
61+
exec(code)
62+
preElem.style.visibility = "visible"
63+
preElem.style.bottom = "5px"
64+
logger.classList.add("plaintext")
65+
except Exception as err:
66+
error_class = err.__class__.__name__
67+
detail = err.args[0]
68+
line_number = "" # PyScript does not currently expose line numbers
69+
result = f"'{error_class}': {detail} {line_number}"
70+
print(result)
71+
# Styling the pre element for error
72+
preElem.style.visibility = "visible"
73+
preElem.style.top = "5px"
74+
preElem.style.backgroundColor = "#f2dede"
75+
preElem.style.border = "1px solid #ebccd1"
76+
logger.classList.add("python")
77+
78+
# usage
79+
my_exec("""${prog}
80+
""")
81+
</py-script>
82+
<script>
83+
hljs.highlightAll();
84+
</script>
85+
</body>
86+
</html>
87+
`;
88+
this.output.srcdoc = prog;
89+
}
90+
91+
createOutput() {
92+
this.alignVertical = true;
93+
var outDiv = document.createElement("div");
94+
$(outDiv).addClass("ac_output");
95+
if (this.alignVertical) {
96+
$(outDiv).addClass("col-md-12");
97+
} else {
98+
$(outDiv).addClass("col-md-5");
99+
}
100+
this.outDiv = outDiv;
101+
this.output = document.createElement("iframe");
102+
$(this.output).css("background-color", "white");
103+
$(this.output).css("position", "relative");
104+
$(this.output).css("height", "400px");
105+
$(this.output).css("width", "100%");
106+
outDiv.appendChild(this.output);
107+
this.outerDiv.appendChild(outDiv);
108+
var clearDiv = document.createElement("div");
109+
$(clearDiv).css("clear", "both"); // needed to make parent div resize properly
110+
this.outerDiv.appendChild(clearDiv);
111+
}
112+
enableSaveLoad() {
113+
$(this.runButton).text($.i18n("msg_activecode_render"));
114+
}
115+
}

runestone/activecode/js/timed_activecode.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import JSActiveCode from "./activecode_js";
99
import HTMLActiveCode from "./activecode_html";
1010
import SQLActiveCode from "./activecode_sql";
1111
import BrythonActiveCode from "./activecode_brython.js";
12+
import PyScriptActiveCode from "./activecode_pyscript.js";
1213

1314
var TimedActiveCodeMixin = {
1415
timedInit: async function (opts) {
@@ -154,3 +155,11 @@ export class TimedBrythonActiveCode extends BrythonActiveCode {
154155
}
155156
}
156157
Object.assign(TimedBrythonActiveCode.prototype, TimedActiveCodeMixin);
158+
159+
export class TimedPyScriptActiveCode extends PyScriptActiveCode {
160+
constructor(opts) {
161+
super(opts);
162+
this.timedInit(opts);
163+
}
164+
}
165+
Object.assign(TimedPyScriptActiveCode.prototype, TimedActiveCodeMixin);

runestone/activecode/test/_sources/index.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2831,7 +2831,7 @@ Trying Brython as Python 3 interpreter
28312831
--------------------------------------
28322832
.. activecode:: test_activecode_python3
28332833
:language: python3
2834-
:python3_interpreter: brython
2834+
:python3_interpreter: brython
28352835

28362836
print("You can see this print inside the iframe console")
28372837
from browser import document, alert, html
@@ -2842,3 +2842,12 @@ Trying Brython as Python 3 interpreter
28422842
document <= html.BUTTON("My button", id="button_alert")
28432843
document["button_alert"].bind("click", hello)
28442844

2845+
2846+
Trying PyScript as Python 3 interpreter
2847+
--------------------------------------
2848+
.. activecode:: test_activecode_pyscript
2849+
:language: python3
2850+
:python3_interpreter: pyscript
2851+
2852+
print("You can see this print inside the iframe console")
2853+

0 commit comments

Comments
 (0)