From 54ccdd46ef04d20755d96068cdff1afe637a1a8a Mon Sep 17 00:00:00 2001 From: hagrid67 <jdhwatson@gmail.com> Date: Sat, 27 Apr 2019 17:54:27 +0100 Subject: [PATCH] update editor notebook --- notebooks/CanvasEditor.ipynb | 253 ++++++++++++++++++++++++++++++++--- 1 file changed, 233 insertions(+), 20 deletions(-) diff --git a/notebooks/CanvasEditor.ipynb b/notebooks/CanvasEditor.ipynb index 5193b72..0157b1f 100644 --- a/notebooks/CanvasEditor.ipynb +++ b/notebooks/CanvasEditor.ipynb @@ -59,7 +59,15 @@ "cell_type": "code", "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "cpu\n" + ] + } + ], "source": [ "import flatland.core.env\n", "from flatland.envs.rail_env import RailEnv, random_rail_generator\n", @@ -214,13 +222,14 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "wid_img.unregister_all()\n", "oEditor = JupEditor(oEnv, wid_img)\n", - "wid_img.register_move(oEditor.event_handler)\n" + "wid_img.register_move(oEditor.event_handler)\n", + "wid_img.register_click(oEditor.on_click)\n" ] }, { @@ -232,21 +241,16 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "wid_drawMode = ipywidgets.RadioButtons(options=[\"Draw\", \"Erase\", \"Origin\", \"Destination\"])\n", "wid_drawMode.observe(oEditor.setDrawMode, names=\"value\")\n", - "wid_refresh = ipywidgets.Button(description=\"Refresh\")\n", - "wid_refresh.on_click(oEditor.redraw_event)\n", - "wid_clear = ipywidgets.Button(description = \"Clear\")\n", - "wid_clear.on_click(oEditor.clear)\n", "wid_debug = ipywidgets.Checkbox(description = \"Debug\")\n", "wid_debug.observe(oEditor.setDebug, names=\"value\")\n", "wid_output = ipywidgets.Output()\n", "oEditor.setOutput(wid_output)\n", - "wid_regen = ipywidgets.Button(description = \"Regenerate\")\n", "wid_filename = ipywidgets.Text(description = \"Filename\")\n", "wid_filename.value = sfEnv\n", "oEditor.setFilename(sfEnv)\n", @@ -255,13 +259,18 @@ "wid_size = ipywidgets.IntSlider(min=5, max=30, step=5, description=\"Regen Size\")\n", "wid_size.observe(oEditor.setRegenSize_event, names=\"value\")\n", "\n", + "prog_steps = ipywidgets.IntProgress(value=0, min=0, max=20, step=1, description=\"Step\")\n", + "#prog_steps.observe(oEditor.)\n", + "\n", "\n", "ldButtons = [\n", " dict(name = \"Refresh\", method = oEditor.redraw_event),\n", " dict(name = \"Clear\", method = oEditor.clear),\n", " dict(name = \"Regenerate\", method = oEditor.regenerate_event),\n", " dict(name = \"Load\", method = oEditor.load),\n", - " dict(name = \"Save\", method = oEditor.save)\n", + " dict(name = \"Save\", method = oEditor.save),\n", + " dict(name = \"Step\", method = oEditor.step_event),\n", + " dict(name = \"Run Steps\", method = oEditor.start_run_event),\n", "]\n", "\n", "lwid_buttons = []\n", @@ -285,7 +294,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 33, "metadata": { "scrolled": false }, @@ -293,7 +302,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "4c71c3622edc4d1b8d28916b65ae12fc", + "model_id": "ab659e423d1c4c6382c1fe1ea21397a6", "version_major": 2, "version_minor": 0 }, @@ -315,14 +324,34 @@ { "cell_type": "code", "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "oEditor.env.number_of_agents" + ] + }, + { + "cell_type": "code", + "execution_count": 16, "metadata": { - "scrolled": false + "scrolled": true }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "58e49268613640a581df588132692c4b", + "model_id": "f7b15f5bac5d40389736f3c35395d655", "version_major": 2, "version_minor": 0 }, @@ -338,6 +367,38 @@ "wid_output" ] }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "import threading\n", + "def bgUpdate(editor):\n", + " for i in range(100):\n", + " editor.step_event()\n", + " time.sleep(0.2)\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "thread = threading.Thread(target=bgUpdate, args = (oEditor,))" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "#thread.start()" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -347,7 +408,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 20, "metadata": {}, "outputs": [], "source": [ @@ -363,7 +424,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 21, "metadata": {}, "outputs": [], "source": [ @@ -379,7 +440,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 22, "metadata": {}, "outputs": [], "source": [ @@ -389,7 +450,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 23, "metadata": {}, "outputs": [], "source": [ @@ -398,10 +459,162 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "3ccdfa67797d4cb087e0518a38cab9ea", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "IntText(value=0)" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "ipywidgets.IntText(0)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "from examples.play_model import Player" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "oP = Player(oEnv)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [], + "source": [ + "oP.step()" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [], + "source": [ + "import asyncio\n", + "def wait_for_change(widget, value):\n", + " future = asyncio.Future()\n", + " def getvalue(change):\n", + " # make the new value available\n", + " future.set_result(change.new)\n", + " widget.unobserve(getvalue, value)\n", + " widget.observe(getvalue, value)\n", + " return future" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "4c8120f49edf43e0b0799f79095e3500", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "IntSlider(value=0)" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "slider = ipywidgets.IntSlider()\n", + "\n", + "async def f():\n", + " for i in range(10):\n", + " print('did work %s'%i)\n", + " x = await wait_for_change(slider, 'value')\n", + " print('async function continued with value %s'%x)\n", + "asyncio.ensure_future(f())\n", + "\n", + "slider" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "be67a7c2ae2e426198fbbf20e4218d2f", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "FloatProgress(value=0.0, max=1.0)" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "\n", + "import threading\n", + "from IPython.display import display\n", + "import time\n", + "progress = ipywidgets.FloatProgress(value=0.0, min=0.0, max=1.0)\n", + "\n", + "def work(progress):\n", + " total = 100\n", + " for i in range(total):\n", + " time.sleep(0.2)\n", + " progress.value = float(i+1)/total\n", + "\n", + "thread = threading.Thread(target=work, args=(progress,))\n", + "display(progress)\n", + "thread.start()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[2,3] == (2,3)" + ] } ], "metadata": { -- GitLab