Skip to content

Commit 36b8760

Browse files
committed
added chanmges from adamj9431#7
1 parent bac3cb7 commit 36b8760

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

notebook_xterm/terminalclient.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ function TerminalClient(elem) {
1313
});
1414

1515
require(['xterm'], function(Terminal) {
16+
this.div_id = elem.attr('id')
1617
var termArea = this.create_ui(elem);
1718
this.term = new Terminal({
1819
rows: 25,
@@ -76,6 +77,7 @@ TerminalClient.prototype.create_ui = function(elem) {
7677
})
7778
this.titleText = $('<div>').html(INITIAL_TITLE).css({float: 'left'}).appendTo(this.titleBar);
7879
this.comIndicator = $('<div>').html('&middot;').css({float: 'left', marginLeft: 10}).hide().appendTo(this.titleBar);
80+
this.close_button = $('<div>').html('<a onclick="window.terminalClient.close()">close</a>').css({float: 'right'}).appendTo(this.titleBar);
7981
this.termArea = $('<div>').appendTo(this.wrap);
8082
return this.termArea;
8183
}
@@ -166,13 +168,14 @@ TerminalClient.prototype.close = function() {
166168
if (this.closed) {
167169
return;
168170
}
171+
this.closed = true;
169172
console.log('Closing notebook_xterm.');
170173
clearTimeout(this.termPollTimer);
171174
this.server_exec(PY_XTERM_INSTANCE + '.deleteTerminalServer()');
172-
this.closed = true;
175+
$("#" + this.div_id).remove()
173176
}
174177
// create the TerminalClient instance (only once!)
175178
if (window.terminalClient) {
179+
window.terminalClient.close()
176180
delete window.terminalClient;
177-
}
178-
window.terminalClient = new TerminalClient($('#notebook_xterm'))
181+
}

notebook_xterm/xterm.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from .terminalserver import TerminalServer
66
from IPython.core.display import display, HTML
77
from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic)
8+
from base64 import b64encode
9+
from uuid import uuid4
810

911
JS_FILE_NAME = 'terminalclient.js'
1012

@@ -17,11 +19,18 @@ def xterm(self, line):
1719
with open(jsPath) as f:
1820
terminalClient_js = f.read()
1921

20-
markup = """
21-
<div id="notebook_xterm"></div>
22-
<script>{0}</script>
23-
""".format(terminalClient_js)
22+
unique_id = str(uuid4())
23+
markup = f"""
24+
<div id="notebook_xterm_{unique_id}"></div>
25+
<script id="notebook_script">{terminalClient_js}
26+
27+
window.terminalClient = new TerminalClient($('#notebook_xterm_{unique_id}'))
28+
</script>
29+
"""
2430
display(HTML(markup))
31+
ts = self.getTerminalServer()
32+
33+
return self.getTerminalServer()
2534

2635
def getTerminalServer(self):
2736
try:
@@ -33,4 +42,4 @@ def getTerminalServer(self):
3342
def deleteTerminalServer(self):
3443
if self.ts:
3544
self.ts.close()
36-
del self.ts
45+
del self.ts

0 commit comments

Comments
 (0)