Skip to content

Instantly share code, notes, and snippets.

@dougnukem
Last active November 3, 2015 22:05
Show Gist options
  • Save dougnukem/bf00d989a7f5e5591336 to your computer and use it in GitHub Desktop.
Save dougnukem/bf00d989a7f5e5591336 to your computer and use it in GitHub Desktop.
calysto_processing_example.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Created file '/Users/ddaniels/dev/src/github.com/dougnukem/notebooks/hello-jupyter/test2.csv'.\n"
]
}
],
"source": [
"%%file test2.csv\n",
"\"State\",\"Population\"\n",
"TX,4708708\n",
"AL,698473\n",
"AZ,6595778\n",
"TX,6595779"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"application/javascript": [
"\n",
" var component = document.getElementById(\"sketch_7\");\n",
" if (component != undefined)\n",
" component.remove();\n",
" component = document.getElementById(\"state_7\");\n",
" if (component != undefined)\n",
" component.remove();\n",
" component = document.getElementById(\"controls_div_7\");\n",
" if (component != undefined)\n",
" component.remove();\n",
" require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n",
" // FIXME: Stop all previously running versions (?)\n",
" var processingInstance = Processing.getInstanceById(\"canvas_7\");\n",
" if (processingInstance != undefined && processingInstance.isRunning())\n",
" processingInstance.noLoop();\n",
" });\n",
" "
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"<div id=\"canvas_div_7\">\n",
" <b>Sketch #7:</b><br/>\n",
" <canvas id=\"canvas_7\"></canvas><br/>\n",
"</div>\n",
"<div id=\"controls_div_7\">\n",
" <button id=\"run_button_7\" onclick=\"startSketch('7');\">\n",
" <i class=\"fa fa-play-circle-o\" style=\"size: 2em;\"></i>\n",
" Run\n",
" </button>\n",
" <button id=\"pause_button_7\" onclick=\"pauseSketch('7');\">\n",
" <i class=\"fa fa-pause\" style=\"size: 2em;\"></i>\n",
" Pause\n",
" </button>\n",
" <button id=\"setup_button_7\" onclick=\"setupSketch('7');\">\n",
" setup()\n",
" </button>\n",
" <button id=\"draw_button_7\" onclick=\"drawSketch('7');\">\n",
" draw()\n",
" </button>\n",
"</div>\n",
"<b>Sketch #7 state:</b> <span id=\"state_7\">Loading...</span><br/>\n",
"<script>\n",
"\n",
"function change_button(button, disable) {\n",
" button.disabled = disable;\n",
" if (disable) {\n",
" button.style.color = \"grey\";\n",
" } else {\n",
" button.style.color = \"black\";\n",
" }\n",
"}\n",
"\n",
"function startSketch(id) {\n",
" switchSketchState(id, true);\n",
"}\n",
"\n",
"function pauseSketch(id) {\n",
" switchSketchState(id, false);\n",
"}\n",
"\n",
"function drawSketch(id) {\n",
" var processingInstance = Processing.getInstanceById(\"canvas_\" + id);\n",
" if (processingInstance != undefined) {\n",
" if (processingInstance.draw != undefined) {\n",
" document.getElementById(\"state_\" + id).innerHTML = \"Drawing...\";\n",
" try {\n",
" processingInstance.redraw();\n",
" document.getElementById(\"state_\" + id).innerHTML = \"Drawing... done! Paused.\";\n",
" document.getElementById(\"state_\" + id).style.color = \"blue\";\n",
" } catch (e) {\n",
" processingInstance.println(e.toString());\n",
" document.getElementById(\"state_\" + id).innerHTML = e.toString();\n",
" document.getElementById(\"state_\" + id).style.color = \"red\";\n",
" }\n",
" } else {\n",
" document.getElementById(\"state_\" + id).innerHTML = \"No drawing() function. Paused.\";\n",
" document.getElementById(\"state_\" + id).style.color = \"blue\";\n",
" }\n",
" } else {\n",
" document.getElementById(\"state_\" + id).innerHTML = \"Error.\";\n",
" document.getElementById(\"state_\" + id).style.color = \"red\";\n",
" }\n",
" change_button(document.getElementById(\"run_button_\" + id), processingInstance.draw == undefined);\n",
" change_button(document.getElementById(\"pause_button_\" + id), true);\n",
" change_button(document.getElementById(\"setup_button_\" + id), processingInstance.setup == undefined);\n",
" change_button(document.getElementById(\"draw_button_\" + id), processingInstance.draw == undefined);\n",
"}\n",
"\n",
"function setupSketch(id) {\n",
" var processingInstance = Processing.getInstanceById(\"canvas_\" + id);\n",
" if (processingInstance != undefined) {\n",
" if (processingInstance.setup != undefined) {\n",
" document.getElementById(\"state_\" + id).innerHTML = \"Setting up...\";\n",
" try {\n",
" processingInstance.setup();\n",
" document.getElementById(\"state_\" + id).innerHTML = \"Setting up... done! Paused.\";\n",
" document.getElementById(\"state_\" + id).style.color = \"blue\";\n",
" } catch (e) {\n",
" processingInstance.println(e.toString());\n",
" document.getElementById(\"state_\" + id).innerHTML = e.toString();\n",
" document.getElementById(\"state_\" + id).style.color = \"red\";\n",
" }\n",
" } else {\n",
" document.getElementById(\"state_\" + id).innerHTML = \"No setup() function. Paused.\";\n",
" document.getElementById(\"state_\" + id).style.color = \"blue\";\n",
" }\n",
" } else {\n",
" document.getElementById(\"state_\" + id).innerHTML = \"Error.\";\n",
" document.getElementById(\"state_\" + id).style.color = \"red\";\n",
" }\n",
" change_button(document.getElementById(\"run_button_\" + id), processingInstance.draw == undefined);\n",
" change_button(document.getElementById(\"pause_button_\" + id), true);\n",
" change_button(document.getElementById(\"setup_button_\" + id), processingInstance.setup == undefined);\n",
" change_button(document.getElementById(\"draw_button_\" + id), processingInstance.draw == undefined);\n",
"}\n",
"\n",
"function switchSketchState(id, on) {\n",
" var processingInstance = Processing.getInstanceById(\"canvas_\" + id);\n",
" if (on) {\n",
" document.getElementById(\"state_\" + id).innerHTML = \"Running...\";\n",
" document.getElementById(\"state_\" + id).style.color = \"green\";\n",
" change_button(document.getElementById(\"run_button_\" + id), true);\n",
" change_button(document.getElementById(\"pause_button_\" + id), processingInstance.draw == undefined);\n",
" change_button(document.getElementById(\"setup_button_\" + id), true);\n",
" change_button(document.getElementById(\"draw_button_\" + id), true);\n",
" processingInstance.loop(); // call Processing loop() function\n",
" } else {\n",
" document.getElementById(\"state_\" + id).innerHTML = \"Paused.\";\n",
" document.getElementById(\"state_\" + id).style.color = \"blue\";\n",
" change_button(document.getElementById(\"run_button_\" + id), processingInstance.draw == undefined);\n",
" change_button(document.getElementById(\"pause_button_\" + id), true);\n",
" change_button(document.getElementById(\"setup_button_\" + id), processingInstance.setup == undefined);\n",
" change_button(document.getElementById(\"draw_button_\" + id), processingInstance.draw == undefined);\n",
" processingInstance.noLoop(); // stop animation, call noLoop()\n",
" }\n",
"}\n",
"\n",
"require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function () {\n",
" var processingCode = '/* @pjs includeTable=\"true\"; */\\n\\nTable table;\\n\\nvoid setup() {\\n table = loadTable(\"test2.csv\", \"header\");\\n println(table.getRowCount() + \" total rows in table\"); \\n println(findMax() + \" max population\");\\n}\\n\\nlong findMax() {\\n int retval = 0;\\n for (TableRow row : table.rows()) {\\n int pop = row.getInt(\"Population\");\\n if (pop > retval)\\n retval = pop;\\n }\\n return retval;\\n}';\n",
" var cc;\n",
" var processingInstance;\n",
" var has_error = false;\n",
" try {\n",
" cc = Processing.compile(processingCode);\n",
" } catch (e) {\n",
" console.log(e);\n",
" cc = Processing.compile(\"println('\" + e.toString() + \"');\");\n",
" document.getElementById(\"state_7\").innerHTML = e.toString();\n",
" document.getElementById(\"state_7\").style.color = \"red\";\n",
" has_error = true;\n",
" }\n",
" if (cc != undefined) {\n",
" try {\n",
" processingInstance = new Processing(\"canvas_7\", cc);\n",
" } catch (e) {\n",
" console.log(e);\n",
" cc = Processing.compile(\"println('\" + e.toString() + \"');\");\n",
" document.getElementById(\"state_7\").innerHTML = e.toString();\n",
" document.getElementById(\"state_7\").style.color = \"red\";\n",
" processingInstance = new Processing(\"canvas_7\", cc);\n",
" has_error = true;\n",
" }\n",
" }\n",
" if (processingInstance != undefined) {\n",
" setTimeout(function () {\n",
" // Canvas:\n",
" if (processingInstance.externals.context === undefined) {\n",
" document.getElementById(\"canvas_div_7\").style.display = \"none\";\n",
" }\n",
" }, 100);\n",
" // Controls:\n",
" if (!(processingInstance.isRunning() && processingInstance.draw != undefined)) {\n",
" document.getElementById(\"controls_div_7\").style.display = \"none\";\n",
" }\n",
" if (processingInstance.draw != undefined) {\n",
" if (!has_error) {\n",
" document.getElementById(\"state_7\").innerHTML = \"Running...\";\n",
" document.getElementById(\"state_7\").style.color = \"green\";\n",
" }\n",
" change_button(document.getElementById(\"run_button_7\"), true);\n",
" change_button(document.getElementById(\"pause_button_7\"), false);\n",
" change_button(document.getElementById(\"setup_button_7\"), true);\n",
" change_button(document.getElementById(\"draw_button_7\"), true);\n",
" } else {\n",
" if (!has_error) {\n",
" document.getElementById(\"state_7\").innerHTML = \"Done.\";\n",
" document.getElementById(\"state_7\").style.color = \"blue\";\n",
" }\n",
" change_button(document.getElementById(\"run_button_7\"), true);\n",
" change_button(document.getElementById(\"pause_button_7\"), true);\n",
" change_button(document.getElementById(\"setup_button_7\"), processingInstance.setup == undefined);\n",
" change_button(document.getElementById(\"draw_button_7\"), true);\n",
" }\n",
" } else {\n",
" document.getElementById(\"canvas_div_7\").style.display = \"none\";\n",
" document.getElementById(\"controls_div_7\").style.display = \"none\";\n",
" if (!has_error) {\n",
" document.getElementById(\"state_7\").innerHTML = \"Error.\";\n",
" document.getElementById(\"state_7\").style.color = \"red\";\n",
" }\n",
" change_button(document.getElementById(\"run_button_7\"), true);\n",
" change_button(document.getElementById(\"pause_button_7\"), true);\n",
" change_button(document.getElementById(\"setup_button_7\"), true);\n",
" change_button(document.getElementById(\"draw_button_7\"), true);\n",
" }\n",
"});\n",
"\n",
"</script>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"/* @pjs includeTable=\"true\"; */\n",
"\n",
"Table table;\n",
"\n",
"void setup() {\n",
" table = loadTable(\"test2.csv\", \"header\");\n",
" println(table.getRowCount() + \" total rows in table\"); \n",
" println(findMax() + \" max population\");\n",
"}\n",
"\n",
"long findMax() {\n",
" int retval = 0;\n",
" for (TableRow row : table.rows()) {\n",
" int pop = row.getInt(\"Population\");\n",
" if (pop > retval)\n",
" retval = pop;\n",
" }\n",
" return retval;\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Calysto Processing",
"language": "java",
"name": "calysto_processing"
},
"language_info": {
"codemirror_mode": {
"name": "text/x-java",
"version": 2
},
"file_extension": ".java",
"mimetype": "text/x-java",
"name": "java"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment