|
| 1 | +# Copyright (c) Metakernel Development Team. |
| 2 | +# Distributed under the terms of the Modified BSD License. |
| 3 | + |
| 4 | +from metakernel import Magic, option |
| 5 | +from IPython.display import IFrame, Javascript |
| 6 | +import string |
| 7 | +import random |
| 8 | +import os |
| 9 | + |
| 10 | + |
| 11 | +class BlocklyMagic(Magic): |
| 12 | + |
| 13 | + @option( |
| 14 | + '-o', '--html_from_origin', action='store', default=None, |
| 15 | + help='use the provided name as filename of html origin' |
| 16 | + ) |
| 17 | + @option( |
| 18 | + '-l', '--html_from_local', action='store', default=None, |
| 19 | + help='use the provided name as filename of html page' |
| 20 | + ) |
| 21 | + @option( |
| 22 | + '-t', '--template_data', action='store', default=None, |
| 23 | + help='generate page based on template, must be used with parameters(-ho or -hl)' |
| 24 | + 'use the provided name as workspace filename\n example : -ht xxx \n local include file: \n\txxx-toolbox.xml \n\txxx-workspace.xml \n\txxx-blocks.js' |
| 25 | + ) |
| 26 | + @option( |
| 27 | + '-h', '--height', action='store', default=350, |
| 28 | + help='set height of iframe ' |
| 29 | + ) |
| 30 | + def line_blockly(self, html_from_origin=None, html_from_local=None, template_data=None, height=350): |
| 31 | + """ |
| 32 | + %blockly - show visual code |
| 33 | +
|
| 34 | + This line magic will allow visual code editing |
| 35 | +
|
| 36 | + Examples: |
| 37 | + %blockly --html_from_origin http://host:port/blockly_page.html |
| 38 | + %blockly --html_from_local blockly_page.html |
| 39 | + %blockly --html_from_origin http://host:port/blockly_template.html --template_data template_data |
| 40 | + """ |
| 41 | + # Display iframe: |
| 42 | + script = """ |
| 43 | + if(document.receiveBlocklyPythonCode === undefined) { |
| 44 | + document.receiveBlocklyPythonCode = function ( event ) { |
| 45 | + //console.log( 'receiveMessage[Index]', event ); |
| 46 | + IPython.notebook.insert_cell_at_index(0, 2).set_text(event.data); |
| 47 | + } |
| 48 | + window.addEventListener("message", document.receiveBlocklyPythonCode, false) |
| 49 | + } |
| 50 | + """ |
| 51 | + #print(script) |
| 52 | + self.kernel.Display(Javascript(script)) |
| 53 | + if height is None: |
| 54 | + height = 350 |
| 55 | + if template_data is not None: |
| 56 | + if html_from_origin is not None: |
| 57 | + try: |
| 58 | + import urllib.request |
| 59 | + urlopen = urllib.request.urlopen |
| 60 | + except: # python2 |
| 61 | + import urllib |
| 62 | + urlopen = urllib.urlopen |
| 63 | + html_template = urlopen(html_from_origin).read().decode("utf-8") |
| 64 | + elif html_from_local is not None: |
| 65 | + with open(html_from_local, "rb") as fp: |
| 66 | + html_template = fp.read().decode("utf-8") |
| 67 | + else: |
| 68 | + raise |
| 69 | + with open(template_data+'-toolbox.xml', "rb") as fp: |
| 70 | + blockly_toolbar = fp.read().decode("utf-8") |
| 71 | + with open(template_data + '-workspace.xml', "rb") as fp: |
| 72 | + blockly_workspace = fp.read().decode("utf-8") |
| 73 | + with open(template_data + '-blocks.js', "rb") as fp: |
| 74 | + blockly_blocks = fp.read().decode("utf-8") |
| 75 | + html_template = html_template.replace("MY_BLOCKLY_TOOLBAR", blockly_toolbar) |
| 76 | + html_template = html_template.replace("MY_BLOCKLY_WORKSPACE", blockly_workspace) |
| 77 | + html_template = html_template.replace("MY_BLOCKLY_BLOCKS", blockly_blocks) |
| 78 | + with open(template_data + '.html', 'w') as fp: |
| 79 | + fp.write(html_template) |
| 80 | + html_from_local = template_data + '.html' |
| 81 | + if html_from_local is not None: |
| 82 | + self.kernel.Display(IFrame(html_from_local, width='100%', height=height)) |
| 83 | + elif html_from_origin is not None: |
| 84 | + self.kernel.Display(IFrame(html_from_origin, width='100%', height=height)) |
| 85 | + else: |
| 86 | + raise |
| 87 | + |
| 88 | + |
| 89 | +def register_magics(kernel): |
| 90 | + kernel.register_magics(Magic) |
| 91 | + |
| 92 | + |
| 93 | +def register_ipython_magics(): |
| 94 | + from metakernel import IPythonKernel |
| 95 | + from IPython.core.magic import register_line_magic |
| 96 | + kernel = IPythonKernel() |
| 97 | + magic = BlocklyMagic(kernel) |
| 98 | + # Make magics callable: |
| 99 | + kernel.line_magics["blockly"] = magic |
| 100 | + |
| 101 | + @register_line_magic |
| 102 | + def blockly(line): |
| 103 | + """ |
| 104 | + Use the blockly code visualizer and generator. |
| 105 | + """ |
| 106 | + kernel.call_magic("%blockly " + line) |
0 commit comments