From 75e2d507bd52748d3d02f2ac982ada065752556c Mon Sep 17 00:00:00 2001 From: Robin Wilson Date: Fri, 24 Apr 2020 17:06:59 +0100 Subject: [PATCH 1/2] Added support for initial command, so it can be run as `%xterm ls` to open an xterm and run ls --- notebook_xterm/terminalclient.js | 2 ++ notebook_xterm/terminalserver.py | 3 +++ notebook_xterm/xterm.py | 9 ++++++++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/notebook_xterm/terminalclient.js b/notebook_xterm/terminalclient.js index ca13742..d542522 100644 --- a/notebook_xterm/terminalclient.js +++ b/notebook_xterm/terminalclient.js @@ -48,6 +48,8 @@ function TerminalClient(elem) { this.poll_server(); console.log('Starting notebook_xterm.'); + this.server_exec(PY_TERMINAL_SERVER + '.initial_transmit()'); + }.bind(this)); } diff --git a/notebook_xterm/terminalserver.py b/notebook_xterm/terminalserver.py index d61c274..64e11e7 100644 --- a/notebook_xterm/terminalserver.py +++ b/notebook_xterm/terminalserver.py @@ -26,6 +26,9 @@ def __init__(self): flags = fcntl(self.file, F_GETFL) fcntl(self.file, F_SETFL, flags | os.O_NONBLOCK) + def initial_transmit(self): + self.transmit(base64.b64encode(self.initial_command)) + def transmit(self,data): # data in the "channel" is b64 encoded so that control characters # don't get lost diff --git a/notebook_xterm/xterm.py b/notebook_xterm/xterm.py index d7153a7..75173ba 100644 --- a/notebook_xterm/xterm.py +++ b/notebook_xterm/xterm.py @@ -5,6 +5,8 @@ from .terminalserver import TerminalServer from IPython.core.display import display, HTML from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic) +import time +from base64 import b64encode JS_FILE_NAME = 'terminalclient.js' @@ -19,9 +21,14 @@ def xterm(self, line): markup = """
- + """.format(terminalClient_js) display(HTML(markup)) + ts = self.getTerminalServer() + ts.initial_command = bytes(line, encoding="utf-8") + b"\r" + + return self.getTerminalServer() + #ts.transmit(b64encode(b"ls")) def getTerminalServer(self): try: From 4fddd434a4d045572bb32d4cdb4b3a0266c113fa Mon Sep 17 00:00:00 2001 From: Robin Wilson Date: Tue, 28 Apr 2020 19:52:51 +0100 Subject: [PATCH 2/2] Updated docs with new `%xterm ` example --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index cb02ffe..bf57c81 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,11 @@ To display a terminal, type the [magic function](http://ipython.readthedocs.io/e %xterm ``` +To display a terminal and immediately run a command in it, just place the command after the `%xterm` call: +``` +%xterm ls +``` + ## Tested Environments + [IBM Data Science Experience](https://datascience.ibm.com/) + Jupyter 4.3.0