{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Grid2Viz demo using Jupyter Dash\n",
    "The `jupyter-dash` package makes it easy to develop Plotly Dash apps from the Jupyter Notebook and JupyterLab.\n",
    "\n",
    "Just replace the standard `dash.Dash` class with the `jupyter_dash.JupyterDash` subclass."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Configuration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pwd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "pkg_root_dir = !pwd\n",
    "pkg_root_dir=pkg_root_dir[0]\n",
    "\n",
    "os.environ[\"GRID2VIZ_ROOT\"] = pkg_root_dir\n",
    "\n",
    "config_path = os.path.join(pkg_root_dir, \"config.ini\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Writing the config-file to launch the app"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "CONFIG_FILE_CONTENT = \"\"\"\n",
    "# This file have been automatically generated, please do not modify it. \n",
    "# You can remove it once done with the application.\n",
    "[DEFAULT]\n",
    "agents_dir={agents_dir}\n",
    "env_dir={env_dir}\n",
    "n_cores={n_cores}\n",
    "# This file will be re generated to each call of \"python -m grid2viz.main\"\n",
    "\"\"\"\n",
    "\n",
    "#short demo\n",
    "#agents_dir = os.path.join(pkg_root_dir,\"grid2viz\", \"data\", \"agents\")\n",
    "#complete demo over some \"warm-up\" scenarios of NeurIPS robustness track competition\n",
    "agents_dir = os.path.join(pkg_root_dir,\"Grid2viz-dataset\", \"NeurIPS_1.2.2_week1\")\n",
    "\n",
    "\n",
    "env_dir = os.path.join(pkg_root_dir,\"grid2viz\", \"data\", \"env_conf\")\n",
    "\n",
    "\n",
    "n_cores = 2#args.n_cores\n",
    "\n",
    "with open(config_path, \"w\") as f:\n",
    "    f.write(CONFIG_FILE_CONTENT.format(agents_dir=agents_dir,\n",
    "                                       env_dir=env_dir,\n",
    "                                       n_cores=n_cores))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Launching\n",
    "The **documentation** of the Grid2viz application can be found here: https://grid2viz.readthedocs.io/en/latest/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!pip install jupyter-dash"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**NOTE**:\n",
    "We run the following code `twice` to actually see the application runnning and get the url where to load the app"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Run it twice sometimes to actually see it runnning and getting the url to go to paly the app\n",
    "from grid2viz.app_jupyter import app as app_jup\n",
    "app_jup.run_server()#mode=\"inline\",debug=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "app_jup.run_server() #you should get the app url as the output of the cell"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#if you want to run it in the notebook\n",
    "#app_jup.run_server(port=8052,mode=\"inline\")#mode=\"inline\",debug=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When running in JupyterLab, with the `jupyterlab-dash` extension, setting `mode=\"jupyterlab\"` will open the app in a tab in JupyterLab.\n",
    "\n",
    "```python\n",
    "app.run_server(mode=\"jupyterlab\")\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "jupytext": {
   "formats": "ipynb,py:percent"
  },
  "kernelspec": {
   "display_name": "grid2viz_env",
   "language": "python",
   "name": "grid2viz_env"
  },
  "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.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}