perspective_python/client/client_sync.rs
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
13use std::collections::HashMap;
14use std::future::Future;
15use std::sync::Arc;
16
17use perspective_client::config::Scalar;
18use perspective_client::{JoinType, TableRef, assert_table_api, assert_view_api};
19#[cfg(doc)]
20use perspective_client::{TableInitOptions, UpdateOptions, config::ViewConfigUpdate};
21use pyo3::exceptions::PyTypeError;
22use pyo3::marker::Ungil;
23use pyo3::prelude::*;
24use pyo3::types::*;
25
26use super::client_async::*;
27use crate::py_err::ResultTClientErrorExt;
28use crate::server::Server;
29
30pub(crate) fn py_to_table_ref(val: &Bound<'_, PyAny>) -> PyResult<TableRef> {
31 if let Ok(t) = val.downcast::<Table>() {
32 let table_ref = t.borrow();
33 Ok(TableRef::from(&*table_ref.0.table))
34 } else if let Ok(name) = val.extract::<String>() {
35 Ok(TableRef::from(name))
36 } else {
37 Err(PyTypeError::new_err(
38 "Expected a Table or string table name",
39 ))
40 }
41}
42
43pub(crate) fn parse_join_type(join_type: Option<&str>) -> PyResult<JoinType> {
44 match join_type {
45 Some("left") => Ok(JoinType::Left),
46 Some("outer") => Ok(JoinType::Outer),
47 None | Some("inner") => Ok(JoinType::Inner),
48 Some(other) => Err(pyo3::exceptions::PyValueError::new_err(format!(
49 "Unknown join type: \"{}\"",
50 other
51 ))),
52 }
53}
54
55pub(crate) fn scalar_to_py(py: Python<'_>, scalar: &Scalar) -> PyObject {
56 match scalar {
57 Scalar::Float(x) => x.into_pyobject(py).unwrap().into_any().unbind(),
58 Scalar::String(x) => x.into_pyobject(py).unwrap().into_any().unbind(),
59 Scalar::Bool(x) => x.into_pyobject(py).unwrap().to_owned().into_any().unbind(),
60 Scalar::Null => py.None(),
61 }
62}
63
64pub(crate) trait PyFutureExt: Future {
65 fn py_block_on(self, py: Python<'_>) -> Self::Output
66 where
67 Self: Sized + Send,
68 Self::Output: Ungil,
69 {
70 use pollster::FutureExt;
71 py.allow_threads(move || self.block_on())
72 }
73}
74
75impl<F: Future> PyFutureExt for F {}
76
77/// An instance of a [`Client`] is a connection to a single [`Server`], whether
78/// locally in-memory or remote over some transport like a WebSocket.
79///
80/// [`Client`] and Perspective objects derived from it have _synchronous_ APIs,
81/// suitable for use in a repl or script context where this is the _only_
82/// [`Client`] connected to its [`Server`]. If you want to
83/// integrate with a Web framework or otherwise connect multiple clients,
84/// use [`AsyncClient`].
85#[pyclass(subclass, module = "perspective")]
86pub struct Client(pub(crate) AsyncClient);
87
88#[pymethods]
89impl Client {
90 #[new]
91 #[pyo3(signature = (handle_request, close_cb=None, name=None))]
92 pub fn new(
93 handle_request: Py<PyAny>,
94 close_cb: Option<Py<PyAny>>,
95 name: Option<String>,
96 ) -> PyResult<Self> {
97 let client = AsyncClient::new(handle_request, close_cb, name)?;
98 Ok(Client(client))
99 }
100
101 /// Create a new [`Client`] instance bound to a specific in-process
102 /// [`Server`] (e.g. generally _not_ the global [`Server`]).
103 #[staticmethod]
104 pub fn from_server(py: Python<'_>, server: Py<Server>) -> PyResult<Self> {
105 server.borrow(py).new_local_client()
106 }
107
108 /// Handle a message from the external message queue.
109 /// [`Client::handle_response`] is part of the low-level message-handling
110 /// API necessary to implement new transports for a [`Client`]
111 /// connection to a local-or-remote [`Server`], and
112 /// doesn't generally need to be called directly by "users" of a
113 /// [`Client`] once connected.
114 pub fn handle_response(&self, py: Python<'_>, response: Py<PyBytes>) -> PyResult<bool> {
115 self.0.handle_response(response).py_block_on(py)
116 }
117
118 /// Creates a new [`Table`] from either a _schema_ or _data_.
119 ///
120 /// The [`Client::table`] factory function can be initialized with either a
121 /// _schema_ (see [`Table::schema`]), or data in one of these formats:
122 ///
123 /// - Apache Arrow
124 /// - CSV
125 /// - JSON row-oriented
126 /// - JSON column-oriented
127 /// - NDJSON
128 ///
129 /// When instantiated with _data_, the schema is inferred from this data.
130 /// While this is convenient, inferrence is sometimes imperfect e.g.
131 /// when the input is empty, null or ambiguous. For these cases,
132 /// [`Client::table`] can first be instantiated with a explicit schema.
133 ///
134 /// When instantiated with a _schema_, the resulting [`Table`] is empty but
135 /// with known column names and column types. When subsqeuently
136 /// populated with [`Table::update`], these columns will be _coerced_ to
137 /// the schema's type. This behavior can be useful when
138 /// [`Client::table`]'s column type inferences doesn't work.
139 ///
140 /// The resulting [`Table`] is _virtual_, and invoking its methods
141 /// dispatches events to the `perspective_server::Server` this
142 /// [`Client`] connects to, where the data is stored and all calculation
143 /// occurs.
144 ///
145 /// # Arguments
146 ///
147 /// - `arg` - Either _schema_ or initialization _data_.
148 /// - `options` - Optional configuration which provides one of:
149 /// - `limit` - The max number of rows the resulting [`Table`] can
150 /// store.
151 /// - `index` - The column name to use as an _index_ column. If this
152 /// `Table` is being instantiated by _data_, this column name must be
153 /// present in the data.
154 /// - `name` - The name of the table. This will be generated if it is
155 /// not provided.
156 /// - `format` - The explicit format of the input data, can be one of
157 /// `"json"`, `"columns"`, `"csv"` or `"arrow"`. This overrides
158 /// language-specific type dispatch behavior, which allows stringified
159 /// and byte array alternative inputs.
160 ///
161 /// # Python Examples
162 ///
163 /// Load a CSV from a `str`:
164 ///
165 /// ```python
166 /// table = client.table("x,y\n1,2\n3,4")
167 /// ```
168 #[pyo3(signature = (input, limit=None, index=None, name=None, format=None))]
169 pub fn table(
170 &self,
171 py: Python<'_>,
172 input: Py<PyAny>,
173 limit: Option<u32>,
174 index: Option<Py<PyString>>,
175 name: Option<Py<PyString>>,
176 format: Option<Py<PyString>>,
177 ) -> PyResult<Table> {
178 Ok(Table(
179 self.0
180 .table(input, limit, index, name, format)
181 .py_block_on(py)?,
182 ))
183 }
184
185 /// Opens a [`Table`] that is hosted on the `perspective_server::Server`
186 /// that is connected to this [`Client`].
187 ///
188 /// The `name` property of [`TableInitOptions`] is used to identify each
189 /// [`Table`]. [`Table`] `name`s can be looked up for each [`Client`]
190 /// via [`Client::get_hosted_table_names`].
191 ///
192 /// # Python Examples
193 ///
194 /// ```python
195 /// table = client.open_table("table_one");
196 /// ```
197 pub fn open_table(&self, py: Python<'_>, name: String) -> PyResult<Table> {
198 let client = self.0.clone();
199 let table = client.open_table(name).py_block_on(py)?;
200 Ok(Table(table))
201 }
202
203 /// Creates a new read-only [`Table`] by performing a JOIN on two
204 /// source tables. The resulting table is reactive: when either source
205 /// table is updated, the join is automatically recomputed.
206 ///
207 /// # Python Examples
208 ///
209 /// ```python
210 /// joined = client.join(orders_table, products_table, "Product ID", "left")
211 /// ```
212 #[pyo3(signature = (left, right, on, join_type=None, name=None, right_on=None))]
213 #[allow(clippy::too_many_arguments, reason = "This is a Python API")]
214 pub fn join(
215 &self,
216 py: Python<'_>,
217 left: &Bound<'_, PyAny>,
218 right: &Bound<'_, PyAny>,
219 on: String,
220 join_type: Option<String>,
221 name: Option<String>,
222 right_on: Option<String>,
223 ) -> PyResult<Table> {
224 let left_ref = py_to_table_ref(left)?;
225 let right_ref = py_to_table_ref(right)?;
226 let jt = parse_join_type(join_type.as_deref())?;
227 let options = perspective_client::JoinOptions {
228 join_type: Some(jt),
229 name,
230 right_on,
231 };
232 let table = self
233 .0
234 .client
235 .join(left_ref, right_ref, &on, options)
236 .py_block_on(py)
237 .into_pyerr()?;
238 Ok(Table(AsyncTable {
239 table: Arc::new(table),
240 client: self.0.clone(),
241 }))
242 }
243
244 /// Retrieves the names of all tables that this client has access to.
245 ///
246 /// `name` is a string identifier unique to the [`Table`] (per [`Client`]),
247 /// which can be used in conjunction with [`Client::open_table`] to get
248 /// a [`Table`] instance without the use of [`Client::table`]
249 /// constructor directly (e.g., one created by another [`Client`]).
250 ///
251 /// # Python Examples
252 ///
253 /// ```python
254 /// tables = client.get_hosted_table_names();
255 /// ```
256 pub fn get_hosted_table_names(&self, py: Python<'_>) -> PyResult<Vec<String>> {
257 self.0.get_hosted_table_names().py_block_on(py)
258 }
259
260 /// Register a callback which is invoked whenever [`Client::table`] (on this
261 /// [`Client`]) or [`Table::delete`] (on a [`Table`] belinging to this
262 /// [`Client`]) are called.
263 pub fn on_hosted_tables_update(&self, py: Python<'_>, callback: Py<PyAny>) -> PyResult<u32> {
264 self.0.on_hosted_tables_update(callback).py_block_on(py)
265 }
266
267 /// Remove a callback previously registered via
268 /// [`Client::on_hosted_tables_update`].
269 pub fn remove_hosted_tables_update(&self, py: Python<'_>, callback_id: u32) -> PyResult<()> {
270 self.0
271 .remove_hosted_tables_update(callback_id)
272 .py_block_on(py)
273 }
274
275 /// Provides the [`SystemInfo`] struct, implementation-specific metadata
276 /// about the [`perspective_server::Server`] runtime such as Memory and
277 /// CPU usage.
278 pub fn system_info(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
279 self.0.system_info().py_block_on(py)
280 }
281
282 /// Terminates this [`Client`], cleaning up any [`View`] handles the
283 /// [`Client`] has open as well as its callbacks.
284 pub fn terminate(&self, py: Python<'_>) -> PyResult<()> {
285 self.0.terminate(py)
286 }
287}
288
289/// [`Table`] is Perspective's columnar data frame, analogous to a Pandas/Polars
290/// `DataFrame` or Apache Arrow, supporting append & in-place updates, removal
291/// by index, and update notifications.
292///
293/// A [`Table`] contains columns, each of which have a unique name, are strongly
294/// and consistently typed, and contains rows of data conforming to the column's
295/// type. Each column in a [`Table`] must have the same number of rows, though
296/// not every row must contain data; null-values are used to indicate missing
297/// values in the dataset. The schema of a [`Table`] is _immutable after
298/// creation_, which means the column names and data types cannot be changed
299/// after the [`Table`] has been created. Columns cannot be added or deleted
300/// after creation either, but a [`View`] can be used to select an arbitrary set
301/// of columns from the [`Table`].
302#[pyclass(subclass, name = "Table", module = "perspective")]
303pub struct Table(AsyncTable);
304
305assert_table_api!(Table);
306
307#[pymethods]
308impl Table {
309 #[new]
310 fn new() -> PyResult<Self> {
311 Err(PyTypeError::new_err(
312 "Do not call Table's constructor directly, construct from a Client instance.",
313 ))
314 }
315
316 /// Returns the name of the index column for the table.
317 ///
318 /// # Python Examples
319 ///
320 /// ```python
321 /// table = perspective.table("x,y\n1,2\n3,4", index="x");
322 /// index = client.get_index()
323 /// ```
324 pub fn get_index(&self) -> Option<String> {
325 self.0.get_index()
326 }
327
328 /// Get a copy of the [`Client`] this [`Table`] came from.
329 pub fn get_client(&self, py: Python<'_>) -> Client {
330 Client(self.0.get_client().py_block_on(py))
331 }
332
333 /// Returns the user-specified row limit for this table.
334 pub fn get_limit(&self) -> Option<u32> {
335 self.0.get_limit()
336 }
337
338 /// Returns the user-specified name for this table, or the auto-generated
339 /// name if a name was not specified when the table was created.
340 pub fn get_name(&self) -> String {
341 self.0.get_name()
342 }
343
344 /// Removes all the rows in the [`Table`], but preserves everything else
345 /// including the schema, index, and any callbacks or registered
346 /// [`View`] instances.
347 ///
348 /// Calling [`Table::clear`], like [`Table::update`] and [`Table::remove`],
349 /// will trigger an update event to any registered listeners via
350 /// [`View::on_update`].
351 pub fn clear(&self, py: Python<'_>) -> PyResult<()> {
352 self.0.clear().py_block_on(py)
353 }
354
355 /// Returns the column names of this [`Table`] in "natural" order (the
356 /// ordering implied by the input format).
357 ///
358 /// # Python Examples
359 ///
360 /// ```python
361 /// columns = table.columns()
362 /// ```
363 pub fn columns(&self, py: Python<'_>) -> PyResult<Vec<String>> {
364 self.0.columns().py_block_on(py)
365 }
366
367 /// Delete this [`Table`] and cleans up associated resources.
368 ///
369 /// [`Table`]s do not stop consuming resources or processing updates when
370 /// they are garbage collected in their host language - you must call
371 /// this method to reclaim these.
372 ///
373 /// # Arguments
374 ///
375 /// - `options` An options dictionary.
376 /// - `lazy` Whether to delete this [`Table`] _lazily_. When false (the
377 /// default), the delete will occur immediately, assuming it has no
378 /// [`View`] instances registered to it (which must be deleted first,
379 /// otherwise this method will throw an error). When true, the
380 /// [`Table`] will only be marked for deltion once its [`View`]
381 /// dependency count reaches 0.
382 ///
383 /// # Python Examples
384 ///
385 /// ```python
386 /// table = client.table("x,y\n1,2\n3,4")
387 ///
388 /// # ...
389 ///
390 /// table.delete(lazy=True)
391 /// ```
392 #[pyo3(signature=(lazy=false))]
393 pub fn delete(&self, py: Python<'_>, lazy: bool) -> PyResult<()> {
394 self.0.delete(lazy).py_block_on(py)
395 }
396
397 /// Create a unique channel ID on this [`Table`], which allows
398 /// `View::on_update` callback calls to be associated with the
399 /// `Table::update` which caused them.
400 pub fn make_port(&self, py: Python<'_>) -> PyResult<i32> {
401 let table = self.0.clone();
402 table.make_port().py_block_on(py)
403 }
404
405 /// Register a callback which is called exactly once, when this [`Table`] is
406 /// deleted with the [`Table::delete`] method.
407 ///
408 /// [`Table::on_delete`] resolves when the subscription message is sent, not
409 /// when the _delete_ event occurs.
410 pub fn on_delete(&self, py: Python<'_>, callback: Py<PyAny>) -> PyResult<u32> {
411 let table = self.0.clone();
412 table.on_delete(callback).py_block_on(py)
413 }
414
415 #[pyo3(signature = (input, format=None))]
416 pub fn remove(&self, py: Python<'_>, input: Py<PyAny>, format: Option<String>) -> PyResult<()> {
417 let table = self.0.clone();
418 table.remove(input, format).py_block_on(py)
419 }
420
421 /// Removes a listener with a given ID, as returned by a previous call to
422 /// [`Table::on_delete`].
423 pub fn remove_delete(&self, py: Python<'_>, callback_id: u32) -> PyResult<()> {
424 let table = self.0.clone();
425 table.remove_delete(callback_id).py_block_on(py)
426 }
427
428 /// Returns a table's [`Schema`], a mapping of column names to column types.
429 ///
430 /// The mapping of a [`Table`]'s column names to data types is referred to
431 /// as a [`Schema`]. Each column has a unique name and a data type, one
432 /// of:
433 ///
434 /// - `"boolean"` - A boolean type
435 /// - `"date"` - A timesonze-agnostic date type (month/day/year)
436 /// - `"datetime"` - A millisecond-precision datetime type in the UTC
437 /// timezone
438 /// - `"float"` - A 64 bit float
439 /// - `"integer"` - A signed 32 bit integer (the integer type supported by
440 /// JavaScript)
441 /// - `"string"` - A `String` data type (encoded internally as a
442 /// _dictionary_)
443 ///
444 /// Note that all [`Table`] columns are _nullable_, regardless of the data
445 /// type.
446 pub fn schema(&self, py: Python<'_>) -> PyResult<HashMap<String, String>> {
447 let table = self.0.clone();
448 table.schema().py_block_on(py)
449 }
450
451 /// Validates the given expressions.
452 pub fn validate_expressions(
453 &self,
454 py: Python<'_>,
455 expression: Py<PyAny>,
456 ) -> PyResult<Py<PyAny>> {
457 let table = self.0.clone();
458 table.validate_expressions(expression).py_block_on(py)
459 }
460
461 /// Create a new [`View`] from this table with a specified
462 /// [`ViewConfigUpdate`].
463 ///
464 /// See [`View`] struct.
465 ///
466 /// # Examples
467 ///
468 /// ```python
469 /// view view = table.view(
470 /// columns=["Sales"],
471 /// aggregates={"Sales": "sum"},
472 /// group_by=["Region", "State"],
473 /// )
474 /// ```
475 #[pyo3(signature = (**config))]
476 pub fn view(&self, py: Python<'_>, config: Option<Py<PyDict>>) -> PyResult<View> {
477 Ok(View(self.0.view(config).py_block_on(py)?))
478 }
479
480 /// Returns the number of rows in a [`Table`].
481 pub fn size(&self, py: Python<'_>) -> PyResult<usize> {
482 self.0.size().py_block_on(py)
483 }
484
485 /// Removes all the rows in the [`Table`], but preserves everything else
486 /// including the schema, index, and any callbacks or registered
487 /// [`View`] instances.
488 ///
489 /// Calling [`Table::clear`], like [`Table::update`] and [`Table::remove`],
490 /// will trigger an update event to any registered listeners via
491 /// [`View::on_update`].
492 #[pyo3(signature = (input, format=None))]
493 pub fn replace(
494 &self,
495 py: Python<'_>,
496 input: Py<PyAny>,
497 format: Option<String>,
498 ) -> PyResult<()> {
499 self.0.replace(input, format).py_block_on(py)
500 }
501
502 /// Updates the rows of this table and any derived [`View`] instances.
503 ///
504 /// Calling [`Table::update`] will trigger the [`View::on_update`] callbacks
505 /// register to derived [`View`], and the call itself will not resolve until
506 /// _all_ derived [`View`]'s are notified.
507 ///
508 /// When updating a [`Table`] with an `index`, [`Table::update`] supports
509 /// partial updates, by omitting columns from the update data.
510 ///
511 /// # Arguments
512 ///
513 /// - `input` - The input data for this [`Table`]. The schema of a [`Table`]
514 /// is immutable after creation, so this method cannot be called with a
515 /// schema.
516 /// - `options` - Options for this update step - see
517 /// [`perspective_client::UpdateOptions`].
518 /// ```
519 #[pyo3(signature = (input, port_id=None, format=None))]
520 pub fn update(
521 &self,
522 py: Python<'_>,
523 input: Py<PyAny>,
524 port_id: Option<u32>,
525 format: Option<String>,
526 ) -> PyResult<()> {
527 self.0.update(input, port_id, format).py_block_on(py)
528 }
529}
530
531/// The [`View`] struct is Perspective's query and serialization interface. It
532/// represents a query on the `Table`'s dataset and is always created from an
533/// existing `Table` instance via the [`Table::view`] method.
534///
535/// [`View`]s are immutable with respect to the arguments provided to the
536/// [`Table::view`] method; to change these parameters, you must create a new
537/// [`View`] on the same [`Table`]. However, each [`View`] is _live_ with
538/// respect to the [`Table`]'s data, and will (within a conflation window)
539/// update with the latest state as its parent [`Table`] updates, including
540/// incrementally recalculating all aggregates, pivots, filters, etc. [`View`]
541/// query parameters are composable, in that each parameter works independently
542/// _and_ in conjunction with each other, and there is no limit to the number of
543/// pivots, filters, etc. which can be applied.
544///
545/// To construct a [`View`], call the [`Table::view`] factory method. A
546/// [`Table`] can have as many [`View`]s associated with it as you need -
547/// Perspective conserves memory by relying on a single [`Table`] to power
548/// multiple [`View`]s concurrently.
549#[pyclass(subclass, name = "View", module = "perspective")]
550pub struct View(pub(crate) AsyncView);
551
552assert_view_api!(View);
553
554#[pymethods]
555impl View {
556 #[new]
557 fn new() -> PyResult<Self> {
558 Err(PyTypeError::new_err(
559 "Do not call View's constructor directly, construct from a Table instance.",
560 ))
561 }
562
563 /// Returns an array of strings containing the column paths of the [`View`]
564 /// without any of the source columns.
565 ///
566 /// A column path shows the columns that a given cell belongs to after
567 /// pivots are applied.
568 #[pyo3(signature = (**window))]
569 pub fn column_paths(
570 &self,
571 py: Python<'_>,
572 window: Option<Py<PyDict>>,
573 ) -> PyResult<Vec<String>> {
574 self.0.column_paths(window).py_block_on(py)
575 }
576
577 /// Renders this [`View`] as a column-oriented JSON string. Useful if you
578 /// want to save additional round trip serialize/deserialize cycles.
579 #[pyo3(signature = (**window))]
580 pub fn to_columns_string(
581 &self,
582 py: Python<'_>,
583 window: Option<Py<PyDict>>,
584 ) -> PyResult<String> {
585 self.0.to_columns_string(window).py_block_on(py)
586 }
587
588 /// Renders this `View` as a row-oriented JSON string.
589 #[pyo3(signature = (**window))]
590 pub fn to_json_string(&self, py: Python<'_>, window: Option<Py<PyDict>>) -> PyResult<String> {
591 self.0.to_json_string(window).py_block_on(py)
592 }
593
594 /// Renders this [`View`] as an [NDJSON](https://github.com/ndjson/ndjson-spec)
595 /// formatted `String`.
596 #[pyo3(signature = (**window))]
597 pub fn to_ndjson(&self, py: Python<'_>, window: Option<Py<PyDict>>) -> PyResult<String> {
598 self.0.to_ndjson(window).py_block_on(py)
599 }
600
601 /// Renders this [`View`] as a row-oriented Python `list`.
602 #[pyo3(signature = (**window))]
603 pub fn to_records<'a>(
604 &self,
605 py: Python<'a>,
606 window: Option<Py<PyDict>>,
607 ) -> PyResult<Bound<'a, PyAny>> {
608 let json = self.0.to_json_string(window).py_block_on(py)?;
609 let json_module = PyModule::import(py, "json")?;
610 json_module.call_method1("loads", (json,))
611 }
612
613 /// Renders this [`View`] as a row-oriented Python `list`.
614 #[pyo3(signature = (**window))]
615 pub fn to_json<'a>(
616 &self,
617 py: Python<'a>,
618 window: Option<Py<PyDict>>,
619 ) -> PyResult<Bound<'a, PyAny>> {
620 self.to_records(py, window)
621 }
622
623 /// Renders this [`View`] as a column-oriented Python `dict`.
624 #[pyo3(signature = (**window))]
625 pub fn to_columns<'a>(
626 &self,
627 py: Python<'a>,
628 window: Option<Py<PyDict>>,
629 ) -> PyResult<Bound<'a, PyAny>> {
630 let json = self.0.to_columns_string(window).py_block_on(py)?;
631 let json_module = PyModule::import(py, "json")?;
632 json_module.call_method1("loads", (json,))
633 }
634
635 /// Renders this [`View`] as a CSV `String` in a standard format.
636 #[pyo3(signature = (**window))]
637 pub fn to_csv(&self, py: Python<'_>, window: Option<Py<PyDict>>) -> PyResult<String> {
638 self.0.to_csv(window).py_block_on(py)
639 }
640
641 /// Renders this [`View`] as a `pandas.DataFrame`.
642 #[pyo3(signature = (**window))]
643 // #[deprecated(since="3.2.0", note="Please use `View::to_pandas`")]
644 pub fn to_dataframe(&self, py: Python<'_>, window: Option<Py<PyDict>>) -> PyResult<Py<PyAny>> {
645 self.0.to_dataframe(window).py_block_on(py)
646 }
647
648 /// Renders this [`View`] as a `pandas.DataFrame`.
649 #[pyo3(signature = (**window))]
650 pub fn to_pandas(&self, py: Python<'_>, window: Option<Py<PyDict>>) -> PyResult<Py<PyAny>> {
651 self.0.to_dataframe(window).py_block_on(py)
652 }
653
654 /// Renders this [`View`] as a `polars.DataFrame`.
655 #[pyo3(signature = (**window))]
656 pub fn to_polars(&self, py: Python<'_>, window: Option<Py<PyDict>>) -> PyResult<Py<PyAny>> {
657 self.0.to_polars(window).py_block_on(py)
658 }
659
660 /// Renders this [`View`] as the Apache Arrow data format.
661 ///
662 /// # Arguments
663 ///
664 /// - `window` - a [`ViewWindow`]
665 #[pyo3(signature = (**window))]
666 pub fn to_arrow(&self, py: Python<'_>, window: Option<Py<PyDict>>) -> PyResult<Py<PyBytes>> {
667 self.0.to_arrow(window).py_block_on(py)
668 }
669
670 /// Delete this [`View`] and clean up all resources associated with it.
671 /// [`View`] objects do not stop consuming resources or processing
672 /// updates when they are garbage collected - you must call this method
673 /// to reclaim these.
674 pub fn delete(&self, py: Python<'_>) -> PyResult<()> {
675 self.0.delete().py_block_on(py)
676 }
677
678 pub fn expand(&self, py: Python<'_>, index: u32) -> PyResult<u32> {
679 self.0.expand(index).py_block_on(py)
680 }
681
682 pub fn collapse(&self, py: Python<'_>, index: u32) -> PyResult<u32> {
683 self.0.collapse(index).py_block_on(py)
684 }
685
686 /// Returns this [`View`]'s _dimensions_, row and column count, as well as
687 /// those of the [`crate::Table`] from which it was derived.
688 ///
689 /// - `num_table_rows` - The number of rows in the underlying
690 /// [`crate::Table`].
691 /// - `num_table_columns` - The number of columns in the underlying
692 /// [`crate::Table`] (including the `index` column if this
693 /// [`crate::Table`] was constructed with one).
694 /// - `num_view_rows` - The number of rows in this [`View`]. If this
695 /// [`View`] has a `group_by` clause, `num_view_rows` will also include
696 /// aggregated rows.
697 /// - `num_view_columns` - The number of columns in this [`View`]. If this
698 /// [`View`] has a `split_by` clause, `num_view_columns` will include all
699 /// _column paths_, e.g. the number of `columns` clause times the number
700 /// of `split_by` groups.
701 pub fn dimensions(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
702 self.0.dimensions().py_block_on(py)
703 }
704
705 /// The expression schema of this [`View`], which contains only the
706 /// expressions created on this [`View`]. See [`View::schema`] for
707 /// details.
708 pub fn expression_schema(&self, py: Python<'_>) -> PyResult<HashMap<String, String>> {
709 self.0.expression_schema().py_block_on(py)
710 }
711
712 /// A copy of the [`ViewConfig`] object passed to the [`Table::view`] method
713 /// which created this [`View`].
714 pub fn get_config(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
715 self.0.get_config().py_block_on(py)
716 }
717
718 /// Calculates the [min, max] of the leaf nodes of a column `column_name`.
719 ///
720 /// # Returns
721 ///
722 /// A tuple of [min, max], whose types are column and aggregate dependent.
723 pub fn get_min_max(
724 &self,
725 py: Python<'_>,
726 column_name: String,
727 ) -> PyResult<(PyObject, PyObject)> {
728 self.0.get_min_max(column_name).py_block_on(py)
729 }
730
731 /// The number of aggregated rows in this [`View`]. This is affected by the
732 /// "group_by" configuration parameter supplied to this view's contructor.
733 ///
734 /// # Returns
735 ///
736 /// The number of aggregated rows.
737 pub fn num_rows(&self, py: Python<'_>) -> PyResult<u32> {
738 self.0.num_rows().py_block_on(py)
739 }
740
741 /// The number of aggregated columns in this [`View`]. This is affected by
742 /// the "split_by" configuration parameter supplied to this view's
743 /// contructor.
744 ///
745 /// # Returns
746 ///
747 /// The number of aggregated columns.
748 pub fn num_columns(&self, py: Python<'_>) -> PyResult<u32> {
749 self.0.num_columns().py_block_on(py)
750 }
751
752 /// The schema of this [`View`].
753 ///
754 /// The [`View`] schema differs from the `schema` returned by
755 /// [`Table::schema`]; it may have different column names due to
756 /// `expressions` or `columns` configs, or it maye have _different
757 /// column types_ due to the application og `group_by` and `aggregates`
758 /// config. You can think of [`Table::schema`] as the _input_ schema and
759 /// [`View::schema`] as the _output_ schema of a Perspective pipeline.
760 pub fn schema(&self, py: Python<'_>) -> PyResult<HashMap<String, String>> {
761 self.0.schema().py_block_on(py)
762 }
763
764 /// Register a callback with this [`View`]. Whenever the [`View`] is
765 /// deleted, this callback will be invoked.
766 pub fn on_delete(&self, py: Python<'_>, callback: Py<PyAny>) -> PyResult<u32> {
767 self.0.on_delete(callback).py_block_on(py)
768 }
769
770 /// Unregister a previously registered [`View::on_delete`] callback.
771 pub fn remove_delete(&self, py: Python<'_>, callback_id: u32) -> PyResult<()> {
772 self.0.remove_delete(callback_id).py_block_on(py)
773 }
774
775 /// Register a callback with this [`View`]. Whenever the view's underlying
776 /// table emits an update, this callback will be invoked with an object
777 /// containing `port_id`, indicating which port the update fired on, and
778 /// optionally `delta`, which is the new data that was updated for each
779 /// cell or each row.
780 ///
781 /// # Arguments
782 ///
783 /// - `on_update` - A callback function invoked on update, which receives an
784 /// object with two keys: `port_id`, indicating which port the update was
785 /// triggered on, and `delta`, whose value is dependent on the mode
786 /// parameter.
787 /// - `options` - If this is provided as `OnUpdateOptions { mode:
788 /// Some(OnUpdateMode::Row) }`, then `delta` is an Arrow of the updated
789 /// rows. Otherwise `delta` will be [`Option::None`].
790 #[pyo3(signature = (callback, mode=None))]
791 pub fn on_update(
792 &self,
793 py: Python<'_>,
794 callback: Py<PyAny>,
795 mode: Option<String>,
796 ) -> PyResult<u32> {
797 self.0.on_update(callback, mode).py_block_on(py)
798 }
799
800 /// Unregister a previously registered update callback with this [`View`].
801 ///
802 /// # Arguments
803 ///
804 /// - `id` - A callback `id` as returned by a recipricol call to
805 /// [`View::on_update`].
806 ///
807 /// # Examples
808 ///
809 /// ```rust
810 /// let callback = |_| async { print!("Updated!") };
811 /// let cid = view.on_update(callback, OnUpdateOptions::default()).await?;
812 /// view.remove_update(cid).await?;
813 /// ```
814 pub fn remove_update(&self, py: Python<'_>, callback_id: u32) -> PyResult<()> {
815 self.0.remove_update(callback_id).py_block_on(py)
816 }
817}