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