<commit_before>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# https://jakevdp.github.io/blog/2013/06/01/ipython-notebook-javascript-python-communication/\n",
    "\n",
    "from IPython.display import HTML\n",
    "\n",
    "input_form = \"\"\"\n",
    "<div style=\"background-color:gainsboro; border:solid black; width:300px; padding:20px;\">\n",
    "Variable Name: <input type=\"text\" id=\"var_name\" value=\"foo\"><br>\n",
    "Variable Value: <input type=\"text\" id=\"var_value\" value=\"bar\"><br>\n",
    "<button onclick=\"set_value()\">Set Value</button>\n",
    "</div>\n",
    "\"\"\"\n",
    "\n",
    "javascript = \"\"\"\n",
    "<script type=\"text/Javascript\">\n",
    "    function set_value(){\n",
    "        var var_name = document.getElementById('var_name').value;\n",
    "        var var_value = document.getElementById('var_value').value;\n",
    "        var command = var_name + \" = '\" + var_value + \"'\";\n",
    "        console.log(\"Executing Command: \" + command);\n",
    "        \n",
    "        var kernel = IPython.notebook.kernel;\n",
    "        kernel.execute(command);\n",
    "    }\n",
    "</script>\n",
    "\"\"\"\n",
    "\n",
    "HTML(input_form + javascript)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from IPython.display import HTML\n",
    "reload(crush)\n",
    "from game import crush\n",
    "\n",
    "javascript_base = \"\"\"\n",
    "<script type=\"text/Javascript\">\n",
    "var kernel = IPython.notebook.kernel;\n",
    "function create_board(board_id, horizontal, vertical, n_colours) {\n",
    "    //var board_id=\"#board_\"+horizontal+\"_\"+vertical;\n",
    "    if($(board_id).children().length==0) {\n",
    "        var trs=[];\n",
    "        for(var v=0; v<vertical; v++) {\n",
    "            var tds=[];\n",
    "            for(var h=0; h<horizontal; h++) {\n",
    "                tds.push(\"<td width='20' height='20' class='i_\"+(horizontal-h-1)+\"_\"+(vertical-v-1)+\"'></td>\");\n",
    "            }\n",
    "            trs.push(\"<tr>\"+tds.join('')+\"</tr>\");\n",
    "        }\n",
    "        $(board_id).append(\"<table border=0>\"+trs.join('')+\"</table>\");\n",
    "        $(board_id).click(function(e) {\n",
    "            console.log(\"HELLO \",e);\n",
    "            \n",
    "            function handle_output(out_type, out) {\n",
    "                console.log(out_type);\n",
    "                console.log(out);\n",
    "                var res = null;\n",
    "                 // if output is a print statement\n",
    "                if(out_type == \"stream\"){\n",
    "                    res = out.data;\n",
    "                }\n",
    "                // if output is a python object\n",
    "                else if(out_type === \"pyout\"){\n",
    "                    res = out.data[\"text/plain\"];\n",
    "                }\n",
    "                // if output is a python error\n",
    "                else if(out_type == \"pyerr\"){\n",
    "                    res = out.ename + \": \" + out.evalue;\n",
    "                }\n",
    "                // if output is something we haven't thought of\n",
    "                else{\n",
    "                    res = \"[out type not implemented]\";   \n",
    "                }\n",
    "                document.getElementById(\"result_output\").value = res;\n",
    "            }\n",
    "            \n",
    "            var cmd1='board, score, n_cols=crush.after_move(board, 0,0, '+n_colours+')';\n",
    "            console.log(cmd1);\n",
    "            \n",
    "            kernel.execute(cmd1, {'output' : function(out_type, out) {\n",
    "                    var cmd2='crush.display_via_javascript_callback(board)';\n",
    "\n",
    "                    kernel.execute(cmd2, {'output' : handle_output}, {silent:false});\n",
    "                    console.log(cmd2);\n",
    "\n",
    "                    //var html_cmd2 = 'HTML('+cmd2+')';\n",
    "                    //kernel.execute(html_cmd2);\n",
    "                    //console.log(html_cmd2);\n",
    "                }}, {silent:false});\n",
    "            \n",
    "        });\n",
    "    }\n",
    "    //$(board_id).append(\"<b>Hello</b>\");\n",
    "    //kernel.execute(command);\n",
    "}\n",
    "function display_board(board_id,a) {\n",
    "    var col=['#fff','#00f','#0f0','#f00','#666','#aaa'];\n",
    "    a.forEach(function(ah,h) {\n",
    "        ah.forEach(function(c,v) {\n",
    "            //console.log(board_id+' .i_'+h+'_'+v);\n",
    "            //$(board_id+' .i_'+h+'_'+v).html(c);\n",
    "            $(board_id+' .i_'+h+'_'+v).css(\"background-color\",col[c]);\n",
    "        });\n",
    "    });\n",
    "}\n",
    "</script>\n",
    "\"\"\"\n",
    "\n",
    "javascript = \"\"\"\n",
    "<div id=\"board_10_14\"></div>\n",
    "<script type=\"text/Javascript\">\n",
    "create_board(\"#board_10_14\",10,14,5);\n",
    "</script>\n",
    "\"\"\"\n",
    "HTML(javascript_base+javascript)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "reload(crush)\n",
    "from game import crush\n",
    "board = crush.new_board(10, 14, n_colours=5)\n",
    "HTML(crush.display_via_javascript_script(\"#board_10_14\", board))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",<commit_msg>Ready to debug on simlim<commit_after>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# https://jakevdp.github.io/blog/2013/06/01/ipython-notebook-javascript-python-communication/\n",
    "\n",
    "from IPython.display import HTML\n",
    "\n",
    "input_form = \"\"\"\n",
    "<div style=\"background-color:gainsboro; border:solid black; width:300px; padding:20px;\">\n",
    "Variable Name: <input type=\"text\" id=\"var_name\" value=\"foo\"><br>\n",
    "Variable Value: <input type=\"text\" id=\"var_value\" value=\"bar\"><br>\n",
    "<button onclick=\"set_value()\">Set Value</button>\n",
    "</div>\n",
    "\"\"\"\n",
    "\n",
    "javascript = \"\"\"\n",
    "<script type=\"text/Javascript\">\n",
    "    function set_value(){\n",
    "        var var_name = document.getElementById('var_name').value;\n",
    "        var var_value = document.getElementById('var_value').value;\n",
    "        var command = var_name + \" = '\" + var_value + \"'\";\n",
    "        console.log(\"Executing Command: \" + command);\n",
    "        \n",
    "        var kernel = IPython.notebook.kernel;\n",
    "        kernel.execute(command);\n",
    "    }\n",
    "</script>\n",
    "\"\"\"\n",
    "\n",
    "HTML(input_form + javascript)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from IPython.display import HTML\n",
    "from game import crush\n",
    "\n",
    "javascript_base = \"\"\"\n",
    "<script type=\"text/Javascript\">\n",
    "var kernel = IPython.notebook.kernel;\n",
    "function create_board(board_id, horizontal, vertical, n_colours) {\n",
    "    //var board_id=\"#board_\"+horizontal+\"_\"+vertical;\n",
    "    if($(board_id).children().length==0) {\n",
    "        console.log(\"Adding table to \"+board_id);\n",
    "        var trs=[];\n",
    "        for(var v=0; v<vertical; v++) {\n",
    "            var tds=[];\n",
    "            for(var h=0; h<horizontal; h++) {\n",
    "                tds.push(\"<td width='20' height='20' class='i_\"+(horizontal-h-1)+\"_\"+(vertical-v-1)+\"'></td>\");\n",
    "            }\n",
    "            trs.push(\"<tr>\"+tds.join('')+\"</tr>\");\n",
    "        }\n",
    "        $(board_id).append(\"<table border=0>\"+trs.join('')+\"</table>\");\n",
    "        $(board_id).click(function(e) {\n",
    "            console.log(\"Cell clicked : \",e);\n",
    "            \n",
    "            function handle_output(out_type, out) {\n",
    "                console.log(out_type);\n",
    "                console.log(out);\n",
    "                var res = null;\n",
    "                 // if output is a print statement\n",
    "                if(out_type == \"stream\"){\n",
    "                    res = out.data;\n",
    "                }\n",
    "                // if output is a python object\n",
    "                else if(out_type === \"pyout\"){\n",
    "                    res = out.data[\"text/plain\"];\n",
    "                }\n",
    "                // if output is a python error\n",
    "                else if(out_type == \"pyerr\"){\n",
    "                    res = out.ename + \": \" + out.evalue;\n",
    "                }\n",
    "                // if output is something we haven't thought of\n",
    "                else{\n",
    "                    res = \"[out type not implemented]\";   \n",
    "                }\n",
    "                document.getElementById(\"result_output\").value = res;\n",
    "            }\n",
    "            \n",
    "            var cmd1='board, score, n_cols=crush.after_move(board, 0,0, '+n_colours+')';\n",
    "            console.log(cmd1);\n",
    "            \n",
    "            kernel.execute(cmd1, {'output' : function(out_type, out) {\n",
    "                    var cmd2='crush.display_via_javascript_callback(board)';\n",
    "\n",
    "                    kernel.execute(cmd2, {'output' : handle_output}, {silent:false});\n",
    "                    console.log(cmd2);\n",
    "\n",
    "                    //var html_cmd2 = 'HTML('+cmd2+')';\n",
    "                    //kernel.execute(html_cmd2);\n",
    "                    //console.log(html_cmd2);\n",
    "                }}, {silent:false});\n",
    "            \n",
    "        });\n",
    "    }\n",
    "    //$(board_id).append(\"<b>Hello</b>\");\n",
    "    //kernel.execute(command);\n",
    "}\n",
    "function display_board(board_id,a) {\n",
    "    var col=['#fff','#00f','#0f0','#f00','#666','#aaa'];\n",
    "    a.forEach(function(ah,h) {\n",
    "        ah.forEach(function(c,v) {\n",
    "            //console.log(board_id+' .i_'+h+'_'+v);\n",
    "            //$(board_id+' .i_'+h+'_'+v).html(c);\n",
    "            $(board_id+' .i_'+h+'_'+v).css(\"background-color\",col[c]);\n",
    "        });\n",
    "    });\n",
    "}\n",
    "</script>\n",
    "\"\"\"\n",
    "\n",
    "javascript = \"\"\"\n",
    "<div id=\"board_10_14\"></div>\n",
    "<script type=\"text/Javascript\">\n",
    "create_board(\"#board_10_14\",10,14,5);\n",
    "</script>\n",
    "\"\"\"\n",
    "HTML(javascript_base+javascript)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "reload(crush)\n",
    "from game import crush\n",
    "board = crush.new_board(10, 14, n_colours=5)\n",
    "HTML(crush.display_via_javascript_script(\"#board_10_14\", board))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
{'fraction_non_alphanumeric': 0.2393061636914786, 'fraction_numerical': 0.012462108454024925, 'mean_word_length': 1.7473976405274114, 'pattern_counts': {'":': 68, '<': 55, '<?xml version=': 0, '>': 51, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3211792', 'n_tokens_mistral': 4362, 'n_tokens_neox': 4203, 'n_words': 635}