forked from adamj9431/notebook_xterm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Adam Johnson
committed
Dec 5, 2017
1 parent
f72d5df
commit 1d4fd7b
Showing
8 changed files
with
319 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
from setuptools import setup | ||
|
||
setup(name='notebook_xterm', | ||
version='0.1.2', | ||
version='0.2.0', | ||
description='A fully-functional terminal emulator in a Jupyter notebook.', | ||
url='http://github.com/adamj9431/notebook_xterm', | ||
author='Adam Johnson', | ||
author_email='[email protected]', | ||
license='MIT', | ||
packages=['notebook_xterm'], | ||
keywords='Jupyter xterm notebook terminal bash shell cli', | ||
python_requires='~=3.3', | ||
install_requires=[ | ||
'future', | ||
], | ||
include_package_data=True, | ||
zip_safe=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,255 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": { | ||
"scrolled": false | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Obtaining file:///Users/adam/Projects/notebook_xterm\n", | ||
"Requirement already satisfied: future in /Users/adam/anaconda3/lib/python3.6/site-packages (from notebook-xterm==0.2.0)\n", | ||
"Installing collected packages: notebook-xterm\n", | ||
" Found existing installation: notebook-xterm 0.1.2\n", | ||
" Uninstalling notebook-xterm-0.1.2:\n", | ||
" Successfully uninstalled notebook-xterm-0.1.2\n", | ||
" Running setup.py develop for notebook-xterm\n", | ||
"Successfully installed notebook-xterm\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"!pip install -e ../ " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"\n", | ||
" <div id=\"notebook_xterm\"></div>\n", | ||
" <script>var MAX_POLL_INTERVAL = 1500;\n", | ||
"var MIN_POLL_INTERVAL = 100;\n", | ||
"var BACKOFF_RATE = 1.8;\n", | ||
"var PY_XTERM_INSTANCE = 'get_ipython().find_magic(\"xterm\").__self__';\n", | ||
"var PY_TERMINAL_SERVER = PY_XTERM_INSTANCE + '.getTerminalServer()';\n", | ||
"function TerminalClient(elem) {\n", | ||
" this.closed = false;\n", | ||
" // require xterm.js\n", | ||
" require.config({\n", | ||
" paths: {\n", | ||
" xterm: '//cdnjs.cloudflare.com/ajax/libs/xterm/2.9.2/xterm.min'\n", | ||
" }\n", | ||
" });\n", | ||
"\n", | ||
" require(['xterm'], function(Terminal) {\n", | ||
" var termArea = this.create_ui(elem);\n", | ||
" this.term = new Terminal({\n", | ||
" rows: 25,\n", | ||
" cols: 100\n", | ||
" });\n", | ||
" this.term.open(termArea[0]);\n", | ||
"\n", | ||
" this.term.on('data', function(data) {\n", | ||
" this.handle_transmit(data);\n", | ||
" }.bind(this));\n", | ||
"\n", | ||
" this.term.on('resize', function() {\n", | ||
" this.handle_resize()\n", | ||
" }.bind(this));\n", | ||
"\n", | ||
" // set title\n", | ||
" this.term.on('title', function(title) {\n", | ||
" this.handle_title(title);\n", | ||
" }.bind(this));\n", | ||
"\n", | ||
" this.termArea.on('remove', function(ev) {\n", | ||
" this.close();\n", | ||
" }.bind(this))\n", | ||
"\n", | ||
" // set the initial size correctly\n", | ||
" this.handle_resize();\n", | ||
"\n", | ||
" // reset the terminal\n", | ||
" this.server_exec(PY_TERMINAL_SERVER + '.transmit(b\"' + btoa('\\r\\nreset\\r\\nclear\\r') + '\")');\n", | ||
"\n", | ||
" // start polling\n", | ||
" this.curPollInterval = MIN_POLL_INTERVAL;\n", | ||
" this.poll_server();\n", | ||
" console.log('Starting notebook_xterm.');\n", | ||
"\n", | ||
" }.bind(this));\n", | ||
"}\n", | ||
"\n", | ||
"TerminalClient.prototype.create_ui = function(elem) {\n", | ||
" var INITIAL_TITLE = 'notebook_xterm'\n", | ||
" // add xterm stylesheet for formatting\n", | ||
" var xtermCssUrl = 'https://cdnjs.cloudflare.com/ajax/libs/xterm/2.9.2/xterm.min.css'\n", | ||
" $('<link/>', {rel: 'stylesheet', href: xtermCssUrl}).appendTo('head');\n", | ||
"\n", | ||
" this.wrap = $('<div>').appendTo(elem);\n", | ||
" this.wrap.css({\n", | ||
" padding: 10,\n", | ||
" margin: 10,\n", | ||
" marginTop: 5,\n", | ||
" backgroundColor: 'black',\n", | ||
" borderRadius: 5\n", | ||
" });\n", | ||
" this.titleBar = $('<div>').appendTo(this.wrap);\n", | ||
" this.titleBar.css({\n", | ||
" color: '#AAA',\n", | ||
" margin: -10,\n", | ||
" marginBottom: 5,\n", | ||
" padding: 10,\n", | ||
" overflow: 'hidden',\n", | ||
" borderBottom: '1px solid #AAA'\n", | ||
" })\n", | ||
" this.titleText = $('<div>').html(INITIAL_TITLE).css({float: 'left'}).appendTo(this.titleBar);\n", | ||
" this.comIndicator = $('<div>').html('·').css({float: 'left', marginLeft: 10}).hide().appendTo(this.titleBar);\n", | ||
" this.termArea = $('<div>').appendTo(this.wrap);\n", | ||
" return this.termArea;\n", | ||
"}\n", | ||
"\n", | ||
"TerminalClient.prototype.update_com_indicator = function() {\n", | ||
" this.comIndicator.show().fadeOut(400);\n", | ||
"}\n", | ||
"\n", | ||
"TerminalClient.prototype.server_exec = function(cmd) {\n", | ||
" if (this.closed) {\n", | ||
" return;\n", | ||
" }\n", | ||
"\n", | ||
" Jupyter.notebook.kernel.execute(cmd, {\n", | ||
" iopub: {\n", | ||
" output: function(data) {\n", | ||
" this.receive_data_callback(data)\n", | ||
" }.bind(this)\n", | ||
" }\n", | ||
" });\n", | ||
" // this.update_com_indicator();\n", | ||
"}\n", | ||
"\n", | ||
"TerminalClient.prototype.poll_server = function() {\n", | ||
" if (this.closed) {\n", | ||
" return;\n", | ||
" }\n", | ||
"\n", | ||
" this.server_exec(PY_TERMINAL_SERVER + '.receive()');\n", | ||
" clearTimeout(this.termPollTimer);\n", | ||
" this.termPollTimer = setTimeout(function() {\n", | ||
" this.poll_server();\n", | ||
" }.bind(this), this.curPollInterval);\n", | ||
" // gradually back off the polling interval\n", | ||
" this.curPollInterval = Math.min(this.curPollInterval*BACKOFF_RATE, MAX_POLL_INTERVAL);\n", | ||
"\n", | ||
" this.check_for_close();\n", | ||
"}\n", | ||
"TerminalClient.prototype.receive_data_callback = function(data) {\n", | ||
" if (this.closed) {\n", | ||
" return;\n", | ||
" }\n", | ||
"\n", | ||
" try {\n", | ||
" var decoded = atob(data.content.text);\n", | ||
" this.term.write(decoded);\n", | ||
" }\n", | ||
" catch(e) {\n", | ||
" var message = \"\\u001b[31;1m~~~ notebook_xterm error ~~~\\u001b[0m\\r\\n\"\n", | ||
" if (data.content.ename && data.content.evalue) {\n", | ||
" message += data.content.ename + \": \" + data.content.evalue + \"\\r\\n\";\n", | ||
" data.content.traceback.map(function(row){\n", | ||
" row = row.replace('\\n', '\\r\\n')\n", | ||
" message += row + '\\r\\n';\n", | ||
" });\n", | ||
" } else {\n", | ||
" message += \"See browser console for more details.\\r\\n\";\n", | ||
" }\n", | ||
" console.log(data.content);\n", | ||
" this.handle_title('error');\n", | ||
" this.term.write(message);\n", | ||
" this.close();\n", | ||
" }\n", | ||
"\n", | ||
"}\n", | ||
"TerminalClient.prototype.handle_transmit = function(data) {\n", | ||
" // we've had interaction, so reset the timer for the next poll\n", | ||
" // to minPollInterval\n", | ||
" this.curPollInterval = MIN_POLL_INTERVAL;\n", | ||
"\n", | ||
" // transmit data to the server, but b64 encode it\n", | ||
" this.server_exec(PY_TERMINAL_SERVER + '.transmit(b\"' + btoa(data) + '\")');\n", | ||
"}\n", | ||
"\n", | ||
"TerminalClient.prototype.handle_resize = function() {\n", | ||
" this.server_exec(PY_TERMINAL_SERVER + '.update_window_size('+ this.term.rows + ', '+ this.term.cols + ')');\n", | ||
"}\n", | ||
"\n", | ||
"TerminalClient.prototype.handle_title = function(title) {\n", | ||
" this.titleText.html(title);\n", | ||
"}\n", | ||
"TerminalClient.prototype.check_for_close = function() {\n", | ||
" if (!this.termArea.length) {\n", | ||
" this.close();\n", | ||
" }\n", | ||
"}\n", | ||
"TerminalClient.prototype.close = function() {\n", | ||
" if (this.closed) {\n", | ||
" return;\n", | ||
" }\n", | ||
" console.log('Closing notebook_xterm.');\n", | ||
" clearTimeout(this.termPollTimer);\n", | ||
" this.server_exec(PY_XTERM_INSTANCE + '.deleteTerminalServer()');\n", | ||
" this.closed = true;\n", | ||
"}\n", | ||
"// create the TerminalClient instance (only once!)\n", | ||
"if (window.terminalClient) {\n", | ||
" delete window.terminalClient;\n", | ||
"}\n", | ||
"window.terminalClient = new TerminalClient($('#notebook_xterm'))\n", | ||
"</script>\n", | ||
" " | ||
], | ||
"text/plain": [ | ||
"<IPython.core.display.HTML object>" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
} | ||
], | ||
"source": [ | ||
"%load_ext notebook_xterm\n", | ||
"%xterm" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 2", | ||
"language": "python", | ||
"name": "python2" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 2 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython2", | ||
"version": "2.7.14" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"!pip install -e ../ " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%load_ext notebook_xterm\n", | ||
"%xterm" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.6.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |