|
| 1 | +import base64 |
| 2 | +import requests |
| 3 | +from typing import Dict |
| 4 | + |
1 | 5 | from .base_client import BaseClient, AsyncBaseClient
|
| 6 | +from . import MdResponse |
| 7 | + |
| 8 | + |
| 9 | +class Axiomatic(BaseClient): |
| 10 | + |
| 11 | + def __init__(self, *args, **kwargs): |
| 12 | + super().__init__(*args, **kwargs) |
| 13 | + |
| 14 | + self.document_helper = DocumentHelper(self) |
| 15 | + |
| 16 | + |
| 17 | +class DocumentHelper: |
| 18 | + |
| 19 | + def __init__(self, ax_client: Axiomatic): |
| 20 | + self.ax_client = ax_client |
| 21 | + |
| 22 | + def pdf_from_url(self, url: str) -> MdResponse: |
| 23 | + """Download a PDF document from a URL and parse it into a Markdown response.""" |
| 24 | + file = requests.get(url) |
| 25 | + response = self.ax_client.document.parse(file=file) |
| 26 | + return response.content |
| 27 | + |
| 28 | + def pdf_from_file(self, path: str) -> MdResponse: |
| 29 | + """Open a PDF document from a file path and parse it into a Markdown response.""" |
| 30 | + with open(path, "rb") as f: |
| 31 | + file = f.read() |
| 32 | + response = self.ax_client.document.parse(file=file) |
| 33 | + return response.content |
| 34 | + |
| 35 | + def plot_b64_images(self, images: Dict[str, str]): |
| 36 | + """Plot a dictionary of base64 images.""" |
| 37 | + import ipywidgets as widgets # type: ignore |
| 38 | + from IPython.display import display # type: ignore |
| 39 | + |
| 40 | + base64_images = list(images.values()) |
| 41 | + current_index = [0] |
| 42 | + |
| 43 | + def display_base64_image(index): |
| 44 | + image_widget.value = base64.b64decode(base64_images[index]) |
| 45 | + |
| 46 | + def navigate_image(change): |
| 47 | + current_index[0] = (current_index[0] + change) % len(base64_images) |
| 48 | + display_base64_image(current_index[0]) |
| 49 | + |
| 50 | + image_widget = widgets.Image(format="png", width=600) |
| 51 | + prev_button = widgets.Button(description="Previous", icon="arrow-left") |
| 52 | + next_button = widgets.Button(description="Next", icon="arrow-right") |
| 53 | + |
| 54 | + prev_button.on_click(lambda b: navigate_image(-1)) |
| 55 | + next_button.on_click(lambda b: navigate_image(1)) |
2 | 56 |
|
| 57 | + buttons = widgets.HBox([prev_button, next_button]) |
| 58 | + layout = widgets.VBox([buttons, image_widget]) |
3 | 59 |
|
4 |
| -class Axiomatic(BaseClient): ... |
| 60 | + display(layout) |
| 61 | + display_base64_image(current_index[0]) |
5 | 62 |
|
6 | 63 |
|
7 | 64 | class AsyncAxiomatic(AsyncBaseClient): ...
|
0 commit comments