Skip to content

Commit 3cf80b2

Browse files
committed
[feat] update blockly_magic
1 parent 270cab0 commit 3cf80b2

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

metakernel/magics/blockly_magic.py

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,88 @@
11
# Copyright (c) Metakernel Development Team.
22
# Distributed under the terms of the Modified BSD License.
3+
# @author ChrisJaunes
34

45
from metakernel import Magic, option
56
from IPython.display import IFrame, Javascript
6-
import string
7-
import random
8-
import os
97

108

119
class BlocklyMagic(Magic):
1210

1311
@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'
1614
)
1715
@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'
2018
)
2119
@option(
2220
'-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+
)
2623
@option(
2724
'-h', '--height', action='store', default=350,
2825
help='set height of iframe '
2926
)
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):
3128
"""
3229
%blockly - show visual code
3330
3431
This line magic will allow visual code editing
35-
32+
If both -o and -l are provided, only -l is used
33+
3634
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
4039
"""
4140
# Display iframe:
4241
script = """
4342
if(document.receiveBlocklyPythonCode === undefined) {
4443
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);
4745
}
4846
window.addEventListener("message", document.receiveBlocklyPythonCode, false)
4947
}
5048
"""
5149
#print(script)
5250
self.kernel.Display(Javascript(script))
51+
5352
if height is None:
5453
height = 350
5554
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:
5759
try:
5860
import urllib.request
5961
urlopen = urllib.request.urlopen
6062
except: # python2
6163
import urllib
6264
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")
6766
else:
68-
raise
67+
raise ValueError("No -l or -o is provided")
6968
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)
7171
with open(template_data + '-workspace.xml', "rb") as fp:
7272
blockly_workspace = fp.read().decode("utf-8")
73+
html_template = html_template.replace("MY_BLOCKLY_WORKSPACE", blockly_workspace)
7374
with open(template_data + '-blocks.js', "rb") as fp:
7475
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)
7877
with open(template_data + '.html', 'w') as fp:
7978
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))
8584
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))
8786

8887

8988
def register_magics(kernel):

0 commit comments

Comments
 (0)