Commit 9de53030 authored by Andrew Gibiansky's avatar Andrew Gibiansky

setting up for widgets

parent 4e9e6e89
......@@ -50,6 +50,3 @@ instance Show a => IHaskellWidget (Parser a) where
Just (String text) = Map.lookup key dict
result = parse widget "<interactive>" $ unpack text
publisher $ toJSON result
-- We have no resources to close.
close widget value = return ()
......@@ -9,6 +9,44 @@
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import IHaskell.Display\n",
"\n",
"-- My widget type\n",
"data Slider = Slider\n",
"\n",
"instance IHaskellDisplay Slider where\n",
" display Slider = return $ Display []\n",
" \n",
"instance IHaskellWidget Slider where\n",
" targetName _ = \"WidgetModel\"\n",
" open _ s = s undefined >> error \"what\"\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Slider"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data"
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
......@@ -125,13 +163,11 @@
" // Update every key press.\n",
" editor.on(\"keyup\", function() {\n",
" var text = editor.getDoc().getValue();\n",
" console.log(\"Sent\",text); \n",
" comm.send({\"text\": text});\n",
" });\n",
"};\n",
"\n",
"ParsecWidget.prototype.handler = function(msg) {\n",
" console.log(\"Handler\", msg); \n",
" var data = msg.content.data;\n",
" this.hasError = data[\"status\"] == \"error\";\n",
" if (this.hasError) {\n",
......@@ -160,113 +196,6 @@
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"float"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<!-- CodeMirror component -->\n",
"<link rel=\"stylesheet\" href=\"/static/components/codemirror/addon/lint/lint.css\">\n",
"<script src=\"/static/components/codemirror/addon/lint/lint.js\" charset=\"utf-8\"></script>\n",
"\n",
"<!-- Parsec widget -->\n",
"<script>\n",
"// Only load this script once.\n",
"var kernel = IPython.notebook.kernel;\n",
"var initialized = kernel !== undefined && kernel != null;\n",
"if (initialized && window.parsecWidgetRegistered === undefined) {\n",
"\n",
"// Do not load this script again.\n",
"window.parsecWidgetRegistered = true;\n",
"\n",
"// Register the comm target.\n",
"var ParsecWidget = function (comm) {\n",
" this.comm = comm;\n",
" this.comm.on_msg($.proxy(this.handler, this));\n",
"\n",
" // Get the cell that was probably executed.\n",
" // The msg_id:cell mapping will make this possible without guessing.\n",
" this.cell = IPython.notebook.get_cell(IPython.notebook.get_selected_index()-1);\n",
"\n",
" // Store this widget so we can use it from callbacks.\n",
" var widget = this;\n",
"\n",
" // Editor options.\n",
" var options = {\n",
" lineNumbers: true,\n",
" // Show parsec errors as lint errors.\n",
" gutters: [\"CodeMirror-lint-markers\"],\n",
" lintWith: {\n",
" \"getAnnotations\": function(cm, update, opts) {\n",
" var errs = [];\n",
" if (widget.hasError) {\n",
" var col = widget.error[\"col\"];\n",
" var line = widget.error[\"line\"];\n",
" errs = [{\n",
" from: CodeMirror.Pos(line - 1, col - 1),\n",
" to: CodeMirror.Pos(line - 1, col),\n",
" message: widget.error[\"msg\"],\n",
" severity: \"error\"\n",
" }];\n",
" }\n",
" update(cm, errs);\n",
" },\n",
" \"async\": true,\n",
" }\n",
" };\n",
"\n",
" // Create the editor.\n",
" var out = this.cell.output_area.element;\n",
" this.textarea = out.find(\"#parsec-editor\")[0];\n",
" this.output = out.find(\"#parsec-output\")[0];\n",
"\n",
" var editor = CodeMirror.fromTextArea(this.textarea, options);\n",
" var editor = editor;\n",
"\n",
" // Update every key press.\n",
" editor.on(\"keyup\", function() {\n",
" var text = editor.getDoc().getValue();\n",
" console.log(\"Sent\",text); \n",
" comm.send({\"text\": text});\n",
" });\n",
"};\n",
"\n",
"ParsecWidget.prototype.handler = function(msg) {\n",
" console.log(\"Handler\", msg); \n",
" var data = msg.content.data;\n",
" this.hasError = data[\"status\"] == \"error\";\n",
" if (this.hasError) {\n",
" out = data[\"msg\"];\n",
" this.error = data;\n",
" } else {\n",
" out = data[\"result\"];\n",
" }\n",
" // Update viewed output.\n",
" this.output.innerHTML = out;\n",
"};\n",
"\n",
"// Register this widget.\n",
"IPython.notebook.kernel.comm_manager.register_target('parsec', IPython.utils.always_new(ParsecWidget));\n",
"console.log(\"Registering Parsec widget.\");\n",
"}\n",
"</script>\n",
"\n",
"<!-- Parsec widget DOM -->\n",
"<form><textarea id=\"parsec-editor\">Insert parser text here...</textarea></form>\n",
"<pre id=\"parsec-output\"></pre>\n"
],
"metadata": {},
"output_type": "display_data"
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
......
......@@ -263,7 +263,8 @@ evaluate kernelState code output = do
helpStr = evalPager evalOut
-- Output things only if they are non-empty.
unless (noResults result && null helpStr) $
let empty = noResults result && null helpStr && null (evalComms evalOut)
unless empty $
liftIO $ output $ FinalResult result helpStr (evalComms evalOut)
-- Make sure to clear all comms we've started.
......@@ -800,7 +801,7 @@ evalCommand output (Expression expr) state = do
-- Store the fact that we should start this comm.
return evalOut {
evalComms = CommInfo uuid (targetName widget) : evalComms evalOut,
evalComms = CommInfo widget uuid (targetName widget) : evalComms evalOut,
evalState = state'
}
......
......@@ -83,23 +83,29 @@ class IHaskellDisplay a where
-- | Display as an interactive widget.
class IHaskellDisplay a => IHaskellWidget a where
-- Output target name for this widget.
-- | Output target name for this widget.
-- The actual input parameter should be ignored.
targetName :: a -> String
-- | Called when the comm is opened. Allows additional messages to be sent
-- after comm open.
open :: a -- ^ Widget to open a comm port with.
-> Value -- ^ Comm open metadata.
-> (Value -> IO ()) -- ^ Way to respond to the message.
-> IO ()
open _ _ = return ()
-- | Respond to a comm data message.
comm :: a -- ^ Widget which is being communicated with.
-> Value -- ^ Sent data.
-> (Value -> IO ()) -- ^ Way to respond to the message.
-> IO ()
comm _ _ _ = return ()
-- | Close the comm, releasing any resources we might need to.
close :: a -- ^ Widget to close comm port with.
-> Value -- ^ Sent data.
-> IO ()
close _ _ = return ()
data Widget = forall a. IHaskellWidget a => Widget a
deriving Typeable
......@@ -195,7 +201,7 @@ data LintStatus
| LintOff
deriving (Eq, Show)
data CommInfo = CommInfo UUID String
data CommInfo = CommInfo Widget UUID String
-- | Output of evaluation.
data EvaluationResult =
......
......@@ -294,10 +294,17 @@ replyTo interface req@ExecuteRequest{ getCode = code } replyHeader state = do
makeSvgImg base64data = unpack $ "<img src=\"data:image/svg+xml;base64," ++ base64data ++ "\"/>"
startComm :: CommInfo -> IO ()
startComm (CommInfo uuid target) = do
startComm (CommInfo widget uuid target) = do
-- Send the actual comm open.
header <- dupHeader replyHeader CommOpenMessage
send $ CommOpen header target uuid (Object mempty)
-- Send anything else the widget requires.
let communicate value = do
head <- dupHeader replyHeader CommDataMessage
writeChan (iopubChannel interface) $ CommData head uuid value
open widget communicate
publish :: EvaluationResult -> IO ()
publish result = do
let final = case result of
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment