|
| 1 | +# ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ |
| 2 | +# ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃ |
| 3 | +# ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃ |
| 4 | +# ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃ |
| 5 | +# ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃ |
| 6 | +# ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫ |
| 7 | +# ┃ Copyright (c) 2017, the Perspective Authors. ┃ |
| 8 | +# ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃ |
| 9 | +# ┃ This file is part of the Perspective library, distributed under the terms ┃ |
| 10 | +# ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃ |
| 11 | +# ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ |
| 12 | + |
| 13 | + |
| 14 | +class VirtualSessionModel: |
| 15 | + """ |
| 16 | + An interface for implementing a Perspective `VirtualServer`. It operates |
| 17 | + thusly: |
| 18 | +
|
| 19 | + - A table is selected by name (validated via `get_hosted_tables`). |
| 20 | +
|
| 21 | + - The UI will ask the model to create a temporary table with the results |
| 22 | + of querying this table with a specific query `config`, a simple struct |
| 23 | + which reflects the UI configurable fields (see `get_features`). |
| 24 | +
|
| 25 | + - The UI will query slices of the temporary table as it needs them to |
| 26 | + render. This may be a rectangular slice, a whole column or the entire |
| 27 | + set, and it is returned from teh model via a custom push-only |
| 28 | + struct `PerspectiveColumn` for now, though in the future we will support |
| 29 | + e.g. Polars and other arrow-native formats directly. |
| 30 | +
|
| 31 | + - The UI will delete its own temporary tables via `view_delete` but it is |
| 32 | + ok for them to die intermittently, the UI will recover automatically. |
| 33 | + """ |
| 34 | + |
| 35 | + def get_features(self): |
| 36 | + """ |
| 37 | + [OPTIONAL] Toggle UI features through data model support. For example, |
| 38 | + setting `"group_by": False` would hide the "Group By" UI control, as |
| 39 | + well as prevent this field from appearing in `config` dicts later |
| 40 | + provided to `table_make_view`. |
| 41 | +
|
| 42 | + This API defaults to just "columns", e.g. a simple flat datagrid in |
| 43 | + which you can just scroll, select and format columns. |
| 44 | +
|
| 45 | + # Example |
| 46 | +
|
| 47 | + ```python |
| 48 | + return { |
| 49 | + "group_by": True, |
| 50 | + "split_by": True, |
| 51 | + "sort": True, |
| 52 | + "expressions": True, |
| 53 | + "filter_ops": { |
| 54 | + "integer": ["==", "<"], |
| 55 | + }, |
| 56 | + "aggregates": { |
| 57 | + "string": ["count"], |
| 58 | + "float": ["count", "sum"], |
| 59 | + }, |
| 60 | + } |
| 61 | + ``` |
| 62 | + """ |
| 63 | + |
| 64 | + pass |
| 65 | + |
| 66 | + def get_hosted_tables(self) -> list[str]: |
| 67 | + """ |
| 68 | + List of `Table` names available to query from. |
| 69 | + """ |
| 70 | + |
| 71 | + pass |
| 72 | + |
| 73 | + def table_schema(self, table_name): |
| 74 | + """ |
| 75 | + Get the _Perspective Schema_ for a `Table`, a mapping of column name to |
| 76 | + Perspective column types, a simplified set of six visually-relevant |
| 77 | + types mapped from DuckDB's much richer type system. Optionally, |
| 78 | + a model may also implement `view_schema` which describes temporary |
| 79 | + tables, but for DuckDB this method is identical. |
| 80 | + """ |
| 81 | + |
| 82 | + pass |
| 83 | + |
| 84 | + def table_columns_size(self, table_name, config): |
| 85 | + pass |
| 86 | + |
| 87 | + def table_size(self, table_name): |
| 88 | + """ |
| 89 | + Get a table's row count. Optionally, a model may also implement the |
| 90 | + `view_size` method to get the row count for temporary tables, but for |
| 91 | + DuckDB this method is identical. |
| 92 | + """ |
| 93 | + |
| 94 | + pass |
| 95 | + |
| 96 | + def view_schema(self, view_name, config): |
| 97 | + return self.table_schema(view_name) |
| 98 | + |
| 99 | + def view_size(self, view_name): |
| 100 | + return self.table_size(view_name) |
| 101 | + |
| 102 | + def table_make_view(self, table_name, view_name, config): |
| 103 | + """ |
| 104 | + Create a temporary table `view_name` from the results of querying |
| 105 | + `table_name` with a query configuration `config`. |
| 106 | + """ |
| 107 | + |
| 108 | + pass |
| 109 | + |
| 110 | + def table_validate_expression(self, view_name, expression): |
| 111 | + """ |
| 112 | + [OPTIONAL] Given a temporary table `view_name`, validate the type of |
| 113 | + a column expression string `expression`, or raise an error if the |
| 114 | + expression is invalid. This is enabeld by `"expressions"` via |
| 115 | + `get_features` and defaults to allow all expressions. |
| 116 | + """ |
| 117 | + |
| 118 | + pass |
| 119 | + |
| 120 | + def view_delete(self, view_name): |
| 121 | + """ |
| 122 | + Delete a temporary table. The UI will do this automatically, and it |
| 123 | + can recover. |
| 124 | + """ |
| 125 | + |
| 126 | + pass |
| 127 | + |
| 128 | + def view_get_data(self, view_name, config, viewport, data): |
| 129 | + """ |
| 130 | + Serialize a rectangular slice `viewport` from temporary table |
| 131 | + `view_name`, into the `PerspectiveColumn` serialization API injected |
| 132 | + via `data`. The push-only `PerspectiveColumn` type can handle casting |
| 133 | + Python types as input, but once a type is pushed to a column name it |
| 134 | + must not be changed. |
| 135 | + """ |
| 136 | + |
| 137 | + pass |
0 commit comments