forked from maximsch2/SublimeIPythonNotebook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubl_ipy_notebook.py
More file actions
261 lines (194 loc) · 8.13 KB
/
subl_ipy_notebook.py
File metadata and controls
261 lines (194 loc) · 8.13 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
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
# -*- coding: utf-8 -*-
# Copyright (c) 2013, Maxim Grechkin
# This file is licensed under GNU General Public License version 3
# See COPYING for details.
from __future__ import print_function
import sublime
import sublime_plugin
try:
from . import ipy_view, ipy_connection
except:
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")
return settings.get("default_address", "127.0.0.1:7777")
def set_last_used_address(value):
settings = sublime.load_settings("SublimeIPythonNotebook.sublime-settings")
settings.set("default_address", value)
sublime.save_settings("SublimeIPythonNotebook.sublime-settings")
class InbPromptListNotebooksCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.show_input_panel("Notebook host:port : ", get_last_used_address(),
self.on_done, None, None)
def on_done(self, line):
self.window.run_command("inb_list_notebooks", {"baseurl": line})
class InbListNotebooksCommand(sublime_plugin.WindowCommand):
def run(self, baseurl):
self.baseurl = baseurl
nbs = ipy_connection.get_notebooks(baseurl)
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")
self.window.show_quick_panel(lst, self.on_done)
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 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)