/
magic_scratch.py
94 lines (81 loc) · 2.71 KB
/
magic_scratch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#-*- coding: utf-8 -*-
"""
@file
@brief Magic command to handle files
"""
import os
import glob
from IPython.core.magic import magics_class, line_magic
from IPython.core.display import HTML, display_html, Javascript
from pyquickhelper.ipythonhelper import MagicCommandParser, MagicClassWithHelpers
from ..jsscripts.snap import __file__ as location_js_snap
@magics_class
class MagicScratch(MagicClassWithHelpers):
"""
Defines magic commands to list the content of a folder
.. versionadded:: 1.1
"""
@staticmethod
def snap_parser():
"""
defines the way to parse the magic command ``%snap``
"""
parser = MagicCommandParser(
description='insert a snap window inside a notebook')
parser.add_argument(
'-f',
'--file',
type=str,
default="",
help='scratch or snap file to display')
parser.add_argument(
'-d',
'--div',
type=str,
default="scratch-div-id",
help='id for the HTML div')
return parser
@line_magic
def snap(self, line):
"""
defines ``%snap``
which inserts a snap window inside a notebook
"""
parser = self.get_parser(MagicScratch.snap_parser, "snap")
args = self.get_args(line, parser)
if args is not None:
if args.f in [None, ""]:
#filename = None
pass
else:
raise NotImplementedError()
iddiv = args.div
js_path = os.path.dirname(location_js_snap)
files = [os.path.split(_)[-1]
for _ in glob.glob(js_path + "/*.js")]
path = "/static/snap/"
js_libs = [path + _ for _ in files]
html_src = """
<div id="{0}"> </div>
""".format(iddiv)
test_js = """
var world;
window.onload = function () {
world = new WorldMorph(document.getElementById('{0}'));
world.worldCanvas.focus();
new IDE_Morph().openIn(world);
setInterval(loop, 1);
};
function loop() {
world.doOneCycle();
}
""".format(iddiv)
display_html(HTML(data=html_src))
return Javascript(data=test_js, lib=js_libs)
def register_scratch_magics():
"""
register magics function, can be called from a notebook
"""
from IPython import get_ipython
ip = get_ipython()
ip.register_magics(MagicScratch)