-
Notifications
You must be signed in to change notification settings - Fork 9
/
flask_terminado.py
63 lines (51 loc) · 1.88 KB
/
flask_terminado.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
from terminado import TermSocket, SingleTermManager
from tornado.ioloop import IOLoop
import tornado.web
import tornado.wsgi
import tornado.web
class IndexHandler(tornado.web.RequestHandler):
def initialize(self, url_prefix):
self.url_prefix = url_prefix
def get(self, *args):
print(args)
self.render('index.html', url_prefix=self.url_prefix)
class Terminal(object):
def __init__(self, app=None, url_prefix='/_flask_terminado'):
assert url_prefix.startswith('/')
url_prefix = url_prefix.rstrip('/')
self.url_prefix = url_prefix
self.app = app
self.tornado_app = None
if app is not None:
self.init_app(app)
def init_app(self, app):
pass
def add_terminal(self, route, command, workdir=None, env=None):
assert route.startswith('/')
if self.app is None:
raise Exception("we don't support init_app yet")
term_manager = SingleTermManager(shell_command=command)
wrapped_app = tornado.wsgi.WSGIContainer(self.app)
handlers = [
("{}/websocket".format(self.url_prefix),
TermSocket, {'term_manager': term_manager}),
(route,
IndexHandler, {'url_prefix': self.url_prefix}),
("{}/(.*)".format(self.url_prefix),
tornado.web.StaticFileHandler, {'path': '.'}),
("/(.*)",
tornado.web.FallbackHandler, {'fallback': wrapped_app}),
]
self.tornado_app = tornado.web.Application(handlers)
def run(self, port=8889, host='127.0.0.1'):
self.tornado_app.listen(port, host)
IOLoop.current().start()
if __name__ == '__main__':
import flask
app = flask.Flask(__name__)
@app.route('/')
def home():
return 'home'
terminal = Terminal(app)
terminal.add_terminal('/bash', ['ipython'])
terminal.run()