{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d6190bb3-da16-4cc8-b4ab-50ef32de2b96",
   "metadata": {},
   "source": [
    "# Editor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cd99ec06-e44c-4dc7-a0a0-f34caf7f2366",
   "metadata": {},
   "outputs": [],
   "source": [
    "import editor.prototypes\n",
    "import editor.editor_widgets\n",
    "from editor.editor import Editor\n",
    "import remi.gui as gui\n",
    "from remi import start, App\n",
    "from threading import Timer, Thread\n",
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c838e89f-cd30-44d4-a984-b64f018c695d",
   "metadata": {},
   "outputs": [],
   "source": [
    "remiport = 8090"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f3928282-2156-4b4a-ba0b-76b72493f85a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from threading import Timer, Thread"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a9a7d0f5-d32f-4555-9be0-f7077d446bc7",
   "metadata": {},
   "outputs": [],
   "source": [
    "class MyApp(Editor):\n",
    "    def __init__(self, *args):\n",
    "        super(MyApp, self).__init__(*args,)\n",
    "            \n",
    "    def _net_interface_ip(self):\n",
    "        ip = super()._net_interface_ip()\n",
    "        return ip + f\"/proxy/{remiport}\"\n",
    "    \n",
    "    def _overload(self, data, **kwargs):\n",
    "        if \"filename\" in kwargs:\n",
    "            filename = kwargs['filename']\n",
    "        else:\n",
    "            return data\n",
    "        paths = self.all_paths()\n",
    "        for pattern in paths.keys():\n",
    "            if ( filename.endswith(\".css\") or filename.endswith(\".html\") or filename.endswith(\".js\") or filename.endswith(\"internal\") ):\n",
    "                if type(data) == str:\n",
    "                    data = re.sub(f\"/{pattern}:\", f\"/proxy/{remiport}/{pattern}:\", data)\n",
    "                else:\n",
    "                    data = re.sub(f\"/{pattern}:\", f\"/proxy/{remiport}/{pattern}:\", data.decode()).encode()\n",
    "        return data\n",
    "\n",
    "    def _process_all(self, func, **kwargs):\n",
    "        print(kwargs)\n",
    "        kwargs.update({\"overload\": self._overload})\n",
    "        super()._process_all(func, **kwargs)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "71c3725e-9235-4682-b304-a0e580ccc8df",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "remi.server      INFO     Started httpserver http://127.0.0.1:8090/\n"
     ]
    }
   ],
   "source": [
    "myRemi = Thread(target=start, \n",
    "                         args=(MyApp,),\n",
    "                         kwargs={'address':'127.0.0.1', \n",
    "                                 'port':remiport, \n",
    "                                 'multiple_instance':True,\n",
    "                                 'enable_file_cache':True, \n",
    "                                 'update_interval':0.5, \n",
    "                                 'start_browser':False})\n",
    "myRemi.start()  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73cd1921-a5a1-424c-aa3b-3f3f2032cd06",
   "metadata": {},
   "source": [
    "http://127.0.0.1:8888/proxy/8090"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6b211659-17bd-4fab-b945-fc56f2af4e06",
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import IFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "616824c7-afdd-4e95-8c85-25d1be1d8c5e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "        <iframe\n",
       "            width=\"100%\"\n",
       "            height=\"600px\"\n",
       "            src=\"http://localhost:8888/proxy/8090/\"\n",
       "            frameborder=\"0\"\n",
       "            allowfullscreen\n",
       "            \n",
       "        ></iframe>\n",
       "        "
      ],
      "text/plain": [
       "<IPython.lib.display.IFrame at 0x28850722d30>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "remi.request     INFO     built UI (path=/)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "new project\n",
      ">>>>>>>>>>>>startup time: 0.3019986152648926\n",
      "{}\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "127.0.0.1 - - [24/Nov/2021 18:37:45] \"GET / HTTP/1.1\" 200 -\n",
      "remi.server.ws   INFO     connection established: ('127.0.0.1', 63958)\n",
      "remi.server.ws   INFO     handshake complete\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loading additional widgets\n",
      "-------------used keys:[]\n",
      "EditorAttributes set widget\n",
      "selected widget: container0\n",
      "selected widget class: Container\n",
      "is widget Container: True\n",
      "0.5186376571655273\n"
     ]
    }
   ],
   "source": [
    "IFrame(src=f\"http://localhost:8888/proxy/{remiport}/\",width=\"100%\",height=\"600px\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f87d351-bc19-4c5a-b6dd-ced963cd4efc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
