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