-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path_render_ipynb.py
More file actions
169 lines (161 loc) · 4.02 KB
/
_render_ipynb.py
File metadata and controls
169 lines (161 loc) · 4.02 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!python3
import json
import io
import editor
import console
def parse_ipynb():
try:
ipynb = json.loads(editor.get_text())
except TypeError:
console.hud_alert("Tap 'Edit as Text' first", 'error')
return
try:
for cell in ipynb['cells']:
if cell['cell_type'] == 'markdown':
for line in cell['source']:
print(line)
print()
# source: 마크다운 내용, 각 줄(str)의 리스트
elif cell['cell_type'] == 'code':
print('['+str(cell['execution_count'])+']')
for line in cell['source']:
print('>>>', line.strip())
print()
output = cell['outputs']
for out_cell in output:
if out_cell['output_type'] == 'execute_result':
print(out_cell['execution_count'],)
for l in out_cell['data']:
if l == 'text/plain':
for txt in out_cell['data'][l]:
print(txt.strip())
elif l == 'text/html':
for txt in out_cell['data'][l]:
print(txt)
print()
elif out_cell['output_type'] == 'stream':
for line in out_cell['text']:
print(line.strip())
elif out_cell['output_type'] == 'error':
print(out_cell['ename'] + ': ' + out_cell['evalue'])
for line in out_cell['traceback']:
print(line)
else:
print('+'*50)
print()
# source: 코드 내용, 각 줄(str)의 리스트
# outputs:
# name: 아웃풋 이름 - stdout,
# output_type: - stream
# text: - 아웃풋 내용, 각 줄의 리스트
# execution_count: 실행 순서(int 혹은 null)
elif cell['cell_type'] == 'raw':
print(cell['source'])
print()
# source: 코드 내용, 각 줄(str)의 리스트
else:
print('+'*50)
print(cell)
print('='*50)
print()
except KeyError:
console.hud_alert('Not a proper notebook', 'error')
if __name__ == '__main__':
parse_ipynb()
'''
프린트만 있는 셀
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5, 6]\n",
"[6, 5, 4, 3, 2, 1]\n"
]
}
],
"source": [
"a = [6, 5, 4, 3, 2, 1]\n",
"print merge_sort(a)\n",
"print a"
]
}
리턴만 있는 셀
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4]"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[1,2,3,4]"
]
}
리턴, 프린트 있는 셀
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1, 2, 3, 4)\n"
]
},
{
"data": {
"text/plain": [
"[1, 2, 3, 4]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print (1,2,3,4)\n",
"[1,2,3,4]"
]
}
빈 셀
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
rawnbconvert 셀
{
"cell_type": "raw",
"metadata": {},
"source": [
"gggggggg"
]
}
'''