Render the trajectory
(
self,
action: Action,
state_info: StateInfo,
meta_data: dict[str, Any],
render_screenshot: bool = False,
)
| 252 | self.render_file.flush() |
| 253 | |
| 254 | def render( |
| 255 | self, |
| 256 | action: Action, |
| 257 | state_info: StateInfo, |
| 258 | meta_data: dict[str, Any], |
| 259 | render_screenshot: bool = False, |
| 260 | ) -> None: |
| 261 | """Render the trajectory""" |
| 262 | # text observation |
| 263 | observation = state_info["observation"] |
| 264 | text_obs = observation["text"] |
| 265 | info = state_info["info"] |
| 266 | new_content = f"<h2>New Page</h2>\n" |
| 267 | new_content += f"<h3 class='url'><a href={state_info['info']['page'].url}>URL: {state_info['info']['page'].url}</a></h3>\n" |
| 268 | new_content += f"<div class='state_obv'><pre>{text_obs}</pre><div>\n" |
| 269 | |
| 270 | if render_screenshot: |
| 271 | # image observation |
| 272 | img_obs = observation["image"] |
| 273 | image = Image.fromarray(img_obs) |
| 274 | byte_io = io.BytesIO() |
| 275 | image.save(byte_io, format="PNG") |
| 276 | byte_io.seek(0) |
| 277 | image_bytes = base64.b64encode(byte_io.read()) |
| 278 | image_str = image_bytes.decode("utf-8") |
| 279 | new_content += f"<img src='data:image/png;base64,{image_str}' style='width:50vw; height:auto;'/>\n" |
| 280 | |
| 281 | # meta data |
| 282 | new_content += f"<div class='prev_action' style='background-color:pink'>{meta_data['action_history'][-1]}</div>\n" |
| 283 | |
| 284 | # action |
| 285 | action_str = get_render_action( |
| 286 | action, |
| 287 | info["observation_metadata"], |
| 288 | action_set_tag=self.action_set_tag, |
| 289 | ) |
| 290 | # with yellow background |
| 291 | action_str = f"<div class='predict_action'>{action_str}</div>" |
| 292 | new_content += f"{action_str}\n" |
| 293 | |
| 294 | # add new content |
| 295 | self.render_file.seek(0) |
| 296 | html = self.render_file.read() |
| 297 | html_body = re.findall(r"<body>(.*?)</body>", html, re.DOTALL)[0] |
| 298 | html_body += new_content |
| 299 | |
| 300 | html = HTML_TEMPLATE.format(body=html_body) |
| 301 | self.render_file.seek(0) |
| 302 | self.render_file.truncate() |
| 303 | self.render_file.write(html) |
| 304 | self.render_file.flush() |
| 305 | |
| 306 | def close(self) -> None: |
| 307 | self.render_file.close() |
no test coverage detected