Multiple terminals and conditional initialisation

This commit is contained in:
Thomas Kluyver 2014-10-03 12:37:26 -07:00
parent d4676bf2ad
commit 9c0084e615
5 changed files with 38 additions and 15 deletions

View File

@ -190,7 +190,6 @@ class NotebookWebApplication(web.Application):
handlers.extend(load_handlers('notebook.handlers'))
handlers.extend(load_handlers('nbconvert.handlers'))
handlers.extend(load_handlers('kernelspecs.handlers'))
handlers.extend(load_handlers('terminal.handlers'))
handlers.extend(load_handlers('services.kernels.handlers'))
handlers.extend(load_handlers('services.contents.handlers'))
handlers.extend(load_handlers('services.clusters.handlers'))
@ -762,6 +761,15 @@ class NotebookApp(BaseIPythonApplication):
proto = 'https' if self.certfile else 'http'
return "%s://%s:%i%s" % (proto, ip, self.port, self.base_url)
def init_terminals(self):
try:
from .terminal import initialize
initialize(self.web_app)
self.web_app.terminals_available = True
except ImportError as e:
self.log.info("Terminals not available (error was %s)", e)
self.web_app.terminals_available = False
def init_signal(self):
if not sys.platform.startswith('win'):
signal.signal(signal.SIGINT, self._handle_sigint)
@ -841,6 +849,7 @@ class NotebookApp(BaseIPythonApplication):
self.init_configurables()
self.init_components()
self.init_webapp()
self.init_terminals()
self.init_signal()
def cleanup_kernels(self):

View File

@ -22,8 +22,9 @@ require([
$("#dummy-screen").hide();
var base_url = utils.get_body_data('baseUrl');
var ws_path = utils.get_body_data('wsPath');
var ws_url = location.protocol.replace('http', 'ws') + "//" + location.host
+ base_url + "terminal/websocket";
+ base_url + ws_path;
var header = $("#header")[0]
function calculate_size() {

View File

@ -10,6 +10,7 @@
{% block params %}
data-base-url="{{base_url}}"
data-ws-path="{{ws_path}}"
{% endblock %}

View File

@ -0,0 +1,14 @@
import os
from terminado import NamedTermManager
from .handlers import TerminalHandler, NewTerminalHandler, TermSocket
def initialize(webapp):
shell = os.environ.get('SHELL', 'sh')
webapp.terminal_manager = NamedTermManager(shell_command=[shell])
handlers = [
(r"/terminals/new", NewTerminalHandler),
(r"/terminals/(\w+)", TerminalHandler),
(r"/terminals/websocket/(\w+)", TermSocket,
{'term_manager': webapp.terminal_manager}),
]
webapp.add_handlers(".*$", handlers)

View File

@ -8,19 +8,17 @@ import terminado
from ..base.handlers import IPythonHandler
class TerminalHandler(IPythonHandler):
"""Render the tree view, listing notebooks, clusters, etc."""
"""Render the terminal interface."""
@web.authenticated
def get(self, path='', name=None):
self.write(self.render_template('terminal.html'))
def get(self, term_name):
self.write(self.render_template('terminal.html',
ws_path="terminals/websocket/%s" % term_name))
class NewTerminalHandler(IPythonHandler):
"""Redirect to a new terminal."""
@web.authenticated
def get(self):
name, _ = self.application.terminal_manager.new_named_terminal()
self.redirect("/terminals/%s" % name, permanent=False)
#-----------------------------------------------------------------------------
# URL to handler mappings
#-----------------------------------------------------------------------------
default_handlers = [
(r"/terminal", TerminalHandler),
(r"/terminal/websocket", terminado.TermSocket,
{'term_manager': terminado.SingleTermManager(shell_command=['bash'])}),
]
TermSocket = terminado.TermSocket