-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsubl_ipy_notebook.py
296 lines (225 loc) · 9.6 KB
/
subl_ipy_notebook.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# -*- coding: utf-8 -*-
# Copyright (c) 2013, Maxim Grechkin
# This file is licensed under GNU General Public License version 3
# See COPYING for details.
import sublime
import sublime_plugin
from . import ipy_view, ipy_connection
manager = ipy_view.manager
class SublimeINListener(sublime_plugin.EventListener):
def on_selection_modified(self, view):
nbview = manager.get_nb_view(view)
if nbview:
nbview.on_sel_modified()
def on_modified(self, view):
nbview = manager.get_nb_view(view)
if nbview:
nbview.on_modified()
def on_close(self, view):
manager.on_close(view)
def get_last_used_address():
settings = sublime.load_settings("SublimeIPythonNotebook.sublime-settings")
lst=settings.get("default_address", [])
return lst if type(lst)==list else [lst]
def set_last_used_address(value):
settings = sublime.load_settings("SublimeIPythonNotebook.sublime-settings")
addresses = get_last_used_address()
if value in addresses:
addresses.pop(addresses.index(value))
settings.set("default_address", [value]+addresses)
sublime.save_settings("SublimeIPythonNotebook.sublime-settings")
class InbPromptListNotebooksCommand(sublime_plugin.WindowCommand):
def run(self):
self.previous_addresses=get_last_used_address()
if len(self.previous_addresses)==0:
self.new_server()
return
self.previous_addresses += ["New Server"]
self.window.show_quick_panel(self.previous_addresses, self.on_done)
def new_server(self):
self.window.show_input_panel("Notebook host:port : ", "http://127.0.0.1:8888",
self.on_done, None, None)
def on_done(self, line):
if line==-1:
return
if type(line)==int:
if line==len(self.previous_addresses)-1:
self.new_server()
else:
self.window.run_command("inb_list_notebooks", {"baseurl": self.previous_addresses[line], "psswd": None})
else:
self.window.run_command("inb_list_notebooks", {"baseurl": line, "psswd": None})
class InbPromptPasswordCommand(sublime_plugin.WindowCommand):
def run(self, baseurl):
self.baseurl=baseurl
self.window.show_input_panel("Password: ", '',
self.on_done, None, None)
def on_done(self, line):
self.window.run_command("inb_list_notebooks", {"baseurl": self.baseurl, 'psswd': line})
class InbListNotebooksCommand(sublime_plugin.WindowCommand):
def run(self, baseurl, psswd):
ipy_connection.install_proxy_opener()
self.baseurl = baseurl
nbs = ipy_connection.get_notebooks(baseurl, psswd)
if nbs=='psswd':
self.window.run_command("inb_prompt_password", {"baseurl": baseurl})
return
if nbs is None:
print("Cannot get a list of notebooks")
return
set_last_used_address(baseurl)
self.nbs = nbs
lst = ["0: Create New Notebook\n"]
for i, nb in enumerate(nbs):
lst.append(str(i+1) + ": " + nb["name"] + "\n")
sublime.set_timeout(lambda: self.window.show_quick_panel(lst, self.on_done), 1)
def on_done(self, picked):
if picked == -1:
return
view = self.window.new_file()
if picked > 0:
manager.create_nb_view(view, self.nbs[picked-1]["notebook_id"], self.baseurl)
else:
new_nb_id = ipy_connection.create_new_notebook(self.baseurl)
if new_nb_id is None:
return
manager.create_nb_view(view, new_nb_id, self.baseurl)
view.run_command("inb_render_notebook")
class SetPagerTextCommand(sublime_plugin.TextCommand):
"""command to set the text in the pop-up pager"""
def run(self, edit, text):
pager_view = self.view.window().get_output_panel("help")
pager_view.insert(edit, 0, text)
self.view.window().run_command("show_panel", {"panel": "output.help"})
class InbRestartKernelCommand(sublime_plugin.TextCommand):
def run(self, edit):
nbview = manager.get_nb_view(self.view)
if nbview:
nbview.restart_kernel()
class InbInterruptKernelCommand(sublime_plugin.TextCommand):
def run(self, edit):
nbview = manager.get_nb_view(self.view)
if nbview and nbview.kernel:
nbview.kernel.interrupt_kernel()
class InbSaveNotebookCommand(sublime_plugin.TextCommand):
def run(self, edit):
nbview = manager.get_nb_view(self.view)
if nbview:
nbview.update_notebook_from_buffer()
nbview.save_notebook()
def description(self):
return "Save IPython notebook"
class InbShutdownKernelCommand(sublime_plugin.TextCommand):
def run(self, edit):
nbview = manager.get_nb_view(self.view)
if nbview and nbview.kernel:
nbview.kernel.shutdown_kernel()
class InbBackspaceCommand(sublime_plugin.TextCommand):
def run(self, edit):
nbview = manager.get_nb_view(self.view)
if nbview:
nbview.on_backspace()
class InbClearBufferCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.set_read_only(False)
self.view.erase(edit, sublime.Region(0, self.view.size()))
class InbRenderNotebookCommand(sublime_plugin.TextCommand):
def run(self, edit):
nbview = manager.get_nb_view(self.view)
if nbview:
self.view.set_read_only(False)
self.view.erase(edit, sublime.Region(0, self.view.size()))
nbview.render_notebook(edit)
class InbInsertOutputCommand(sublime_plugin.TextCommand):
def run(self, edit, cell_index):
nbview = manager.get_nb_view(self.view)
if not nbview:
raise Exception("Failed to get NBView")
cell = nbview.get_cell_by_index(cell_index)
if not cell:
raise Exception("Failed to get cell")
cell.output_result(edit)
class InbRunInNotebookCommand(sublime_plugin.TextCommand):
def run(self, edit, inplace):
nbview = manager.get_nb_view(self.view)
if nbview:
nbview.run_cell(edit, inplace)
class InbDeleteCurrentCellCommand(sublime_plugin.TextCommand):
def run(self, edit):
nbview = manager.get_nb_view(self.view)
if nbview:
nbview.delete_current_cell(edit)
class InbInsertCellAboveCommand(sublime_plugin.TextCommand):
def run(self, edit):
nbview = manager.get_nb_view(self.view)
if nbview:
nbview.insert_cell_above(edit)
class InbInsertCellBelowCommand(sublime_plugin.TextCommand):
def run(self, edit):
nbview = manager.get_nb_view(self.view)
if nbview:
nbview.insert_cell_below(edit)
class InbComplete(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):
nbview = manager.get_nb_view(view)
if nbview:
return nbview.handle_completions(view, prefix, locations)
class InbMoveUpCommand(sublime_plugin.TextCommand):
def run(self, edit):
nbview = manager.get_nb_view(self.view)
if not nbview or not nbview.move_up():
self.view.run_command("move", {"by": "lines", "forward": False})
class InbMoveDownCommand(sublime_plugin.TextCommand):
def run(self, edit):
nbview = manager.get_nb_view(self.view)
if not nbview or not nbview.move_down():
self.view.run_command("move", {"by": "lines", "forward": True})
class InbMoveLeftCommand(sublime_plugin.TextCommand):
def run(self, edit):
nbview = manager.get_nb_view(self.view)
if not nbview or not nbview.move_left():
self.view.run_command("move", {"by": "characters", "forward": False})
class InbMoveRightCommand(sublime_plugin.TextCommand):
def run(self, edit):
nbview = manager.get_nb_view(self.view)
if not nbview or not nbview.move_right():
self.view.run_command("move", {"by": "characters", "forward": True})
class InbOpenAsIpynbCommand(sublime_plugin.WindowCommand):
def run(self):
view = self.window.active_view()
nbview = manager.get_nb_view(view)
if nbview:
s = str(nbview.notebook)
new_view = self.window.new_file()
new_view.run_command('inb_insert_string', {'s': s})
new_view.set_name(nbview.name + ".ipynb")
class InbInsertStringCommand(sublime_plugin.TextCommand):
def run(self, edit, s):
self.view.insert(edit, 0, s)
class InbMoveToCell(sublime_plugin.TextCommand):
def run(self, edit, up):
nbview = manager.get_nb_view(self.view)
if nbview:
nbview.move_to_cell(up)
class InbChangeCellTypeCommand(sublime_plugin.TextCommand):
def run(self, edit, new_type):
nbview = manager.get_nb_view(self.view)
if nbview:
nbview.change_current_cell_type(edit, new_type)
class InbRenameNotebookCommand(sublime_plugin.TextCommand):
def run(self, edit):
nbview = manager.get_nb_view(self.view)
if nbview:
self.nbview = nbview
sublime.active_window().show_input_panel("Notebook name", nbview.get_name(),
self.on_done, None, None)
def on_done(self, line):
self.nbview.set_name(line)
class RewritePromptNumberCommand(sublime_plugin.TextCommand):
def run(self, edit, cell_index):
nbview = manager.get_nb_view(self.view)
if not nbview:
raise Exception("Failed to get NBView")
cell = nbview.get_cell_by_index(cell_index)
if cell:
cell.rewrite_prompt_number(edit)