Skip to content

Instantly share code, notes, and snippets.

@rgbkrk
Last active July 27, 2017 03:42
Show Gist options
  • Save rgbkrk/3d18909a847bff680c95d71887aabb80 to your computer and use it in GitHub Desktop.
Save rgbkrk/3d18909a847bff680c95d71887aabb80 to your computer and use it in GitHub Desktop.
Proposing some javascript bits
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"source": [
"## Kernel Interface Proposal \n",
"\n",
"When a user uses a javascript kernel, the last evaluation, is inspected\n",
" \n",
"```js\n",
"> x\n",
"<representation>\n",
"```\n",
"\n",
"`x` can be inspected at first with `util.inspect` as well as some smarts to convert certain kinds of objects to rich data for an `execute_result`.\n",
"\n",
"If a user wants to send something more rich for display, they can import the `display` function\n",
"\n",
"```js\n",
"import { display, updateDisplay } from 'inodejs';\n",
"```\n",
"\n",
"Then the interface should be as js as possible\n",
"\n",
"```\n",
"display(obj, options)\n",
"```\n",
"\nwhere options can include `display_id`, `update`, etc. We'd want a way to set metadata or transient as well"
],
"metadata": {}
},
{
"cell_type": "code",
"source": [
"var util = require('util')"
],
"outputs": [],
"execution_count": 1,
"metadata": {
"collapsed": false,
"outputHidden": false,
"inputHidden": false
}
},
{
"cell_type": "code",
"source": [
"function raw_display(o) {\n",
" // Pretend this is sending it on the iopub socket at display_data\n",
" console.log(o);\n",
"}\n",
"\n",
"function display(obj, opts={}) {\n",
" // opts will accept these keys:\n",
" \n",
" // raw: boolean // obj is expected to be a mimebundle dict\n",
" // metadata: object, raw\n",
" // transient: object, raw\n",
" // display_id: string - if specified, folded into transient\n",
" \n",
" const data = opts.raw ? obj : { 'text/plain': util.inspect(obj) };\n",
" const content = Object.assign({},\n",
" {\n",
" data,\n",
" metadata: opts.metadata || {},\n",
" transient: opts.transient || {}\n",
" }\n",
" );\n",
" \n",
" if(opts.display_id) {\n",
" content.transient = Object.assign({}, content.transient, { display_id: opts.display_id })\n",
" }\n",
" \n",
" return content\n",
"}"
],
"outputs": [],
"execution_count": 2,
"metadata": {
"collapsed": false,
"outputHidden": false,
"inputHidden": false
}
},
{
"cell_type": "code",
"source": [
"display(2)"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": [
"{ data: { 'text/plain': '2' }, metadata: {}, transient: {} }"
]
},
"metadata": {}
}
],
"execution_count": 6,
"metadata": {
"collapsed": false,
"outputHidden": false,
"inputHidden": false
}
},
{
"cell_type": "code",
"source": [
"display(2, {display_id: 235})"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": [
"{ data: { 'text/plain': '2' },\n",
" metadata: {},\n",
" transient: { display_id: 235 } }"
]
},
"metadata": {}
}
],
"execution_count": 3,
"metadata": {
"collapsed": false,
"outputHidden": false,
"inputHidden": false
}
},
{
"cell_type": "code",
"source": [
"display(2, {transient: {model_id: 124}, display_id: 235})"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": [
"{ data: { 'text/plain': '2' },\n",
" metadata: {},\n",
" transient: { model_id: 124, display_id: 235 } }"
]
},
"metadata": {}
}
],
"execution_count": 4,
"metadata": {
"collapsed": false,
"outputHidden": false,
"inputHidden": false
}
},
{
"cell_type": "code",
"source": [
"display(2, {transient: {model_id: 124}})"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": [
"{ data: { 'text/plain': '2' },\n",
" metadata: {},\n",
" transient: { model_id: 124 } }"
]
},
"metadata": {}
}
],
"execution_count": 5,
"metadata": {
"collapsed": false,
"outputHidden": false,
"inputHidden": false
}
},
{
"cell_type": "code",
"source": [],
"outputs": [],
"execution_count": null,
"metadata": {
"collapsed": false,
"outputHidden": false,
"inputHidden": false
}
}
],
"metadata": {
"kernelspec": {
"name": "javascript",
"language": "javascript",
"display_name": "Javascript (Node.js)"
},
"hide_input": true,
"kernel_info": {
"name": "javascript"
},
"language_info": {
"name": "javascript",
"version": "6.9.2",
"mimetype": "application/javascript",
"file_extension": ".js"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment