-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
104 lines (101 loc) · 3.34 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<html>
<head>
<!--
It looks like there was a bug introduced, at least for us, in Brython 3.9.1. I have an issue here: brython-dev/brython#1595 that should hopefully explain what's happening. Until then, forcing 3.9.0 should be good for us. We've been unable to replicate, so this is just a guess.
-->
<script type="text/javascript" src="https://cdn.rawgit.com/brython-dev/brython/3.9.0/www/src/brython.js"></script>
<script src="https://cdn.rawgit.com/brython-dev/brython/3.9.0/www/src/brython_stdlib.js"></script>
<script>
'use strict';
let evalScript;
window.addEventListener('load', function(e) {
postToParent({type: 'load'});
});
function postToParent(msg) {
window.parent.postMessage(msg, '*');
}
const brythonOptions = {
debug: 1,
indexedDB: window.safari !== undefined,
};
window.addEventListener('message', function(e) {
var data = e.data;
switch (data.type) {
case 'eval':
if (typeof evalScript !== 'undefined') {
document.body.removeChild(evalScript);
}
const files = data.data;
const mainFile = files.filter(file => {
return file.isActive;
})[0];
var rawFile = new XMLHttpRequest();
rawFile.open("GET", "main.py", false);
const program = rawFile.text;
postToParent({type: 'program_started'});
try {
evalScript = document.createElement('script');
evalScript.type = 'text/python';
evalScript.innerHTML = `from brython_graphics import *\n${program}`;
} catch (e) {
postToParent({type: 'program_error', error: e.message});
}
// overwrite console.error to briefly capture the console output in the case of an error.
// Brython reports syntaxerrors directly to the console:
// https://brython.info/static_doc/en/test.html
const oldError = console.error;
let errors = [];
console.error = function(output) {
errors = errors.concat(output);
};
document.body.appendChild(evalScript);
if (typeof brython === 'function') {
try {
brython(brythonOptions);
} catch(e) {
let currentError = errors.pop();
if (currentError) {
postToParent({type: 'program_stderr', stderr: currentError});
}
}
} else {
alert('Brython not defined');
}
console.error = oldError;
break;
default:
break;
}
});
</script>
<style>
html, body {
margin: 0;
padding: 0;
border: 0;
fontSize: 100%;
font: inherit;
verticalAlign: baseline;
height: 100%;
width: 100%;
}
body {
display: flex;
align-items: center;
justify-content: center;
}
iframe {
width: 100%;
height: 100%;
border: 0;
}
canvas {
border: 1px solid #d3d3d3;
margin: 5px;
}
</style>
</head>
<body>
<canvas id="brython-canvas" width="400" height="480"></canvas>
</body>
</html>