Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkircos committed Feb 11, 2025
2 parents a8de3aa + d36774b commit 26d685f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
21 changes: 16 additions & 5 deletions quadratic-core/src/a1/a1_selection/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,19 @@ impl A1Selection {
/// Used to trigger Python get_cells call to return a DataFrame with the
/// appropriate column headers. This is true if the selection is a table and
/// data is included.
pub fn has_table_headers(&self) -> bool {
pub fn has_table_headers(&self, context: &A1Context, is_python: bool) -> bool {
if self.ranges.len() != 1 {
return false;
}

if let Some(CellRefRange::Table { range }) = self.ranges.first() {
if is_python {
let show_columns = context
.try_table(&range.table_name)
.map_or(false, |table| table.show_columns);
return show_columns || range.headers;
}

range.data || range.headers
} else {
false
Expand Down Expand Up @@ -825,10 +833,13 @@ mod tests {
#[test]
fn test_has_table_headers() {
let context = A1Context::test(&[], &[("Table1", &["A", "B"], Rect::test_a1("A1:B2"))]);
assert!(A1Selection::test_a1_context("Table1", &context).has_table_headers());
assert!(A1Selection::test_a1_context("Table1[#ALL]", &context).has_table_headers());
assert!(A1Selection::test_a1_context("Table1[#headers]", &context).has_table_headers());
assert!(A1Selection::test_a1_context("Table1[#all]", &context).has_table_headers());
assert!(A1Selection::test_a1_context("Table1", &context).has_table_headers(&context, false));
assert!(A1Selection::test_a1_context("Table1[#ALL]", &context)
.has_table_headers(&context, false));
assert!(A1Selection::test_a1_context("Table1[#headers]", &context)
.has_table_headers(&context, false));
assert!(A1Selection::test_a1_context("Table1[#all]", &context)
.has_table_headers(&context, false));
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,18 @@ impl GridController {
} else {
false
};

let cells = selection_sheet.get_cells_response(*rect);
let is_python = matches!(code.language, CodeCellLanguage::Python);

CellA1Response {
cells,
x: rect.min.x,
y: rect.min.y,
w: rect.width() as i64,
h: rect.height() as i64,
two_dimensional,
has_headers: selection.has_table_headers(),
has_headers: selection.has_table_headers(&context, is_python),
}
} else {
CellA1Response {
Expand Down
23 changes: 3 additions & 20 deletions quadratic-core/src/controller/execution/run_code/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ impl GridController {
pub(super) fn js_code_result_to_code_cell_value(
&mut self,
transaction: &mut PendingTransaction,
mut js_code_result: JsCodeResult,
js_code_result: JsCodeResult,
start: SheetPos,
language: CodeCellLanguage,
) -> DataTable {
Expand Down Expand Up @@ -409,20 +409,7 @@ impl GridController {
};

let value = if js_code_result.success {
if let Some(mut array_output) = js_code_result.output_array {
// if the output is a dataframe of headers only, we want to convert it to a list
let is_headers_only = js_code_result.has_headers && array_output.len() == 1;
if is_headers_only && js_code_result.output_display_type == Some("DataFrame".into())
{
js_code_result.has_headers = false;
js_code_result.output_display_type = Some("list".into());
array_output = array_output[0]
.clone()
.into_iter()
.map(|row| vec![row])
.collect();
}

if let Some(array_output) = js_code_result.output_array {
let (array, ops) = Array::from_string_list(start.into(), sheet, array_output);
transaction.reverse_operations.extend(ops);
if let Some(array) = array {
Expand Down Expand Up @@ -488,11 +475,7 @@ impl GridController {
);

transaction.cells_accessed.clear();

data_table.show_columns = match language {
CodeCellLanguage::Javascript => data_table.width() > 1,
_ => js_code_result.has_headers,
};
data_table.show_columns = js_code_result.has_headers;

// If no headers were returned, we want column headers: [0, 2, 3, ...etc]
if !js_code_result.has_headers {
Expand Down

0 comments on commit 26d685f

Please sign in to comment.