notebook/IPython/html/terminal/api_handlers.py

45 lines
1.3 KiB
Python
Raw Normal View History

2014-10-04 07:20:13 +08:00
import json
2015-01-17 09:08:22 +08:00
from tornado import web, gen
2014-10-04 07:20:13 +08:00
from ..base.handlers import IPythonHandler, json_errors
from ..utils import url_path_join
2014-10-04 07:20:13 +08:00
class TerminalRootHandler(IPythonHandler):
@web.authenticated
@json_errors
def get(self):
tm = self.terminal_manager
2014-10-04 07:20:13 +08:00
terms = [{'name': name} for name in tm.terminals]
self.finish(json.dumps(terms))
@web.authenticated
@json_errors
def post(self):
"""POST /terminals creates a new terminal and redirects to it"""
name, _ = self.terminal_manager.new_named_terminal()
self.finish(json.dumps({'name': name}))
2014-10-04 07:20:13 +08:00
class TerminalHandler(IPythonHandler):
SUPPORTED_METHODS = ('GET', 'DELETE')
@web.authenticated
@json_errors
def get(self, name):
tm = self.terminal_manager
2014-10-04 07:20:13 +08:00
if name in tm.terminals:
self.finish(json.dumps({'name': name}))
else:
raise web.HTTPError(404, "Terminal not found: %r" % name)
@web.authenticated
@json_errors
2015-01-17 09:08:22 +08:00
@gen.coroutine
2014-10-04 07:20:13 +08:00
def delete(self, name):
tm = self.terminal_manager
2014-10-04 07:20:13 +08:00
if name in tm.terminals:
2015-01-17 09:08:22 +08:00
yield tm.terminate(name, force=True)
2014-10-04 07:20:13 +08:00
self.set_status(204)
self.finish()
else:
2015-01-17 09:08:22 +08:00
raise web.HTTPError(404, "Terminal not found: %r" % name)