-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.html
More file actions
110 lines (107 loc) · 3.93 KB
/
index.html
File metadata and controls
110 lines (107 loc) · 3.93 KB
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
105
106
107
108
109
110
<!doctype html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.58.2/codemirror.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.58.2/codemirror.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.58.2/theme/ayu-mirage.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.58.2/mode/python/python.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.58.2/addon/comment/comment.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.58.2/keymap/sublime.js"></script>
<link rel="stylesheet" type="text/css" href="index.css">
<script src="./lib/brython-runner.bundle.js"></script>
</head>
<body>
<div class="dev-box">
<div class="toolbar-row">
<button class="toolbar-button" onclick="run()">
Run
</button>
</div>
<div class="workspace-row">
<div class="editor-column">
<div class="editor" id="editor"></div>
</div>
<div class="run-column">
<div class="output-box" id="output">
</div>
</div>
</div>
</div>
<script>
// Initialize the code editor.
const CODEMIRROR_OPTIONS = {
lineNumbers: true,
lineSeparator: '\n',
mode: undefined,
theme: 'ayu-mirage',
fontSize: 14,
indentUnit: 4,
tabSize: 4,
indentWithTabs: false,
lineWrapping: true,
readOnly: false,
smartIndent: true,
matchBrackets: true,
autoCloseBrackets: true,
showTrailingSpace: true,
keyMap: 'sublime',
extraKeys: null,
showInvisibles: true,
viewportMargin: Infinity,
}
const codeStoreKey = 'brython-runner-dev-code'
const loadedCode = localStorage.getItem(codeStoreKey)
const code = loadedCode || 'print("hello world")'
const editor = CodeMirror(document.getElementById('editor'), {
...CODEMIRROR_OPTIONS,
mode: 'python',
value: code,
})
editor.on('change', (editor, change) => {
localStorage.setItem(codeStoreKey, editor.getDoc().getValue())
})
const runner = new BrythonRunner({
stdout: {
write(content) {
var el = document.createElement('code')
var text = document.createTextNode(content)
el.appendChild(text)
document.getElementById('output').appendChild(el)
},
flush() { }
},
stderr: {
write(content) {
var el = document.createElement('code')
var text = document.createTextNode(content)
el.appendChild(text)
el.setAttribute('class', 'error')
document.getElementById('output').appendChild(el)
},
flush() { }
},
stdin: {
async readline() {
var data = prompt()
var el = document.createElement('code')
var text = document.createTextNode(data + '\n')
el.appendChild(text)
document.getElementById('output').appendChild(el)
return data
},
}
})
function clearOutput() {
document.getElementById('output').innerHTML = ''
}
function getCode() {
return editor.getDoc().getValue()
}
function run() {
clearOutput()
const code = getCode()
runner.runCode(code)
}
</script>
</body>
</html>