A monorepo containing jupyter-blocks and jupyter-tidyblocks. Blockly extension for JupyterLab.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #30 from DenisaCG/blocks_metadata

add checks for top level initializations of blocks

authored by

Carlos Herrero and committed by
GitHub
5181e91e 44470a07

+40 -2
+40 -2
src/layout.ts
··· 102 102 return; 103 103 } 104 104 105 + /** 106 + * Return the extra coded (if it exists), composed of the individual 107 + * data from each block in the workspace, which are defined in the 108 + * toplevel_init property. (e.g. : imports needed for the block) 109 + * 110 + * Add extra code example: 111 + * Blockly.Blocks['block_name'].toplevel_init = `import numpy` 112 + */ 113 + getBlocksToplevelInit(): string { 114 + // Initalize string which will return the extra code provided 115 + // by the blocks, in the toplevel_init property. 116 + let finalToplevelInit = ''; 117 + 118 + // Get all the blocks in the workspace in order. 119 + const ordered = true; 120 + const used_blocks = this._workspace.getAllBlocks(ordered); 121 + 122 + // For each block in the workspace, check if theres is a toplevel_init, 123 + // if there is, add it to the final string. 124 + for (const block in used_blocks) { 125 + const current_block = used_blocks[block].type; 126 + if (Blockly.Blocks[current_block].toplevel_init) { 127 + // console.log(Blockly.Blocks[current_block].toplevel_init); 128 + // Attach it to the final string 129 + const string = Blockly.Blocks[current_block].toplevel_init; 130 + finalToplevelInit = finalToplevelInit + string; 131 + } 132 + } 133 + // console.log(finalToplevelInit); 134 + return finalToplevelInit; 135 + } 136 + 105 137 run(): void { 138 + // Get extra code from the blocks in the workspace. 139 + const extra_init = this.getBlocksToplevelInit(); 106 140 // Serializing our workspace into the chosen language generator. 107 - const code = this._manager.generator.workspaceToCode(this._workspace); 141 + const code = 142 + extra_init + this._manager.generator.workspaceToCode(this._workspace); 108 143 this._cell.model.sharedModel.setSource(code); 109 144 this.addWidget(this._cell); 110 145 this._resizeWorkspace(); ··· 181 216 change: BlocklyManager.Change 182 217 ) { 183 218 if (change === 'kernel') { 219 + // Get extra code from the blocks in the workspace. 220 + const extra_init = this.getBlocksToplevelInit(); 184 221 // Serializing our workspace into the chosen language generator. 185 - const code = this._manager.generator.workspaceToCode(this._workspace); 222 + const code = 223 + extra_init + this._manager.generator.workspaceToCode(this._workspace); 186 224 this._cell.model.sharedModel.setSource(code); 187 225 this._cell.model.mimeType = this._manager.mimeType; 188 226 }