Skip to content
Merged
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
5,022 changes: 5,021 additions & 1 deletion package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
"author": "",
"license": "ISC",
"devDependencies": {
"css-minimizer-webpack-plugin": "^3.0.0",
"copy-webpack-plugin": "^8.0.0",
"css-loader": "^5.0.0",
"css-minimizer-webpack-plugin": "^3.0.0",
"eslint": "^7.0.0",
"html-loader": "^2.0.0",
"html-webpack-plugin": "^5.0.0",
Expand All @@ -41,4 +41,4 @@
"vega-embed": "3.14.0",
"wavedrom": "^2.0.0"
}
}
}
94 changes: 66 additions & 28 deletions runestone/activecode/js/activecode_pyscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,51 +29,84 @@ export default class PyScriptActiveCode extends ActiveCode {
<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; font-size: 13px; width: 94%; padding: 9.5px; line-height: 1.42857143; border: 1px ; border-radius: 4px;
#console {
width: 100%;
height: 150px;
margin-bottom: 20px;
border: 1px solid #ddd;
border-radius: 4px;
padding: 10px;
font-family: monospace;
background-color: #f5f5f5;
white-space: pre-wrap;
word-wrap: break-word;
overflow-y: auto;
}
code{
border: 1px solid #ccc; border-radius: 4px;
#plot_area {
width: 100%;
height: 500px;
border: 1px solid #ddd;
border-radius: 4px;
padding: 10px;
background-color: #f8f8f8;
display: flex;
justify-content: center;
align-items: center;
}
#plot_area img {
max-width: 100%;
max-height: 100%;
object-fit: contain;
}
</style>
</head>
<body>
<py-config>
terminal = false
packages = [ "pandas", "numpy", "matplotlib"]
packages = ["pandas", "numpy", "matplotlib", "sympy"]
</py-config>
<pre id="consolePre">
<code id="consoleCode"></code>
</pre>
<div id="console"></div>
<div id="plot_area"></div>
<py-script>
import sys
from js import document
logger = document.getElementById('consoleCode')
preElem = document.getElementById('consolePre')
import matplotlib.pyplot as plt
import io
import base64

class NewOut:
def write(self, data):
logger.innerHTML += str(data)
display(data, "console")
sys.stderr = sys.stdout = NewOut()

def display(obj, target="console"):
target_div = document.getElementById(target)
if not target_div:
raise ValueError(f"Target div '{target}' not found")

if target == "plot_area":
if isinstance(obj, plt.Figure):
fig = obj
else:
fig = plt.gcf()
buf = io.BytesIO()
fig.savefig(buf, format='png', dpi=100, bbox_inches='tight')
buf.seek(0)
img_str = base64.b64encode(buf.getvalue()).decode('utf-8')
target_div.innerHTML = f'<img src="data:image/png;base64,{img_str}" />'
plt.close(fig)
elif target == "console":
target_div.innerHTML += str(obj) + '\\n'
target_div.scrollTop = target_div.scrollHeight

def my_exec(code):
try:
exec(code)
preElem.style.visibility = "visible"
preElem.style.bottom = "5px"
logger.classList.add("plaintext")
except Exception as err:
error_class = err.__class__.__name__
detail = err.args[0]
line_number = "" # PyScript does not currently expose line numbers
result = f"'{error_class}': {detail} {line_number}"
print(result)
# Styling the pre element for error
preElem.style.visibility = "visible"
preElem.style.top = "5px"
preElem.style.backgroundColor = "#f2dede"
preElem.style.border = "1px solid #ebccd1"
logger.classList.add("python")
result = f"'{error_class}': {detail}"
display(result, "console")

# usage
my_exec("""${prog}
Expand All @@ -98,17 +131,22 @@ my_exec("""${prog}
$(outDiv).addClass("col-md-5");
}
this.outDiv = outDiv;

this.output = document.createElement("iframe");
$(this.output).css("background-color", "white");
$(this.output).css("position", "relative");
$(this.output).css("height", "400px");
$(this.output).css("width", "100%");
$(this.output).css({
"background-color": "white",
"position": "relative",
"height": "750px",
"width": "100%"
});
outDiv.appendChild(this.output);

this.outerDiv.appendChild(outDiv);
var clearDiv = document.createElement("div");
$(clearDiv).css("clear", "both"); // needed to make parent div resize properly
$(clearDiv).css("clear", "both");
this.outerDiv.appendChild(clearDiv);
}

enableSaveLoad() {
$(this.runButton).text($.i18n("msg_activecode_render"));
}
Expand Down
Loading