()
| 146 | // 2. get and run content of all tabs (including dynamically added ones) |
| 147 | // 3. run main tab. |
| 148 | async function executeNotebook() { |
| 149 | // Clean the console and errors |
| 150 | notebook.innerHTML = ''; |
| 151 | error.textContent = ''; |
| 152 | |
| 153 | // get the content of the css editor |
| 154 | // and add the css to the head |
| 155 | // use dataset.status for a flag to know when to update |
| 156 | let cssCode = buffers['css'].getValue(); |
| 157 | let cssStatus = checkCssStatus(); |
| 158 | switch (cssStatus) { |
| 159 | case 'none': |
| 160 | addCSS(cssCode); |
| 161 | break; |
| 162 | case 'modified': |
| 163 | // remove the old style then add the new one |
| 164 | document.getElementsByTagName('style')[0].remove(); |
| 165 | addCSS(cssCode); |
| 166 | break; |
| 167 | default: |
| 168 | // do nothing |
| 169 | } |
| 170 | |
| 171 | if (pyvm) { |
| 172 | pyvm.destroy(); |
| 173 | pyvm = null; |
| 174 | } |
| 175 | pyvm = rp.vmStore.init('notebook_vm'); |
| 176 | |
| 177 | // add some helpers for js/python code |
| 178 | window.injectPython = (ns) => { |
| 179 | for (const [k, v] of Object.entries(ns)) { |
| 180 | pyvm.addToScope(k, v); |
| 181 | } |
| 182 | }; |
| 183 | window.pushNotebook = (elem) => { |
| 184 | notebook.appendChild(elem); |
| 185 | }; |
| 186 | window.handlePyError = (err) => { |
| 187 | handlePythonError(error, err); |
| 188 | }; |
| 189 | pyvm.setStdout((text) => { |
| 190 | const para = document.createElement('p'); |
| 191 | para.appendChild(document.createTextNode(text)); |
| 192 | notebook.appendChild(para); |
| 193 | }); |
| 194 | for (const el of ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p']) { |
| 195 | pyvm.addToScope(el, (text) => { |
| 196 | const elem = document.createElement(el); |
| 197 | elem.appendChild(document.createTextNode(text)); |
| 198 | notebook.appendChild(elem); |
| 199 | }); |
| 200 | } |
| 201 | pyvm.addToScope('notebook_html', (html) => { |
| 202 | notebook.innerHTML += html; |
| 203 | }); |
| 204 | |
| 205 | let jsCode = buffers['js'].getValue(); |
nothing calls this directly
no test coverage detected