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