-
Notifications
You must be signed in to change notification settings - Fork 5
/
clrmagic.py
81 lines (69 loc) · 2.48 KB
/
clrmagic.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
import clr
def create_cs_function(name, code, dependencies = None):
clr.AddReference("clrmagic")
from MagicIPython import MagicCS
from System import String
from System.Collections.Generic import List
if dependencies is not None and len(dependencies) > 0 :
myarray = List[String]()
for i,d in enumerate(dependencies):
myarray.Add( d )
myarray = myarray.ToArray()
else:
myarray = List[String]().ToArray()
obj = MagicCS.CreateFunction(name, code, myarray)
return lambda *params: run_cs_function(obj, params)
def run_cs_function(func, params):
clr.AddReference("clrmagic")
from MagicIPython import MagicCS
from System.Collections.Generic import List
from System import Object
par = List[Object]()
for p in params :
par.Add ( p )
return MagicCS.RunFunction(func, par.ToArray())
import sys
from IPython.core.magic import Magics, magics_class, line_magic, cell_magic
from IPython.core.magic import line_cell_magic
from IPython.core.display import HTML
from IPython.core import display
@magics_class
class CustomMagics(Magics):
@cell_magic
def CS(self, line, cell):
"""
Defines command ``%%CS``.
"""
#if not sys.platform.startswith("win"):
# raise Exception("Works only on Windows.")
#from clrfunction import create_cs_function
if line is not None:
spl = line.strip().split(" ")
name = spl[0]
deps = " ".join(spl[1:]) if len(spl) > 1 else ""
deps = deps.split(";")
if name == "-h":
print( "Usage: "
" %%CS function_name dependency1;dependency2"
" function code")
else :
try:
f = create_cs_function(name, cell, deps)
except Exception as e :
print(e)
return
if self.shell is not None:
self.shell.user_ns[name] = f
return f
def load_ipython_extension(ip):
"""
register magics function, can be called from a notebook
"""
#ip = get_ipython()
ip.register_magics(CustomMagics)
# enable C# (CSHARP) highlight
patch = ("IPython.config.cell_magic_highlight['clrmagic'] = "
"{'reg':[/^%%CS/]};")
js = display.Javascript(data=patch,
lib=["https://github.com/codemirror/CodeMirror/blob/master/mode/clike/clike.js"])
#register_magics()