This repository was archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 267
Expand file tree
/
Copy pathReportMagics.py
More file actions
94 lines (80 loc) · 2.94 KB
/
ReportMagics.py
File metadata and controls
94 lines (80 loc) · 2.94 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
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
#iPython notebook magic
from IPython.core.magic import (
Magics, magics_class, cell_magic, line_magic,register_line_cell_magic, needs_local_scope
)
@magics_class
class ReportMagic(Magics):
var_files = ''
def __init__(self, shell, data):
super(ReportMagic,self).__init__(shell)
self._code_store = []
self._markdown_store = []
self._conf_code_store=[]
self._conf_markdown_store=[]
self.data = data
shell.user_ns['__mycodestore'] = self._code_store
shell.user_ns['__mymarkdownstore'] = self._markdown_store
self.ip = get_ipython()
@cell_magic
@needs_local_scope
def add_code_to_report(self, line='\r\n', cell='\r\n'):
"""store the cell in the store"""
self._code_store.append('\r\n')
self._code_store.append(cell)
self.ip.run_cell(cell)
@line_magic
@needs_local_scope
def add_conf_line_to_report(self, line, cell=None,local_ns=None):
"""store the cell in the store"""
if cell is None:
cell = line
self._code_store.append(cell)
self._code_store = list(set(self._code_store))
self.ip.run_cell(cell)
return cell
@line_magic
@needs_local_scope
def add_interaction_code_to_report(self, line, cell=None,local_ns=None):
"""store the cell in the store"""
if cell is None:
cell = line
self._code_store.append(cell)
self._code_store = list(set(self._code_store))
self.ip.run_cell(cell)
return cell
@line_magic
def add_markdown_to_report(self, line):
"""store the cell in the store"""
self._markdown_store.append(cell)
@cell_magic
@needs_local_scope
def add_conf_code_to_report(self, line, cell):
"""store the cell in the store"""
self._conf_code_store.append("\r\n")
self._conf_code_store.append(cell)
self.ip.run_cell(cell)
@cell_magic
def add_conf_markdown_to_report(self, line, cell):
"""store the cell in the store"""
self._conf_markdown_store.append(cell)
@line_magic
def show_report(self,line):
"""show all recorded statements"""
self._conf_markdown_store = ['# Configuration Code']
self._markdown_store = ['## Target variable']
return self._conf_markdown_store,self._conf_code_store ,self._markdown_store,self._code_store
@line_magic
def reset_report(self, line):
self._markdown_store = []
self._code_store = []
@line_magic
def reset_all(self, line):
self._conf_markdown_store = []
self._markdown_store = []
self._conf_code_store = []
self._code_store = []
# This class must then be registered with a manually created instance,
# since its constructor has different arguments from the default:
ip = get_ipython()
magics = ReportMagic(ip, 0)
ip.register_magics(magics)