IHaskell/notebooks/Test.ipynb
2014-05-17 22:29:39 -07:00

223 lines
7.4 KiB
Plaintext

{
"metadata": {
"celltoolbar": "Hiding",
"language": "haskell",
"name": "",
"signature": "sha256:b630a2733d4a58680ceb3b868982dc891d570e9ccad5ce31a29d5e1c110962b7"
},
"nbformat": 3,
"nbformat_minor": 0,
"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": [
":ext NoImplicitPrelude\n",
"4 + 4"
],
"language": "python",
"metadata": {
"hidden": false
},
"outputs": [
{
"html": [
"<span class='err-msg'>Not in scope: `+'<br/>Perhaps you meant `IHaskellPrelude.+' (imported from Prelude)</span>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"Not in scope: `+'\n",
"Perhaps you meant `IHaskellPrelude.+' (imported from Prelude)"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import Text.Parsec\n",
"import Text.Parsec.String\n",
"import Text.Parsec.Prim\n",
"import Text.Parsec.Char\n",
"\n",
"data List = List [Float] deriving Show\n",
"\n",
"let -- Parse a nonempty int list like [1, 2, 3]\n",
" parser :: Parser List\n",
" parser = do\n",
" char '['\n",
" values <- option [] $ many1 (try float <|> int)\n",
" char ']'\n",
" return $ List values\n",
" \n",
" -- Parse an element of an int list, like \"3, \"\n",
" int :: Parser Float\n",
" int = do\n",
" value <- many1 $ oneOf \"0123456789\"\n",
" optional $ char ','\n",
" whitespace\n",
" return (fromIntegral (read value :: Int) :: Float)\n",
" \n",
" float :: Parser Float\n",
" float = do\n",
" value <- many1 $ oneOf \"0123456789\"\n",
" char '.'\n",
" after <- many1 $ oneOf \"0123456789\"\n",
" optional $ char ','\n",
" whitespace\n",
" return (read (value ++ \".\" ++ after) :: Float)\n",
" \n",
" -- Parse any whitespace\n",
" whitespace = many $ oneOf \" \\t\"\n",
" \n",
"parser"
],
"language": "python",
"metadata": {
"hidden": false
},
"outputs": [
{
"javascript": [
"// Only load this script once.\n",
"var kernel = IPython.notebook.kernel;\n",
"var initialized = kernel !== undefined && kernel != null;\n",
"console.log(\"Initialized\", initialized);\n",
"if (initialized && window.parsecWidgetRegistered === undefined) {\n",
"\n",
"// Do not load this script again.\n",
"window.parsecWidgetRegistered = true;\n",
"\n",
"var parsecWidgetCounter = 0;\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",
" // Give the elements a different name.\n",
" this.textarea.id += parsecWidgetCounter;\n",
" this.output.id += parsecWidgetCounter;\n",
" parsecWidgetCounter++;\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",
" comm.send({\"text\": text});\n",
" });\n",
"};\n",
"\n",
"ParsecWidget.prototype.handler = function(msg) {\n",
" var data = msg.content.data;\n",
" this.hasError = data[\"status\"] == \"error\";\n",
" console.log('handler', msg);\n",
" if (this.hasError) {\n",
" this.output.innerHTML = data[\"msg\"];\n",
" this.error = data;\n",
" } else {\n",
" this.output.innerHTML = data[\"result\"];\n",
" }\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"
],
"metadata": {},
"output_type": "display_data"
},
{
"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 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": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}