Commit 721c5d1d authored by Andrew Gibiansky's avatar Andrew Gibiansky

comms work now

parent 631bebe1
......@@ -8,7 +8,6 @@ import Data.String.Here
import Text.Parsec.String
import IHaskell.Display
import IHaskell.Display (IHaskellWidget)
instance IHaskellDisplay (Parser a) where
display renderable = do
......@@ -18,33 +17,52 @@ instance IHaskellDisplay (Parser a) where
dom key =
let divId = "text" ++ show key ++ "" in
[i|
<form><textarea id="${divId}">Hello!</textarea></form>
<form><textarea id="parsec-editor">Hello!</textarea></form>
<!--
<script>
// Start the Comm.
var CommManager = IPython.notebook.kernel.comm_manager;
var comm = CommManager.new_comm("parsec", {}, {
iopub : {
output : function () {
console.log("Iopub output init:");
console.log(arguments);
}
}
});
// Create the editor.
var textarea = document.getElementById("${divId}");
var editor = CodeMirror.fromTextArea(textarea);
editor.on("change", function() {
var text = editor.getDoc().getValue();
console.log("New text: " + text);
comm.send({"text": text}, function () {
console.log("Got response!", arguments);
});
});
// Register the comm target.
var ParsecWidget = function (comm) {
this.comm = comm;
this.comm.on_msg($.proxy(this.handler, this));
// get the cell that was probably executed
// msg_id:cell mapping will make this possible without guessing
this.cell = IPython.notebook.get_cell(IPython.notebook.get_selected_index()-1);
this.callbacks = {
iopub : {
output : function () {
console.log("Iopub output", arguments);
}
}
};
// Create the editor.
console.log("Editoring");
var out = this.cell.output_area.element;
var textarea = output_area.find("#parsec-editor")[0];
var editor = CodeMirror.fromTextArea(textarea);
editor.on("change", function() {
var text = editor.getDoc().getValue();
console.log("New text: " + text);
comm.send({"text": text}, function () {
console.log("Got response!", arguments);
});
});
};
ParsecWidget.prototype.handler = function(msg) {
console.log('handle', this, msg, this.cell.output_area);
};
IPython.notebook.kernel.comm_manager.register_target('parsec', IPython.utils.always_new(ParsecWidget));
</script>
-->
|]
instance IHaskellWidget (Parser a) where
targetName _ = "parsec"
open widget value publisher = return ()
comm widget value publisher = return ()
comm widget value publisher = do
DEAL WITH ACTUAL PARSECS
publisher value
close widget value = return ()
......@@ -21,52 +21,64 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"oneOf \"Hello\" :: Parser Char"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x <- display (oneOf \"Hello\" :: Parser Char)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"case x of \n",
" Display [DisplayData MimeHtml dat] -> putStrLn $ unpack dat\n",
" _ -> error \"no\""
"oneOf \"Hello\" :: Parser Char"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<span class='err-msg'>Not in scope: data constructor `MimeHtml'<br/><br/><br/>Not in scope: `unpack'</span>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"Not in scope: data constructor `MimeHtml'\n",
"\n",
" <form><textarea id=\"parsec-editor\">Hello!</textarea></form>\n",
" <!--\n",
" <script>\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",
" // msg_id:cell mapping will make this possible without guessing\n",
" this.cell = IPython.notebook.get_cell(IPython.notebook.get_selected_index()-1);\n",
" this.callbacks = {\n",
" iopub : {\n",
" output : function () {\n",
" console.log(\"Iopub output\", arguments);\n",
" }\n",
" }\n",
" };\n",
"\n",
"Not in scope: `unpack'"
]
" // Create the editor.\n",
" console.log(\"Editoring\");\n",
" var out = this.cell.output_area.element;\n",
" var textarea = output_area.find(\"#parsec-editor\")[0];\n",
" var editor = CodeMirror.fromTextArea(textarea);\n",
" editor.on(\"change\", function() {\n",
" var text = editor.getDoc().getValue();\n",
" console.log(\"New text: \" + text);\n",
" comm.send({\"text\": text}, function () {\n",
" console.log(\"Got response!\", arguments);\n",
" });\n",
" });\n",
" };\n",
"\n",
" ParsecWidget.prototype.handler = function(msg) {\n",
" console.log('handle', this, msg, this.cell.output_area);\n",
" };\n",
"\n",
" IPython.notebook.kernel.comm_manager.register_target('parsec', IPython.utils.always_new(ParsecWidget));\n",
" </script>\n",
" -->\n",
" "
],
"metadata": {},
"output_type": "display_data"
}
],
"prompt_number": 3
......@@ -75,45 +87,90 @@
"cell_type": "code",
"collapsed": false,
"input": [
"import IHaskell.Types"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import ClassyPrelude (unpack)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"oneOf \"Hello\" :: Parser Char"
"import IHaskell.Display\n",
"import Data.String.Here\n",
":set -XQuasiQuotes\n",
"\n",
"html [i|\n",
" <script>\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",
" // Create the editor.\n",
" var out = this.cell.output_area.element;\n",
" var textarea = out.find(\"#parsec-editor\")[0];\n",
" var editor = CodeMirror.fromTextArea(textarea);\n",
" editor.on(\"change\", function() {\n",
" var text = editor.getDoc().getValue();\n",
" comm.send({\"text\": text});\n",
" });\n",
" this.editor = editor;\n",
" };\n",
"\n",
" ParsecWidget.prototype.handler = function(msg) {\n",
" var data = msg.content.data;\n",
" if (data[\"status\"] == \"error\") {\n",
" editor.getDoc().setValue(\"ERROR!!!\");\n",
" }\n",
" };\n",
"\n",
" IPython.notebook.kernel.comm_manager.register_target('parsec', IPython.utils.always_new(ParsecWidget));\n",
" </script>\n",
" |]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<span class='err-msg'>Not in scope: type constructor or class `Parser'<br/><br/><br/>Not in scope: `oneOf'</span>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"Not in scope: type constructor or class `Parser'\n",
"\n",
" <script>\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",
" // msg_id:cell mapping will make this possible without guessing\n",
" this.cell = IPython.notebook.get_cell(IPython.notebook.get_selected_index()-1);\n",
" this.callbacks = {\n",
" iopub : {\n",
" output : function () {\n",
" console.log(\"Iopub output\", arguments);\n",
" }\n",
" }\n",
" };\n",
"\n",
" // Create the editor.\n",
" console.log(\"Editoring\");\n",
" var out = this.cell.output_area.element;\n",
" var textarea = out.find(\"#parsec-editor\")[0];\n",
" var editor = CodeMirror.fromTextArea(textarea);\n",
" editor.on(\"change\", function() {\n",
" var text = editor.getDoc().getValue();\n",
" console.log(\"New text: \" + text);\n",
" comm.send({\"text\": text}, function () {\n",
" console.log(\"Got response!\", arguments);\n",
" });\n",
" });\n",
" };\n",
"\n",
"Not in scope: `oneOf'"
]
" ParsecWidget.prototype.handler = function(msg) {\n",
" console.log('handle', this, msg, this.cell.output_area);\n",
" };\n",
"\n",
" IPython.notebook.kernel.comm_manager.register_target('parsec', IPython.utils.always_new(ParsecWidget));\n",
" </script>\n",
" "
],
"metadata": {},
"output_type": "display_data"
}
],
"prompt_number": 1
......
......@@ -696,7 +696,7 @@ evalCommand output (Expression expr) state = do
-- Check if this is a widget.
let widgetExpr = printf "(IHaskell.Display.Widget (%s))" expr :: String
isWidget <- attempt $ exprType displayExpr
isWidget <- attempt $ exprType widgetExpr
if canRunDisplay
then do
......
......@@ -186,7 +186,7 @@ runKernel profileSrc initInfo = do
if isCommMessage request
then liftIO $ do
oldState <- takeMVar state
let replier = writeChan (shellReplyChannel interface)
let replier = writeChan (iopubChannel interface)
newState <- handleComm replier oldState request replyHeader
putMVar state newState
else do
......@@ -373,11 +373,15 @@ replyTo _ ObjectInfoRequest{objectName = oname} replyHeader state = do
handleComm :: (Message -> IO ()) -> KernelState -> Message -> MessageHeader -> IO KernelState
handleComm replier kernelState req replyHeader = do
putStrLn "Handle comm"
print req
let widgets = openComms kernelState
uuid = commUuid req
dat = commData req
communicate value = do
head <- dupHeader replyHeader CommDataMessage
putStrLn "Sending back data:"
print value
replier $ CommData head uuid value
case lookup uuid widgets of
Nothing -> fail $ "no widget with uuid " ++ show uuid
......@@ -387,6 +391,7 @@ handleComm replier kernelState req replyHeader = do
open widget dat communicate
return kernelState
CommDataMessage -> do
putStrLn "comm data"
comm widget dat communicate
return kernelState
CommCloseMessage -> do
......
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