-
Notifications
You must be signed in to change notification settings - Fork 2
/
sage2ipython.py
179 lines (153 loc) · 5.32 KB
/
sage2ipython.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
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
170
171
172
173
174
175
176
177
178
179
"""Quick and dirty conversion of SAGE worksheets to IPython notebooks.
See www.sagemath.org and ipython.org.
This has been tested with SAGE version 4.2.1 and IPython version 0.13.1.
Usage:
>>> import sage2ipython
>>> sage2ipython.sage2ipy('/path/to/sage/worksheet/html/file','output_file_name.ipynb')
Note that this does NOT operate on the (binary) .sws files.
It uses the html version of the worksheet, which can usually be found in
~/.sage/sage_notebook.sagenb/home/username/number/.
To convert all your SAGE worksheets, do:
>>> import sage2ipython
>>> sage2ipython.convert_all_sage_worksheets('username')
where 'username' is your account name. You may also need to edit the
SAGE notebook account name that occurs in the path in convert_all_sage_worksheets.
General notes/limitations:
- All code blocks are assumed to be Python code blocks.
- Output is simply deleted.
- Everything else is put in Markdown cells.
- Double backslashes are handled properly only if you have the development version of IPython.
Otherwise, you should convert them to quadruple backslashes.
"""
preamble_string = """{
"metadata": {
"name": "pyclaw_tutorial"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
"""
post_string = """
]
}
]
}
"""
cell_strings = {'code' : 'input', 'markdown' : 'source'}
def open_cell(stream,state):
"""Write IPython notebook string to open a cell."""
if not state['first_cell']:
stream.write(',')
stream.write('\n')
stream.write("""
{
"cell_type": "%s",
"metadata": {},
"%s": [\n""" % (state['cell_type'],cell_strings[state['cell_type']]))
state['first_cell'] = False
state['cell_open'] = True
state['first_line'] = True
return state
def close_cell(stream, state):
"""Write IPython notebook string to close a cell."""
if state['cell_type'] is 'code':
stream.write("""
],
"language": "python",
"metadata": {},
"outputs": []
}""")
else:
stream.write("""
]
}""")
state['cell_open'] = False
state['cell_type'] = None
state['is_output'] = False
return state
def escape_characters(line):
"""Escape special characters and remove some HTML."""
line = line.replace("\\","\\\\")
line = line.replace('"','\\"')
line = line.replace('<p>','')
line = line.replace('</p>',' ')
line = line.replace(' ','')
line = line.replace('&','&')
line = line.replace('<','<')
return line
def sage2ipy(path,outname):
"""Converts the .html version of a SAGE worksheet to an IPython notebook.
All we need to know: Code cells begin and end with {{{...}}}.
Everything not in a code cell will be put in a markdown cell.
Any output is simply deleted.
"""
infile = open(path+'worksheet.html','rU')
outfile = open(outname+'.ipynb','w')
outfile.write(preamble_string)
i=-1
state = {}
state['cell_type'] = None
state['cell_open'] = False
state['first_line'] = True
state['first_cell'] = True
state['is_output'] = False
lines=infile.readlines()
while i<len(lines)-1:
i=i+1
line=lines[i]
if line.startswith('}}}'):
state = close_cell(outfile,state)
elif state['is_output']:
continue
elif line.startswith('{{{'):
if state['cell_type'] is not 'code':
if state['cell_open']:
close_cell(outfile,state)
state['cell_type'] = 'code'
open_cell(outfile,state)
else:
raise Exception('Code cell starting inside a code cell')
elif line.startswith('///'):
state['is_output'] = True
continue
elif line.isspace():
continue
else:
if state['cell_type'] is 'code':
if state['first_line']:
state['first_line'] = False
else:
outfile.write(',\n')
line = line.replace('\\','\\\\')
line = line.replace('"','\\"')
outfile.write(' '*13 + '"'+line[:-1]+r'\n'+'"')
elif state['cell_type'] is 'markdown':
outfile.write(',\n')
line = escape_characters(line)
outfile.write(' '*13 + '"'+line[:-1]+'"')
elif state['cell_type'] is None:
state['cell_type'] = 'markdown'
open_cell(outfile,state)
line = escape_characters(line)
outfile.write(' '*13 + '"'+line[:-1]+'"')
outfile.write(post_string)
outfile.close()
def convert_all_sage_worksheets(user):
"""Convert all worksheets belonging to 'user'.
Uses default path to SAGE worksheets on Mac OS X.
Note that worksheet titles won't be preserved,
because they're not stored in the worksheet file
(SAGE must keep an index somewhere else).
"""
import os
import pickle
path = '/Users/'+user+'/.sage/sage_notebook.sagenb/home/admin'
worksheets = [x for x in os.listdir(path) if x.isdigit()]
for worksheet in worksheets:
ws_path = path+'/'+worksheet+'/'
f = open(ws_path+'worksheet_conf.pickle')
ws_data = pickle.load(f)
ws_name = ws_data['name']
sage2ipy(ws_path,ws_name)