-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path__init__.py
More file actions
69 lines (56 loc) · 1.75 KB
/
Copy path__init__.py
File metadata and controls
69 lines (56 loc) · 1.75 KB
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
from IPython.core.magic import register_cell_magic
from IPython.core.inputtransformer import StatelessInputTransformer
from IPython.display import display, HTML
from IPython.core.error import TryNext
import warnings
from .node import Node
import os
import subprocess
nodeapp = Node()
@register_cell_magic
def node(line, cell):
return nodeapp.write(cell)
@register_cell_magic
def py(line, cell):
return cell
def shutdown_hook(ipython):
nodeapp.terminate()
raise TryNext
ip = get_ipython()
def modify_for_node_for_new_version(lines):
if lines[0].strip() == '%%py':
lines.pop(0)
else:
lines.insert(0, '%%node\n')
return lines
class NodeInputTransformer():
def __init__(self):
self.lines=[]
self.lineNumber=0
self.isPython=False
def push(self, line):
if self.lineNumber==0 and line.strip()[0:28]=='get_ipython().run_cell_magic':
self.isPython=True
if line.strip()[0:40]=="get_ipython().run_cell_magic('py', '', '":
return '\n'.join(line.strip()[40:-2].split('\\n'))
return line
if self.isPython ==True:
return line
if self.lineNumber==0:
self.lines.append(line)
else:
self.lines.append(line)
self.lineNumber+=1
return ''
def reset(self):
if self.lineNumber!=0:
self.lineNumber=0
nodeapp.write('\n'.join(self.lines))
self.lines=[]
self.lineNumber=0
self.isPython=False
pass
if hasattr(ip, 'input_transformers_cleanup'):
ip.input_transformers_cleanup.append(modify_for_node_for_new_version)
else:
ip.input_transformer_manager.logical_line_transforms.insert(0, NodeInputTransformer())