{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Simple Animation Demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import time\n",
    "from IPython import display\n",
    "from ipycanvas import canvas\n",
    "from flatland.utils.rendertools import RenderTool\n",
    "from flatland.envs.rail_env import RailEnv\n",
    "from flatland.envs.rail_env import RailEnvActions as rea\n",
    "from flatland.envs.persistence import RailEnvPersister"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pickle failed to load file: complex_scene_2.pkl  trying msgpack (deprecated)...\n",
      "pickle failed to load file: complex_scene_2.pkl  trying msgpack (deprecated)...\n",
      "pickle failed to load file: complex_scene_2.pkl  trying msgpack (deprecated)...\n",
      "This env file has no max_episode_steps (deprecated) - setting to 100\n"
     ]
    }
   ],
   "source": [
    "env, env_dict = RailEnvPersister.load_new(\"complex_scene_2.pkl\", load_from_package=\"env_data.railway\")\n",
    "_ = env.reset()\n",
    "env._max_episode_steps = 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1989b7cb08b74047a8bdc81eca1917a6",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas()"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "oRT = RenderTool(env, gl=\"PILSVG\", jupyter=False, show_debug=True)\n",
    "image_arr = oRT.get_image()\n",
    "oCanvas = canvas.Canvas()\n",
    "oCanvas.put_image_data(image_arr[:,:,0:3])\n",
    "display.display(oCanvas)\n",
    "\n",
    "done={\"__all__\":False}\n",
    "while not done[\"__all__\"]:\n",
    "    actions = {}\n",
    "    for agent_handle, agents in enumerate(env.agents):\n",
    "        actions.update({agent_handle:rea.MOVE_FORWARD})\n",
    "    obs, rew, done, info = env.step(actions)\n",
    "    oRT.render_env(show_observations=False,show_predictions=False)\n",
    "    gIm = oRT.get_image()\n",
    "    oCanvas.put_image_data(gIm[:,:,0:3])\n",
    "    time.sleep(0.1)"
   ]
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {
     "2433ff4d1b324fac968f6109f0a61185": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {
       "_model_module": "@jupyter-widgets/base",
       "_model_module_version": "1.2.0",
       "_model_name": "LayoutModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "1.2.0",
       "_view_name": "LayoutView",
       "align_content": null,
       "align_items": null,
       "align_self": null,
       "border": null,
       "bottom": null,
       "display": null,
       "flex": null,
       "flex_flow": null,
       "grid_area": null,
       "grid_auto_columns": null,
       "grid_auto_flow": null,
       "grid_auto_rows": null,
       "grid_column": null,
       "grid_gap": null,
       "grid_row": null,
       "grid_template_areas": null,
       "grid_template_columns": null,
       "grid_template_rows": null,
       "height": "500px",
       "justify_content": null,
       "justify_items": null,
       "left": null,
       "margin": null,
       "max_height": null,
       "max_width": null,
       "min_height": null,
       "min_width": null,
       "object_fit": null,
       "object_position": null,
       "order": null,
       "overflow": null,
       "overflow_x": null,
       "overflow_y": null,
       "padding": null,
       "right": null,
       "top": null,
       "visibility": null,
       "width": "700px"
      }
     },
     "f2dc1da3e8c74208b9627e97e8630a5f": {
      "model_module": "ipycanvas",
      "model_module_version": "^0.4.1",
      "model_name": "CanvasModel",
      "state": {
       "_dom_classes": [],
       "_model_module": "ipycanvas",
       "_model_module_version": "^0.4.1",
       "_model_name": "CanvasModel",
       "_view_count": null,
       "_view_module": "ipycanvas",
       "_view_module_version": "^0.4.1",
       "_view_name": "CanvasView",
       "image_data": null,
       "layout": "IPY_MODEL_2433ff4d1b324fac968f6109f0a61185",
       "size": [
        700,
        500
       ],
       "sync_image_data": false
      }
     }
    },
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}