Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] Dynamic Responsive Printing of Tables, Schema and Series #1662

Merged
merged 12 commits into from
Nov 25, 2023
Merged
140 changes: 55 additions & 85 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ async-stream = "0.3.5"
bytes = "1.4.0"
chrono = "0.4.26"
chrono-tz = "0.8.3"
comfy-table = "7.1.0"
futures = "0.3.28"
html-escape = "0.2.13"
indexmap = "2.0.0"
itertools = "0.11"
num-derive = "0.3.3"
num-traits = "0.2"
prettytable-rs = "0.10"
rand = "^0.8"
rayon = "1.7.0"
rstest = "0.18.2"
Expand Down
2 changes: 1 addition & 1 deletion daft/dataframe/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@
try:
from IPython.display import display

display(dataframe_display)
display(dataframe_display, clear=True)

Check warning on line 1077 in daft/dataframe/dataframe.py

View check run for this annotation

Codecov / codecov/patch

daft/dataframe/dataframe.py#L1077

Added line #L1077 was not covered by tests
except ImportError:
print(dataframe_display)
return None
Expand Down
2 changes: 1 addition & 1 deletion src/daft-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ base64 = "0.21.5"
bincode = {workspace = true}
chrono = {workspace = true}
chrono-tz = {workspace = true}
comfy-table = {workspace = true}
common-error = {path = "../common/error", default-features = false}
dyn-clone = "1.0.16"
fnv = "1.0.7"
Expand All @@ -14,7 +15,6 @@ log = {workspace = true}
ndarray = "0.15.6"
num-derive = {workspace = true}
num-traits = {workspace = true}
prettytable-rs = {workspace = true}
pyo3 = {workspace = true, optional = true}
pyo3-log = {workspace = true}
rand = {workspace = true}
Expand Down
22 changes: 12 additions & 10 deletions src/daft-core/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
borrow::Cow,
collections::{hash_map::DefaultHasher, HashSet},
fmt::{Display, Formatter, Result},
hash::{Hash, Hasher},
Expand All @@ -8,7 +9,7 @@ use std::{
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};

use crate::datatypes::Field;
use crate::{datatypes::Field, utils::display_table::make_comfy_table};

use common_error::{DaftError, DaftResult};

Expand Down Expand Up @@ -167,15 +168,16 @@ impl Default for Schema {
impl Display for Schema {
// Produces an ASCII table.
fn fmt(&self, f: &mut Formatter) -> Result {
let mut table = prettytable::Table::new();

let header = self
.fields
.iter()
.map(|(name, field)| format!("{}\n{}", name, field.dtype))
.collect();
table.add_row(header);
write!(f, "{table}")
let table = make_comfy_table(
self.fields
.values()
.map(Cow::Borrowed)
.collect::<Vec<_>>()
.as_slice(),
None,
None,
);
writeln!(f, "{table}")
}
}

Expand Down
46 changes: 10 additions & 36 deletions src/daft-core/src/series/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ mod ops;
mod serdes;
mod series_like;
use std::{
borrow::Cow,
fmt::{Display, Formatter, Result},
sync::Arc,
};

use crate::{
array::ops::{from_arrow::FromArrow, full::FullNull},
datatypes::{DataType, Field},
utils::display_table::make_comfy_table,
with_match_daft_types,
};
use common_error::DaftResult;
Expand Down Expand Up @@ -77,48 +79,20 @@ impl Series {
}
}

pub fn to_prettytable(&self) -> prettytable::Table {
let mut table = prettytable::Table::new();

let header =
prettytable::Cell::new(format!("{}\n{}", self.name(), self.data_type()).as_str())
.with_style(prettytable::Attr::Bold);
table.add_row(prettytable::Row::new(vec![header]));

let head_rows;
let tail_rows;

if self.len() > 10 {
head_rows = 5;
tail_rows = 5;
} else {
head_rows = self.len();
tail_rows = 0;
}

for i in 0..head_rows {
let row = vec![self.str_value(i).unwrap()];
table.add_row(row.into());
}
if tail_rows != 0 {
let row = vec!["..."];
table.add_row(row.into());
}

for i in 0..tail_rows {
let row = vec![self.str_value(self.len() - tail_rows - 1 + i).unwrap()];
table.add_row(row.into());
}

table
pub fn to_comfy_table(&self) -> comfy_table::Table {
make_comfy_table(
vec![Cow::Borrowed(self.field())].as_slice(),
Some([self].as_slice()),
Some(80),
)
}
}

impl Display for Series {
// `f` is a buffer, and this method must write the formatted string into it
fn fmt(&self, f: &mut Formatter) -> Result {
let table = self.to_prettytable();
write!(f, "{table}")
let table = self.to_comfy_table();
writeln!(f, "{table}")
}
}

Expand Down
Loading
Loading