Load a dict of list of dict representations of some layout data, automatically parse its type, and save it as any of BaseLayoutElement or Layout datatype. Args: data (Union[Dict, List]): A dict of list of dict representations of the layout data Raises: V
(data: Union[Dict, List[Dict]])
| 45 | |
| 46 | |
| 47 | def load_dict(data: Union[Dict, List[Dict]]) -> Union[BaseLayoutElement, Layout]: |
| 48 | """Load a dict of list of dict representations of some layout data, |
| 49 | automatically parse its type, and save it as any of BaseLayoutElement |
| 50 | or Layout datatype. |
| 51 | |
| 52 | Args: |
| 53 | data (Union[Dict, List]): |
| 54 | A dict of list of dict representations of the layout data |
| 55 | |
| 56 | Raises: |
| 57 | ValueError: |
| 58 | If the data format is incompatible with the layout-data-JSON format, |
| 59 | raise a `ValueError`. |
| 60 | ValueError: |
| 61 | If any `block_type` name is not in the available list of layout element |
| 62 | names defined in `BASECOORD_ELEMENT_NAMEMAP`, raise a `ValueError`. |
| 63 | |
| 64 | Returns: |
| 65 | Union[BaseLayoutElement, Layout]: |
| 66 | Based on the dict format, it will automatically parse the type of |
| 67 | the data and load it accordingly. |
| 68 | """ |
| 69 | if isinstance(data, dict): |
| 70 | if "page_data" in data: |
| 71 | # It is a layout instance |
| 72 | return Layout(load_dict(data["blocks"])._blocks, page_data=data["page_data"]) |
| 73 | else: |
| 74 | |
| 75 | if data["block_type"] not in BASECOORD_ELEMENT_NAMEMAP: |
| 76 | raise ValueError(f"Invalid block_type {data['block_type']}") |
| 77 | |
| 78 | # Check if it is a textblock |
| 79 | is_textblock = any(ele in data for ele in TextBlock._features) |
| 80 | if is_textblock: |
| 81 | return TextBlock.from_dict(data) |
| 82 | else: |
| 83 | return BASECOORD_ELEMENT_NAMEMAP[data["block_type"]].from_dict(data) |
| 84 | |
| 85 | elif isinstance(data, list): |
| 86 | return Layout([load_dict(ele) for ele in data]) |
| 87 | |
| 88 | else: |
| 89 | raise ValueError(f"Invalid input JSON structure.") |
| 90 | |
| 91 | |
| 92 | def load_csv(filename: str, block_type: str = None) -> Layout: |