@@ -102,7 +102,7 @@ openBuffer(
102102openBuffer (
103103 buffers ,
104104 'js' ,
105- '// Javascript code go here' ,
105+ '// Javascript code goes here' ,
106106 'javascript' ,
107107 buffersDropDown ,
108108 buffersList
@@ -136,13 +136,15 @@ function onReady() {
136136 notebook . innerHTML = '' ;
137137}
138138
139- document . getElementById ( 'run-btn' ) . addEventListener ( 'click' , readEditors ) ;
139+ document . getElementById ( 'run-btn' ) . addEventListener ( 'click' , executeNotebook ) ;
140+
141+ let pyvm = null ;
140142
141143// on click of run
142144// 1. add css stylesheet
143145// 2. get and run content of all tabs (including dynamically added ones)
144146// 3. run main tab.
145- function readEditors ( ) {
147+ async function executeNotebook ( ) {
146148 // Clean the console and errors
147149 notebook . innerHTML = '' ;
148150 error . textContent = '' ;
@@ -165,9 +167,39 @@ function readEditors() {
165167 // do nothing
166168 }
167169
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+
169201 let jsCode = buffers [ 'js' ] . getValue ( ) ;
170- runJS ( jsCode ) ;
202+ await runJS ( jsCode ) ;
171203
172204 // get all the buffers, except css, js and main
173205 // css is auto executed at the start
@@ -177,20 +209,10 @@ function readEditors() {
177209
178210 for ( const [ name ] of Object . entries ( pythonBuffers ) ) {
179211 let pythonCode = buffers [ name ] . getValue ( ) ;
180- runPython ( pythonCode , notebook , error ) ;
212+ runPython ( pyvm , pythonCode , error ) ;
181213 }
182214
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
194216
195217 // gets code from main editor
196218 let mainCode = buffers [ 'main' ] . getValue ( ) ;
@@ -202,7 +224,7 @@ function parseCodeFromMainEditor() {
202224 - evalFlags, startLine, endLine
203225 */
204226 let parsedCode = iomdParser ( mainCode ) ;
205- parsedCode . forEach ( async ( chunk ) => {
227+ for ( const chunk of parsedCode ) {
206228 // For each type of chunk, do somthing
207229 // so far have py for python, md for markdown and math for math ;p
208230 let content = chunk . chunkContent ;
@@ -211,11 +233,11 @@ function parseCodeFromMainEditor() {
211233 // so users don't have to type py manually
212234 case '' :
213235 case 'py' :
214- runPython ( content , notebook , error ) ;
236+ runPython ( pyvm , content , error ) ;
215237 break ;
216238 // TODO: fix how js is injected and ran
217239 case 'js' :
218- runJS ( content ) ;
240+ await runJS ( content ) ;
219241 break ;
220242 case 'md' :
221243 notebook . innerHTML += renderMarkdown ( content ) ;
@@ -226,7 +248,7 @@ function parseCodeFromMainEditor() {
226248 default :
227249 // do nothing when we see an unknown chunk for now
228250 }
229- } ) ;
251+ }
230252}
231253
232254function updatePopup ( type , message ) {
0 commit comments