use serde_json::{json, Value};
use super::{Component, Role};
#[derive(Clone, Debug)]
pub struct Dataframe {
id: String,
label: String,
headers: Vec<String>,
value: Value,
interactive: bool,
addable: bool,
sortable: bool,
}
impl Dataframe {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
headers: Vec::new(),
value: json!([]),
interactive: true,
addable: true,
sortable: true,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn headers(mut self, items: &[&str]) -> Self {
self.headers = items.iter().map(|s| s.to_string()).collect();
self
}
pub fn data<T: serde::Serialize>(mut self, v: &T) -> Self {
self.value = serde_json::to_value(v).unwrap_or(json!([]));
self
}
pub fn interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
pub fn addable(mut self, on: bool) -> Self {
self.addable = on;
self
}
pub fn sortable(mut self, on: bool) -> Self {
self.sortable = on;
self
}
}
impl Component for Dataframe {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"dataframe"
}
fn role(&self) -> Role {
Role::Input
}
fn props(&self) -> Value {
json!({ "label": self.label, "headers": self.headers, "value": self.value, "interactive": self.interactive, "addable": self.addable, "sortable": self.sortable })
}
}
#[derive(Clone, Debug)]
pub struct Plot {
id: String,
label: String,
variant: &'static str,
title: Option<String>,
xlabel: Option<String>,
ylabel: Option<String>,
colors: Vec<String>,
width: u32,
height: u32,
value: Value,
}
impl Plot {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
variant: "line",
title: None,
xlabel: None,
ylabel: None,
colors: vec!["#6366f1".into(), "#8b5cf6".into(), "#16a34a".into()],
width: 480,
height: 280,
value: json!({ "labels": [], "series": [] }),
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn variant(mut self, v: &'static str) -> Self {
self.variant = v;
self
}
pub fn title(mut self, t: impl Into<String>) -> Self {
self.title = Some(t.into());
self
}
pub fn xlabel(mut self, t: impl Into<String>) -> Self {
self.xlabel = Some(t.into());
self
}
pub fn ylabel(mut self, t: impl Into<String>) -> Self {
self.ylabel = Some(t.into());
self
}
pub fn colors(mut self, c: &[&str]) -> Self {
self.colors = c.iter().map(|s| s.to_string()).collect();
self
}
pub fn size(mut self, w: u32, h: u32) -> Self {
self.width = w;
self.height = h;
self
}
pub fn data<T: serde::Serialize>(mut self, v: &T) -> Self {
self.value =
serde_json::to_value(v).unwrap_or_else(|_| json!({ "labels": [], "series": [] }));
self
}
}
impl Component for Plot {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"plot"
}
fn role(&self) -> Role {
Role::Output
}
fn props(&self) -> Value {
json!({
"label": self.label, "variant": self.variant, "title": self.title,
"xlabel": self.xlabel, "ylabel": self.ylabel, "colors": self.colors,
"width": self.width, "height": self.height, "value": self.value
})
}
}
#[derive(Clone, Debug)]
pub struct Metric {
id: String,
label: String,
value: String,
delta: Option<String>,
delta_color: Option<String>,
unit: Option<String>,
}
impl Metric {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
value: String::new(),
delta: None,
delta_color: None,
unit: None,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn value(mut self, v: impl Into<String>) -> Self {
self.value = v.into();
self
}
pub fn delta(mut self, d: impl Into<String>) -> Self {
self.delta = Some(d.into());
self
}
pub fn delta_color(mut self, c: impl Into<String>) -> Self {
self.delta_color = Some(c.into());
self
}
pub fn unit(mut self, u: impl Into<String>) -> Self {
self.unit = Some(u.into());
self
}
}
impl Component for Metric {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"metric"
}
fn role(&self) -> Role {
Role::Output
}
fn props(&self) -> Value {
json!({
"label": self.label,
"value": self.value,
"delta": self.delta,
"delta_color": self.delta_color,
"unit": self.unit
})
}
}
#[derive(Clone, Debug)]
pub struct Label {
id: String,
label: String,
value: String,
variant: String,
size: u32,
}
impl Label {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
value: String::new(),
variant: "normal".into(),
size: 26,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn value(mut self, v: impl Into<String>) -> Self {
self.value = v.into();
self
}
pub fn variant(mut self, v: impl Into<String>) -> Self {
self.variant = v.into();
self
}
pub fn size(mut self, px: u32) -> Self {
self.size = px;
self
}
}
impl Component for Label {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"label"
}
fn role(&self) -> Role {
Role::Output
}
fn props(&self) -> Value {
json!({ "label": self.label, "value": self.value, "variant": self.variant, "size": self.size })
}
}
#[derive(Clone, Debug)]
pub struct Json {
id: String,
label: String,
value: Value,
interactive: bool,
out: bool,
}
impl Json {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
value: Value::Null,
interactive: true,
out: false,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn value(mut self, v: Value) -> Self {
self.value = v;
self
}
pub fn value_str(mut self, s: &str) -> Self {
if let Ok(v) = serde_json::from_str(s) {
self.value = v;
}
self
}
pub fn interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
pub fn output(mut self) -> Self {
self.out = true;
self
}
pub fn input(mut self) -> Self {
self.out = false;
self
}
}
impl Component for Json {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"json"
}
fn role(&self) -> Role {
if self.out {
Role::Output
} else {
Role::Input
}
}
fn props(&self) -> Value {
json!({
"label": self.label, "value": self.value, "interactive": self.interactive
})
}
}
#[derive(Clone, Debug)]
pub struct File {
id: String,
label: String,
multiple: bool,
types: Vec<String>,
max_size: u64,
interactive: bool,
}
impl File {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
multiple: true,
types: Vec::new(),
max_size: 0,
interactive: true,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn multiple(mut self, on: bool) -> Self {
self.multiple = on;
self
}
pub fn types(mut self, t: &[&str]) -> Self {
self.types = t.iter().map(|s| s.to_string()).collect();
self
}
pub fn max_size(mut self, n: u64) -> Self {
self.max_size = n;
self
}
pub fn interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
}
impl Component for File {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"file"
}
fn role(&self) -> Role {
Role::Input
}
fn props(&self) -> Value {
json!({
"label": self.label, "multiple": self.multiple, "types": self.types,
"max_size": self.max_size, "interactive": self.interactive
})
}
}
#[derive(Clone, Debug)]
pub struct DownloadButton {
id: String,
label: String,
filename: String,
value: String,
}
impl DownloadButton {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
filename: "download.bin".into(),
value: String::new(),
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn filename(mut self, n: impl Into<String>) -> Self {
self.filename = n.into();
self
}
pub fn value(mut self, v: impl Into<String>) -> Self {
self.value = v.into();
self
}
}
impl Component for DownloadButton {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"download"
}
fn role(&self) -> Role {
Role::Output
}
fn props(&self) -> Value {
json!({ "label": self.label, "filename": self.filename, "value": self.value })
}
}
#[derive(Clone, Debug)]
pub struct Code {
id: String,
label: String,
language: Option<String>,
value: String,
interactive: bool,
theme: String,
line_numbers: bool,
out: bool,
}
impl Code {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
language: None,
value: String::new(),
interactive: true,
theme: "auto".to_string(),
line_numbers: true,
out: false,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn language(mut self, l: impl Into<String>) -> Self {
self.language = Some(l.into());
self
}
pub fn value(mut self, v: impl Into<String>) -> Self {
self.value = v.into();
self
}
pub fn interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
pub fn theme(mut self, t: impl Into<String>) -> Self {
self.theme = t.into();
self
}
pub fn lines(mut self, on: bool) -> Self {
self.line_numbers = on;
self
}
pub fn output(mut self) -> Self {
self.out = true;
self
}
pub fn input(mut self) -> Self {
self.out = false;
self
}
}
impl Component for Code {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"code"
}
fn role(&self) -> Role {
if self.out {
Role::Output
} else {
Role::Input
}
}
fn props(&self) -> Value {
json!({
"label": self.label, "language": self.language, "value": self.value,
"interactive": self.interactive, "theme": self.theme,
"line_numbers": self.line_numbers
})
}
}
#[derive(Clone, Debug)]
pub struct Explorer {
id: String,
label: String,
root: String,
pattern: Option<String>,
value: String,
}
impl Explorer {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
root: ".".to_string(),
pattern: None,
value: String::new(),
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn root(mut self, r: impl Into<String>) -> Self {
self.root = r.into();
self
}
pub fn pattern(mut self, p: impl Into<String>) -> Self {
self.pattern = Some(p.into());
self
}
pub fn value(mut self, v: impl Into<String>) -> Self {
self.value = v.into();
self
}
}
impl Component for Explorer {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"explorer"
}
fn role(&self) -> Role {
Role::Input
}
fn props(&self) -> Value {
json!({ "label": self.label, "root": self.root, "pattern": self.pattern, "value": self.value })
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ColumnType {
Text,
Number,
Boolean,
Dropdown(Vec<String>),
}
impl ColumnType {
pub fn to_json(&self) -> Value {
match self {
ColumnType::Text => json!({ "type": "text" }),
ColumnType::Number => json!({ "type": "number" }),
ColumnType::Boolean => json!({ "type": "boolean" }),
ColumnType::Dropdown(choices) => json!({ "type": "dropdown", "choices": choices }),
}
}
}
#[derive(Clone, Debug)]
pub struct ColumnDef {
pub id: String,
pub label: String,
pub col_type: ColumnType,
pub editable: bool,
pub width: Option<u32>,
}
impl ColumnDef {
pub fn new(id: impl Into<String>, label: impl Into<String>, col_type: ColumnType) -> Self {
Self {
id: id.into(),
label: label.into(),
col_type,
editable: true,
width: None,
}
}
pub fn editable(mut self, on: bool) -> Self {
self.editable = on;
self
}
pub fn width(mut self, w: u32) -> Self {
self.width = Some(w);
self
}
pub fn to_json(&self) -> Value {
let mut obj = self.col_type.to_json();
if let Value::Object(ref mut map) = obj {
map.insert("id".into(), json!(self.id));
map.insert("label".into(), json!(self.label));
map.insert("editable".into(), json!(self.editable));
if let Some(w) = self.width {
map.insert("width".into(), json!(w));
}
}
obj
}
}
#[derive(Clone, Debug)]
pub struct DataEditor {
id: String,
label: String,
columns: Vec<ColumnDef>,
data: Vec<Vec<Value>>,
interactive: bool,
allow_add: bool,
allow_delete: bool,
allow_paste: bool,
sortable: bool,
max_height: Option<u32>,
}
impl DataEditor {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
columns: Vec::new(),
data: Vec::new(),
interactive: true,
allow_add: true,
allow_delete: true,
allow_paste: true,
sortable: true,
max_height: None,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn column(
mut self,
id: impl Into<String>,
label: impl Into<String>,
col_type: ColumnType,
) -> Self {
self.columns.push(ColumnDef::new(id, label, col_type));
self
}
pub fn add_column(mut self, col: ColumnDef) -> Self {
self.columns.push(col);
self
}
pub fn data(mut self, rows: Vec<Vec<Value>>) -> Self {
self.data = rows;
self
}
pub fn interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
pub fn allow_add(mut self, on: bool) -> Self {
self.allow_add = on;
self
}
pub fn allow_delete(mut self, on: bool) -> Self {
self.allow_delete = on;
self
}
pub fn allow_paste(mut self, on: bool) -> Self {
self.allow_paste = on;
self
}
pub fn sortable(mut self, on: bool) -> Self {
self.sortable = on;
self
}
pub fn max_height(mut self, h: u32) -> Self {
self.max_height = Some(h);
self
}
}
impl Component for DataEditor {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"dataeditor"
}
fn role(&self) -> Role {
Role::Input
}
fn props(&self) -> Value {
let cols_json: Vec<Value> = self.columns.iter().map(|c| c.to_json()).collect();
json!({
"label": self.label,
"columns": cols_json,
"data": self.data,
"interactive": self.interactive,
"allow_add": self.allow_add,
"allow_delete": self.allow_delete,
"allow_paste": self.allow_paste,
"sortable": self.sortable,
"max_height": self.max_height,
})
}
}
#[derive(Clone, Debug)]
pub struct WebGlPlot {
id: String,
label: String,
title: Option<String>,
xlabel: Option<String>,
ylabel: Option<String>,
colors: Vec<String>,
height: u32,
max_points: usize,
show_fps: bool,
interactive: bool,
initial_series: Vec<WebGlSeries>,
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct WebGlSeries {
pub name: String,
pub color: String,
pub data: Vec<f32>,
}
impl WebGlPlot {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
title: None,
xlabel: None,
ylabel: None,
colors: vec![
"#00f0ff".to_string(),
"#ff007f".to_string(),
"#7000ff".to_string(),
"#00ff66".to_string(),
],
height: 340,
max_points: 200_000,
show_fps: true,
interactive: true,
initial_series: Vec::new(),
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn title(mut self, t: impl Into<String>) -> Self {
self.title = Some(t.into());
self
}
pub fn xlabel(mut self, l: impl Into<String>) -> Self {
self.xlabel = Some(l.into());
self
}
pub fn ylabel(mut self, l: impl Into<String>) -> Self {
self.ylabel = Some(l.into());
self
}
pub fn colors(mut self, cols: &[&str]) -> Self {
self.colors = cols.iter().map(|s| s.to_string()).collect();
self
}
pub fn height(mut self, h: u32) -> Self {
self.height = h;
self
}
pub fn max_points(mut self, n: usize) -> Self {
self.max_points = n;
self
}
pub fn show_fps(mut self, on: bool) -> Self {
self.show_fps = on;
self
}
pub fn interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
pub fn series(
mut self,
name: impl Into<String>,
color: impl Into<String>,
data: &[f32],
) -> Self {
self.initial_series.push(WebGlSeries {
name: name.into(),
color: color.into(),
data: data.to_vec(),
});
self
}
}
impl Component for WebGlPlot {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"webgl_plot"
}
fn role(&self) -> Role {
Role::Output
}
fn props(&self) -> Value {
json!({
"label": self.label,
"title": self.title,
"xlabel": self.xlabel,
"ylabel": self.ylabel,
"colors": self.colors,
"height": self.height,
"max_points": self.max_points,
"show_fps": self.show_fps,
"interactive": self.interactive,
"series": self.initial_series,
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum PivotAggregator {
#[serde(rename = "sum")]
Sum,
#[serde(rename = "mean")]
Mean,
#[serde(rename = "count")]
Count,
#[serde(rename = "min")]
Min,
#[serde(rename = "max")]
Max,
}
impl PivotAggregator {
pub fn as_str(&self) -> &'static str {
match self {
PivotAggregator::Sum => "sum",
PivotAggregator::Mean => "mean",
PivotAggregator::Count => "count",
PivotAggregator::Min => "min",
PivotAggregator::Max => "max",
}
}
}
#[derive(Clone, Debug)]
pub struct PivotTable {
id: String,
label: String,
headers: Vec<String>,
data: Vec<Vec<Value>>,
row_dimensions: Vec<String>,
col_dimensions: Vec<String>,
value_field: Option<String>,
aggregator: PivotAggregator,
height: Option<u32>,
}
impl PivotTable {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
headers: Vec::new(),
data: Vec::new(),
row_dimensions: Vec::new(),
col_dimensions: Vec::new(),
value_field: None,
aggregator: PivotAggregator::Sum,
height: None,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn headers(mut self, h: &[&str]) -> Self {
self.headers = h.iter().map(|s| s.to_string()).collect();
self
}
pub fn data(mut self, rows: Vec<Vec<Value>>) -> Self {
self.data = rows;
self
}
pub fn rows(mut self, r: &[&str]) -> Self {
self.row_dimensions = r.iter().map(|s| s.to_string()).collect();
self
}
pub fn cols(mut self, c: &[&str]) -> Self {
self.col_dimensions = c.iter().map(|s| s.to_string()).collect();
self
}
pub fn value_field(mut self, v: impl Into<String>) -> Self {
self.value_field = Some(v.into());
self
}
pub fn aggregator(mut self, agg: PivotAggregator) -> Self {
self.aggregator = agg;
self
}
pub fn height(mut self, h: u32) -> Self {
self.height = Some(h);
self
}
}
impl Component for PivotTable {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"pivot_table"
}
fn role(&self) -> Role {
Role::Output
}
fn props(&self) -> Value {
json!({
"label": self.label,
"headers": self.headers,
"data": self.data,
"row_dimensions": self.row_dimensions,
"col_dimensions": self.col_dimensions,
"value_field": self.value_field,
"aggregator": self.aggregator.as_str(),
"height": self.height,
})
}
}