@@ -102,7 +102,7 @@ openBuffer(
102
102
openBuffer (
103
103
buffers ,
104
104
'js' ,
105
- '// Javascript code go here' ,
105
+ '// Javascript code goes here' ,
106
106
'javascript' ,
107
107
buffersDropDown ,
108
108
buffersList
@@ -136,13 +136,15 @@ function onReady() {
136
136
notebook . innerHTML = '' ;
137
137
}
138
138
139
- document . getElementById ( 'run-btn' ) . addEventListener ( 'click' , readEditors ) ;
139
+ document . getElementById ( 'run-btn' ) . addEventListener ( 'click' , executeNotebook ) ;
140
+
141
+ let pyvm = null ;
140
142
141
143
// on click of run
142
144
// 1. add css stylesheet
143
145
// 2. get and run content of all tabs (including dynamically added ones)
144
146
// 3. run main tab.
145
- function readEditors ( ) {
147
+ async function executeNotebook ( ) {
146
148
// Clean the console and errors
147
149
notebook . innerHTML = '' ;
148
150
error . textContent = '' ;
@@ -165,9 +167,39 @@ function readEditors() {
165
167
// do nothing
166
168
}
167
169
168
- //
170
+ if ( pyvm ) {
171
+ pyvm . destroy ( ) ;
172
+ pyvm = null ;
173
+ }
174
+ pyvm = rp . vmStore . init ( 'notebook_vm' ) ;
175
+
176
+ // add some helpers for js/python code
177
+ window . injectPython = ( ns ) => {
178
+ for ( const [ k , v ] of Object . entries ( ns ) ) {
179
+ pyvm . addToScope ( k , v ) ;
180
+ }
181
+ } ;
182
+ window . pushNotebook = ( elem ) => {
183
+ notebook . appendChild ( elem ) ;
184
+ } ;
185
+ pyvm . setStdout ( ( text ) => {
186
+ const para = document . createElement ( 'p' ) ;
187
+ para . appendChild ( document . createTextNode ( text ) ) ;
188
+ notebook . appendChild ( para ) ;
189
+ } ) ;
190
+ for ( const el of [ 'h1' , 'h2' , 'h3' , 'h4' , 'h5' , 'h6' , 'p' ] ) {
191
+ pyvm . addToScope ( el , ( text ) => {
192
+ const elem = document . createElement ( el ) ;
193
+ elem . appendChild ( document . createTextNode ( text ) ) ;
194
+ notebook . appendChild ( elem ) ;
195
+ } ) ;
196
+ }
197
+ pyvm . addToScope ( 'notebook_html' , ( html ) => {
198
+ notebook . innerHTML += html ;
199
+ } ) ;
200
+
169
201
let jsCode = buffers [ 'js' ] . getValue ( ) ;
170
- runJS ( jsCode ) ;
202
+ await runJS ( jsCode ) ;
171
203
172
204
// get all the buffers, except css, js and main
173
205
// css is auto executed at the start
@@ -177,20 +209,10 @@ function readEditors() {
177
209
178
210
for ( const [ name ] of Object . entries ( pythonBuffers ) ) {
179
211
let pythonCode = buffers [ name ] . getValue ( ) ;
180
- runPython ( pythonCode , notebook , error ) ;
212
+ runPython ( pyvm , pythonCode , error ) ;
181
213
}
182
214
183
- parseCodeFromMainEditor ( ) ;
184
- }
185
-
186
- // Parses what is the code editor
187
- // either runs python or renders math or markdown
188
- function parseCodeFromMainEditor ( ) {
189
- // TODO: fix how javascript is injected and executed
190
- // Read javascript code from the jsEditor
191
- // Inject JS into DOM, so that functions can be called from python
192
- // let js_code = buffers["js"].getValue();
193
- // runJS(js_code);
215
+ // now parse from the main editor
194
216
195
217
// gets code from main editor
196
218
let mainCode = buffers [ 'main' ] . getValue ( ) ;
@@ -202,7 +224,7 @@ function parseCodeFromMainEditor() {
202
224
- evalFlags, startLine, endLine
203
225
*/
204
226
let parsedCode = iomdParser ( mainCode ) ;
205
- parsedCode . forEach ( async ( chunk ) => {
227
+ for ( const chunk of parsedCode ) {
206
228
// For each type of chunk, do somthing
207
229
// so far have py for python, md for markdown and math for math ;p
208
230
let content = chunk . chunkContent ;
@@ -211,11 +233,11 @@ function parseCodeFromMainEditor() {
211
233
// so users don't have to type py manually
212
234
case '' :
213
235
case 'py' :
214
- runPython ( content , notebook , error ) ;
236
+ runPython ( pyvm , content , error ) ;
215
237
break ;
216
238
// TODO: fix how js is injected and ran
217
239
case 'js' :
218
- runJS ( content ) ;
240
+ await runJS ( content ) ;
219
241
break ;
220
242
case 'md' :
221
243
notebook . innerHTML += renderMarkdown ( content ) ;
@@ -226,7 +248,7 @@ function parseCodeFromMainEditor() {
226
248
default :
227
249
// do nothing when we see an unknown chunk for now
228
250
}
229
- } ) ;
251
+ }
230
252
}
231
253
232
254
function updatePopup ( type , message ) {
0 commit comments