Skip to content

Commit d38992b

Browse files
authored
Merge pull request #31 from afshin/terminal-editor
Add terminal and editor handlers
2 parents cff037e + 9352a3c commit d38992b

File tree

5 files changed

+101
-2
lines changed

5 files changed

+101
-2
lines changed

nbclassic/edit/__init__.py

Whitespace-only changes.

nbclassic/edit/handlers.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#encoding: utf-8
2+
"""Tornado handlers for the terminal emulator."""
3+
4+
# Copyright (c) Jupyter Development Team.
5+
# Distributed under the terms of the Modified BSD License.
6+
7+
from tornado import web
8+
from jupyter_server.base.handlers import JupyterHandler, path_regex
9+
from jupyter_server.utils import url_escape
10+
from jupyter_server.extension.handler import (
11+
ExtensionHandlerMixin,
12+
ExtensionHandlerJinjaMixin
13+
)
14+
15+
class EditorHandler(ExtensionHandlerJinjaMixin, ExtensionHandlerMixin, JupyterHandler):
16+
"""Render the text editor interface."""
17+
18+
@web.authenticated
19+
def get(self, path):
20+
path = path.strip('/')
21+
if not self.contents_manager.file_exists(path):
22+
raise web.HTTPError(404, u'File does not exist: %s' % path)
23+
24+
basename = path.rsplit('/', 1)[-1]
25+
self.write(self.render_template('edit.html',
26+
file_path=url_escape(path),
27+
basename=basename,
28+
page_title=basename + " (editing)",
29+
)
30+
)
31+
32+
default_handlers = [
33+
(r"/edit%s" % path_regex, EditorHandler),
34+
]

nbclassic/notebookapp.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,14 @@
4343
from jupyter_server.log import log_request
4444
from jupyter_server.transutils import _
4545
from jupyter_server.serverapp import (
46+
ServerApp,
4647
random_ports,
4748
load_handlers
4849
)
50+
from jupyter_server.utils import url_path_join as ujoin
51+
52+
from .terminal.handlers import TerminalHandler, TermSocket
53+
4954

5055
#-----------------------------------------------------------------------------
5156
# Module globals
@@ -88,13 +93,13 @@
8893
'ip': 'ServerApp.ip',
8994
'port': 'ServerApp.port',
9095
'port-retries': 'ServerApp.port_retries',
91-
'transport': 'ServerApp.KernelManager.transport',
96+
#'transport': 'KernelManager.transport',
9297
'keyfile': 'ServerApp.keyfile',
9398
'certfile': 'ServerApp.certfile',
9499
'client-ca': 'ServerApp.client_ca',
95100
'notebook-dir': 'ServerApp.notebook_dir',
96101
'browser': 'ServerApp.browser',
97-
'gateway-url': 'ServerApp.GatewayClient.url',
102+
#'gateway-url': 'GatewayClient.url',
98103
})
99104

100105
#-----------------------------------------------------------------------------
@@ -209,6 +214,21 @@ def initialize_handlers(self):
209214

210215
handlers.extend(load_handlers('nbclassic.tree.handlers'))
211216
handlers.extend(load_handlers('nbclassic.notebook.handlers'))
217+
handlers.extend(load_handlers('nbclassic.edit.handlers'))
218+
219+
# Add terminal handlers
220+
try:
221+
term_mgr = self.serverapp.web_app.settings['terminal_manager']
222+
except KeyError:
223+
pass # Terminals not enabled
224+
else:
225+
handlers.append(
226+
(r"/terminals/(\w+)", TerminalHandler)
227+
)
228+
handlers.append(
229+
(r"/terminals/websocket/(\w+)", TermSocket,
230+
{'term_manager': term_mgr})
231+
)
212232

213233
handlers.append(
214234
(r"/nbextensions/(.*)", FileFindHandler, {

nbclassic/terminal/__init__.py

Whitespace-only changes.

nbclassic/terminal/handlers.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#encoding: utf-8
2+
"""Tornado handlers for the terminal emulator."""
3+
4+
# Copyright (c) Jupyter Development Team.
5+
# Distributed under the terms of the Modified BSD License.
6+
7+
from tornado import web
8+
import terminado
9+
from jupyter_server._tz import utcnow
10+
from jupyter_server.base.handlers import JupyterHandler
11+
from jupyter_server.base.zmqhandlers import WebSocketMixin
12+
from jupyter_server.extension.handler import (
13+
ExtensionHandlerMixin,
14+
ExtensionHandlerJinjaMixin
15+
)
16+
17+
class TerminalHandler(ExtensionHandlerJinjaMixin, ExtensionHandlerMixin, JupyterHandler):
18+
"""Render the terminal interface."""
19+
@web.authenticated
20+
def get(self, term_name):
21+
self.write(self.render_template('terminal.html',
22+
ws_path="terminals/websocket/%s" % term_name))
23+
24+
25+
class TermSocket(WebSocketMixin, JupyterHandler, terminado.TermSocket):
26+
27+
def origin_check(self):
28+
"""Terminado adds redundant origin_check
29+
30+
Tornado already calls check_origin, so don't do anything here.
31+
"""
32+
return True
33+
34+
def get(self, *args, **kwargs):
35+
if not self.get_current_user():
36+
raise web.HTTPError(403)
37+
return super(TermSocket, self).get(*args, **kwargs)
38+
39+
def on_message(self, message):
40+
super(TermSocket, self).on_message(message)
41+
self.application.settings['terminal_last_activity'] = utcnow()
42+
43+
def write_message(self, message, binary=False):
44+
super(TermSocket, self).write_message(message, binary=binary)
45+
self.application.settings['terminal_last_activity'] = utcnow()

0 commit comments

Comments
 (0)