|
11 | 11 | }, |
12 | 12 | { |
13 | 13 | "cell_type": "code", |
14 | | - "execution_count": 1, |
| 14 | + "execution_count": 2, |
15 | 15 | "id": "d99f5fbc", |
16 | 16 | "metadata": {}, |
17 | 17 | "outputs": [], |
18 | 18 | "source": [ |
19 | | - "import requests" |
| 19 | + "import requests\n", |
| 20 | + "import asyncio\n", |
| 21 | + "import json\n", |
| 22 | + "import websockets\n", |
| 23 | + "from ipyleaflet import Map, GeoJSON" |
20 | 24 | ] |
21 | 25 | }, |
22 | 26 | { |
|
29 | 33 | }, |
30 | 34 | { |
31 | 35 | "cell_type": "code", |
32 | | - "execution_count": 2, |
| 36 | + "execution_count": 3, |
33 | 37 | "id": "f95065a9", |
34 | 38 | "metadata": {}, |
35 | 39 | "outputs": [], |
36 | 40 | "source": [ |
37 | | - "dispatch_api = \"http://localhost:8000/\"" |
| 41 | + "dispatch_api = \"localhost:8000\"" |
38 | 42 | ] |
39 | 43 | }, |
40 | 44 | { |
41 | 45 | "cell_type": "code", |
42 | | - "execution_count": 14, |
| 46 | + "execution_count": 4, |
43 | 47 | "id": "251a343f", |
44 | 48 | "metadata": {}, |
45 | 49 | "outputs": [], |
|
50 | 54 | }, |
51 | 55 | { |
52 | 56 | "cell_type": "code", |
53 | | - "execution_count": 7, |
| 57 | + "execution_count": 5, |
54 | 58 | "id": "c83aa9d5", |
55 | 59 | "metadata": {}, |
56 | 60 | "outputs": [], |
|
96 | 100 | }, |
97 | 101 | { |
98 | 102 | "cell_type": "code", |
99 | | - "execution_count": 8, |
| 103 | + "execution_count": 6, |
100 | 104 | "id": "e0618338", |
101 | 105 | "metadata": {}, |
102 | 106 | "outputs": [], |
103 | 107 | "source": [ |
104 | | - "tiles = requests.post(f\"{dispatch_api}/tiles\", json={\n", |
| 108 | + "tiles = requests.post(f\"http://{dispatch_api}/tiles\", json={\n", |
105 | 109 | " \"grid\": \"20x20km\",\n", |
106 | 110 | " \"aoi\": area_of_interest\n", |
107 | 111 | "}).json()" |
108 | 112 | ] |
109 | 113 | }, |
110 | 114 | { |
111 | 115 | "cell_type": "code", |
112 | | - "execution_count": 13, |
| 116 | + "execution_count": 7, |
113 | 117 | "id": "6de8d686", |
114 | 118 | "metadata": {}, |
115 | 119 | "outputs": [ |
116 | 120 | { |
117 | 121 | "data": { |
118 | 122 | "application/vnd.jupyter.widget-view+json": { |
119 | | - "model_id": "ef6f633a4eca4ec686c253deb9302479", |
| 123 | + "model_id": "eafd8bf582b0472ba6f0b719594d1041", |
120 | 124 | "version_major": 2, |
121 | 125 | "version_minor": 0 |
122 | 126 | }, |
123 | 127 | "text/plain": [ |
124 | 128 | "Map(center=[42.251628548555004, -6.37490034623255], controls=(ZoomControl(options=['position', 'zoom_in_text',…" |
125 | 129 | ] |
126 | 130 | }, |
127 | | - "execution_count": 13, |
| 131 | + "execution_count": 7, |
128 | 132 | "metadata": {}, |
129 | 133 | "output_type": "execute_result" |
130 | 134 | } |
131 | 135 | ], |
132 | 136 | "source": [ |
133 | | - "from ipyleaflet import Map, GeoJSON\n", |
134 | | - "\n", |
135 | 137 | "# Create a map centered at the approximate center of the area of interest\n", |
136 | 138 | "m = Map(center=[42.251628548555004, -6.37490034623255], zoom=8)\n", |
137 | 139 | " \n", |
|
174 | 176 | } |
175 | 177 | ], |
176 | 178 | "source": [ |
177 | | - "upscaling_task = requests.post(f\"{dispatch_api}/upscale_tasks\", json={\n", |
| 179 | + "upscaling_task = requests.post(f\"http://{dispatch_api}/upscale_tasks\", json={\n", |
178 | 180 | " \"title\": \"Forest Fire Detection\",\n", |
179 | 181 | " \"label\": \"openeo\",\n", |
180 | 182 | " \"service\": {\n", |
|
203 | 205 | }, |
204 | 206 | { |
205 | 207 | "cell_type": "code", |
206 | | - "execution_count": null, |
| 208 | + "execution_count": 9, |
| 209 | + "id": "ce1e3da9-d0ae-4c57-b42e-143f5dc109fd", |
| 210 | + "metadata": {}, |
| 211 | + "outputs": [], |
| 212 | + "source": [ |
| 213 | + "upscaling_task = {\n", |
| 214 | + " \"id\": 4\n", |
| 215 | + "}" |
| 216 | + ] |
| 217 | + }, |
| 218 | + { |
| 219 | + "cell_type": "code", |
| 220 | + "execution_count": 38, |
207 | 221 | "id": "ac428293-7cd4-49a8-9bfa-4e0dc8f4d2cc", |
208 | 222 | "metadata": {}, |
209 | 223 | "outputs": [ |
210 | 224 | { |
211 | 225 | "data": { |
212 | 226 | "application/vnd.jupyter.widget-view+json": { |
213 | | - "model_id": "a89dccec118c4dbe829f9b97b34eb872", |
| 227 | + "model_id": "42f8c0e923364838ab783c8c7fe48dcb", |
214 | 228 | "version_major": 2, |
215 | 229 | "version_minor": 0 |
216 | 230 | }, |
|
220 | 234 | }, |
221 | 235 | "metadata": {}, |
222 | 236 | "output_type": "display_data" |
| 237 | + }, |
| 238 | + { |
| 239 | + "ename": "ConnectionClosedError", |
| 240 | + "evalue": "received 1012 (service restart); then sent 1012 (service restart)", |
| 241 | + "output_type": "error", |
| 242 | + "traceback": [ |
| 243 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 244 | + "\u001b[0;31mConnectionResetError\u001b[0m Traceback (most recent call last)", |
| 245 | + "File \u001b[0;32m~/.pyenv/versions/3.10.12/lib/python3.10/asyncio/selector_events.py:862\u001b[0m, in \u001b[0;36m_SelectorSocketTransport._read_ready__data_received\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 861\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 862\u001b[0m data \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_sock\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrecv\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmax_size\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 863\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m (\u001b[38;5;167;01mBlockingIOError\u001b[39;00m, \u001b[38;5;167;01mInterruptedError\u001b[39;00m):\n", |
| 246 | + "\u001b[0;31mConnectionResetError\u001b[0m: [Errno 54] Connection reset by peer", |
| 247 | + "\nThe above exception was the direct cause of the following exception:\n", |
| 248 | + "\u001b[0;31mConnectionClosedError\u001b[0m Traceback (most recent call last)", |
| 249 | + "Cell \u001b[0;32mIn[38], line 52\u001b[0m\n\u001b[1;32m 49\u001b[0m \u001b[38;5;28;01mbreak\u001b[39;00m\n\u001b[1;32m 51\u001b[0m \u001b[38;5;66;03m# Run the websocket listener in the notebook\u001b[39;00m\n\u001b[0;32m---> 52\u001b[0m \u001b[38;5;28;01mawait\u001b[39;00m listen_for_updates()\n", |
| 250 | + "Cell \u001b[0;32mIn[38], line 31\u001b[0m, in \u001b[0;36mlisten_for_updates\u001b[0;34m()\u001b[0m\n\u001b[1;32m 29\u001b[0m \u001b[38;5;28;01masync\u001b[39;00m \u001b[38;5;28;01mwith\u001b[39;00m websockets\u001b[38;5;241m.\u001b[39mconnect(ws_url) \u001b[38;5;28;01mas\u001b[39;00m websocket:\n\u001b[1;32m 30\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[0;32m---> 31\u001b[0m message \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mawait\u001b[39;00m websocket\u001b[38;5;241m.\u001b[39mrecv()\n\u001b[1;32m 32\u001b[0m status \u001b[38;5;241m=\u001b[39m json\u001b[38;5;241m.\u001b[39mloads(message)\n\u001b[1;32m 33\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m status\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdata\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n", |
| 251 | + "File \u001b[0;32m~/.pyenv/versions/3.10.12/lib/python3.10/site-packages/websockets/asyncio/connection.py:322\u001b[0m, in \u001b[0;36mConnection.recv\u001b[0;34m(self, decode)\u001b[0m\n\u001b[1;32m 318\u001b[0m \u001b[38;5;66;03m# fallthrough\u001b[39;00m\n\u001b[1;32m 319\u001b[0m \n\u001b[1;32m 320\u001b[0m \u001b[38;5;66;03m# Wait for the protocol state to be CLOSED before accessing close_exc.\u001b[39;00m\n\u001b[1;32m 321\u001b[0m \u001b[38;5;28;01mawait\u001b[39;00m asyncio\u001b[38;5;241m.\u001b[39mshield(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mconnection_lost_waiter)\n\u001b[0;32m--> 322\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mprotocol\u001b[38;5;241m.\u001b[39mclose_exc \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mself\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mrecv_exc\u001b[39;00m\n", |
| 252 | + "\u001b[0;31mConnectionClosedError\u001b[0m: received 1012 (service restart); then sent 1012 (service restart)" |
| 253 | + ] |
223 | 254 | } |
224 | 255 | ], |
225 | 256 | "source": [ |
226 | | - "from ipyleaflet import Map, GeoJSON\n", |
227 | | - "import requests\n", |
228 | | - "import time\n", |
229 | | - "\n", |
230 | 257 | "m = Map(center=[42.251628548555004, -6.37490034623255], zoom=8)\n", |
231 | 258 | "geo_json = GeoJSON(\n", |
232 | 259 | " data={\n", |
|
253 | 280 | " \"fillOpacity\": 0.5\n", |
254 | 281 | " }\n", |
255 | 282 | "\n", |
256 | | - "while upscaling_task[\"status\"] not in [\"finished\", \"canceled\", \"failed\"]:\n", |
257 | | - " upscaling_task = requests.get(f\"{dispatch_api}/upscale_tasks/{upscaling_task['id']}\").json()\n", |
258 | | - " if upscaling_task.get(\"jobs\"):\n", |
259 | | - " features = []\n", |
260 | | - " for job in upscaling_task[\"jobs\"]:\n", |
261 | | - " features.append({\n", |
262 | | - " \"type\": \"Feature\",\n", |
263 | | - " \"geometry\": job[\"parameters\"][\"spatial_extent\"],\n", |
264 | | - " \"properties\": {\n", |
265 | | - " \"status\": job[\"status\"],\n", |
| 283 | + "async def listen_for_updates():\n", |
| 284 | + " ws_url = f\"ws://{dispatch_api}/ws/upscale_tasks/{upscaling_task['id']}?interval=15\"\n", |
| 285 | + " async with websockets.connect(ws_url) as websocket:\n", |
| 286 | + " while True:\n", |
| 287 | + " message = await websocket.recv()\n", |
| 288 | + " status = json.loads(message)\n", |
| 289 | + " if status.get(\"data\"):\n", |
| 290 | + " features = []\n", |
| 291 | + " for job in status[\"data\"][\"jobs\"]:\n", |
| 292 | + " features.append({\n", |
| 293 | + " \"type\": \"Feature\",\n", |
| 294 | + " \"geometry\": job[\"parameters\"][\"spatial_extent\"],\n", |
| 295 | + " \"properties\": {\n", |
| 296 | + " \"status\": job[\"status\"],\n", |
| 297 | + " }\n", |
| 298 | + " })\n", |
| 299 | + " geo_json.data = {\n", |
| 300 | + " \"type\": \"FeatureCollection\",\n", |
| 301 | + " \"features\": features\n", |
266 | 302 | " }\n", |
267 | | - " })\n", |
268 | | - " geo_json.data = {\n", |
269 | | - " \"type\": \"FeatureCollection\",\n", |
270 | | - " \"features\": features\n", |
271 | | - " }\n", |
272 | | - " geo_json.style_callback = job_style\n", |
273 | | - " time.sleep(15)" |
| 303 | + " geo_json.style_callback = job_style\n", |
| 304 | + " if status.get(\"status\") in [\"finished\", \"canceled\", \"failed\"]:\n", |
| 305 | + " break\n", |
| 306 | + "\n", |
| 307 | + "# Run the websocket listener in the notebook\n", |
| 308 | + "await listen_for_updates()" |
274 | 309 | ] |
| 310 | + }, |
| 311 | + { |
| 312 | + "cell_type": "code", |
| 313 | + "execution_count": null, |
| 314 | + "id": "56bbbdce-45bc-4c05-a6f6-66ab6dac4808", |
| 315 | + "metadata": {}, |
| 316 | + "outputs": [], |
| 317 | + "source": [] |
275 | 318 | } |
276 | 319 | ], |
277 | 320 | "metadata": { |
|
0 commit comments