GeezerVison is showing github.com — feat: Disable progress bars in Anywidget mode to reduce notebook clut… · google/bigframes@853240d
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: bigframes/core/compile/polars/compiler.py +63-1Lines changed: 63 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 16 | 16 | import dataclasses | 17 | 17 | import functools | 18 | 18 | import itertools | | 19 | +import json | 19 | 20 | from typing import cast, Literal, Optional, Sequence, Tuple, Type, TYPE_CHECKING | 20 | 21 |
| 21 | 22 | import pandas as pd | @@ -429,7 +430,68 @@ def _(self, op: ops.ScalarOp, input: pl.Expr) -> pl.Expr: | 429 | 430 | @compile_op.register(json_ops.JSONDecode) | 430 | 431 | def _(self, op: ops.ScalarOp, input: pl.Expr) -> pl.Expr: | 431 | 432 | assert isinstance(op, json_ops.JSONDecode) | 432 | | - return input.str.json_decode(_DTYPE_MAPPING[op.to_type]) | | 433 | + target_dtype = _bigframes_dtype_to_polars_dtype(op.to_type) | | 434 | + if op.safe: | | 435 | + # Polars does not support safe JSON decoding (returning null on failure). | | 436 | + # We use map_elements to provide safe JSON decoding. | | 437 | + def safe_decode(val): | | 438 | + if val is None: | | 439 | + return None | | 440 | + try: | | 441 | + decoded = json.loads(val) | | 442 | + except Exception: | | 443 | + return None | | 444 | + | | 445 | + if decoded is None: | | 446 | + return None | | 447 | + | | 448 | + if op.to_type == bigframes.dtypes.INT_DTYPE: | | 449 | + if type(decoded) is bool: | | 450 | + return None | | 451 | + if isinstance(decoded, int): | | 452 | + return decoded | | 453 | + if isinstance(decoded, float): | | 454 | + if decoded.is_integer(): | | 455 | + return int(decoded) | | 456 | + if isinstance(decoded, str): | | 457 | + try: | | 458 | + return int(decoded) | | 459 | + except Exception: | | 460 | + pass | | 461 | + return None | | 462 | + | | 463 | + if op.to_type == bigframes.dtypes.FLOAT_DTYPE: | | 464 | + if type(decoded) is bool: | | 465 | + return None | | 466 | + if isinstance(decoded, (int, float)): | | 467 | + return float(decoded) | | 468 | + if isinstance(decoded, str): | | 469 | + try: | | 470 | + return float(decoded) | | 471 | + except Exception: | | 472 | + pass | | 473 | + return None | | 474 | + | | 475 | + if op.to_type == bigframes.dtypes.BOOL_DTYPE: | | 476 | + if isinstance(decoded, bool): | | 477 | + return decoded | | 478 | + if isinstance(decoded, str): | | 479 | + if decoded.lower() == "true": | | 480 | + return True | | 481 | + if decoded.lower() == "false": | | 482 | + return False | | 483 | + return None | | 484 | + | | 485 | + if op.to_type == bigframes.dtypes.STRING_DTYPE: | | 486 | + if isinstance(decoded, str): | | 487 | + return decoded | | 488 | + return None | | 489 | + | | 490 | + return decoded | | 491 | + | | 492 | + return input.map_elements(safe_decode, return_dtype=target_dtype) | | 493 | + | | 494 | + return input.str.json_decode(target_dtype) | 433 | 495 |
| 434 | 496 | @compile_op.register(arr_ops.ToArrayOp) | 435 | 497 | def _(self, op: ops.ToArrayOp, *inputs: pl.Expr) -> pl.Expr: | |
Collapse file Copy file name to clipboardExpand all lines: bigframes/core/compile/polars/lowering.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -391,7 +391,7 @@ def _lower_cast(cast_op: ops.AsTypeOp, arg: expression.Expression): | 391 | 391 | return arg | 392 | 392 |
| 393 | 393 | if arg.output_type == dtypes.JSON_DTYPE: | 394 | | - return json_ops.JSONDecode(cast_op.to_type).as_expr(arg) | | 394 | + return json_ops.JSONDecode(cast_op.to_type, safe=cast_op.safe).as_expr(arg) | 395 | 395 | if ( | 396 | 396 | arg.output_type == dtypes.STRING_DTYPE | 397 | 397 | and cast_op.to_type == dtypes.DATETIME_DTYPE | |
Collapse file Copy file name to clipboardExpand all lines: bigframes/display/anywidget.py +21-18Lines changed: 21 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -133,25 +133,26 @@ def _initial_load(self) -> None: | 133 | 133 | # obtain the row counts | 134 | 134 | # TODO(b/428238610): Start iterating over the result of `to_pandas_batches()` | 135 | 135 | # before we get here so that the count might already be cached. | 136 | | - self._reset_batches_for_new_page_size() | | 136 | + with bigframes.option_context("display.progress_bar", None): | | 137 | + self._reset_batches_for_new_page_size() | 137 | 138 |
| 138 | | - if self._batches is None: | 139 | | - self._error_message = ( | 140 | | - "Could not retrieve data batches. Data might be unavailable or " | 141 | | - "an error occurred." | 142 | | - ) | 143 | | - self.row_count = None | 144 | | - elif self._batches.total_rows is None: | 145 | | - # Total rows is unknown, this is an expected state. | 146 | | - # TODO(b/461536343): Cheaply discover if we have exactly 1 page. | 147 | | - # There are cases where total rows is not set, but there are no additional | 148 | | - # pages. We could disable the "next" button in these cases. | 149 | | - self.row_count = None | 150 | | - else: | 151 | | - self.row_count = self._batches.total_rows | | 139 | + if self._batches is None: | | 140 | + self._error_message = ( | | 141 | + "Could not retrieve data batches. Data might be unavailable or " | | 142 | + "an error occurred." | | 143 | + ) | | 144 | + self.row_count = None | | 145 | + elif self._batches.total_rows is None: | | 146 | + # Total rows is unknown, this is an expected state. | | 147 | + # TODO(b/461536343): Cheaply discover if we have exactly 1 page. | | 148 | + # There are cases where total rows is not set, but there are no additional | | 149 | + # pages. We could disable the "next" button in these cases. | | 150 | + self.row_count = None | | 151 | + else: | | 152 | + self.row_count = self._batches.total_rows | 152 | 153 |
| 153 | | - # get the initial page | 154 | | - self._set_table_html() | | 154 | + # get the initial page | | 155 | + self._set_table_html() | 155 | 156 |
| 156 | 157 | @traitlets.observe("_initial_load_complete") | 157 | 158 | def _on_initial_load_complete(self, change: dict[str, Any]): | @@ -281,7 +282,9 @@ def _reset_batches_for_new_page_size(self) -> None: | 281 | 282 | def _set_table_html(self) -> None: | 282 | 283 | """Sets the current html data based on the current page and page size.""" | 283 | 284 | new_page = None | 284 | | - with self._setting_html_lock: | | 285 | + with self._setting_html_lock, bigframes.option_context( | | 286 | + "display.progress_bar", None | | 287 | + ): | 285 | 288 | if self._error_message: | 286 | 289 | self.table_html = ( | 287 | 290 | f"<div class='bigframes-error-message'>" | |
Collapse file Copy file name to clipboardExpand all lines: bigframes/display/html.py +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -363,7 +363,8 @@ def repr_mimebundle( | 363 | 363 |
| 364 | 364 | if opts.repr_mode == "anywidget": | 365 | 365 | try: | 366 | | - return get_anywidget_bundle(obj, include=include, exclude=exclude) | | 366 | + with bigframes.option_context("display.progress_bar", None): | | 367 | + return get_anywidget_bundle(obj, include=include, exclude=exclude) | 367 | 368 | except ImportError: | 368 | 369 | # Anywidget is an optional dependency, so warn rather than fail. | 369 | 370 | # TODO(shuowei): When Anywidget becomes the default for all repr modes, | |
Collapse file Copy file name to clipboardExpand all lines: bigframes/operations/json_ops.py +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -220,6 +220,7 @@ def output_type(self, *input_types): | 220 | 220 | class JSONDecode(base_ops.UnaryOp): | 221 | 221 | name: typing.ClassVar[str] = "json_decode" | 222 | 222 | to_type: dtypes.Dtype | | 223 | + safe: bool = False | 223 | 224 |
| 224 | 225 | def output_type(self, *input_types): | 225 | 226 | input_type = input_types[0] | |
Collapse file Copy file name to clipboardExpand all lines: bigframes/session/polars_executor.py +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 34 | 34 | numeric_ops, | 35 | 35 | string_ops, | 36 | 36 | ) | | 37 | +import bigframes.operations.json_ops as json_ops | 37 | 38 | from bigframes.session import executor, semi_executor | 38 | 39 |
| 39 | 40 | if TYPE_CHECKING: | | 94 | 95 | string_ops.EndsWithOp, | 95 | 96 | string_ops.StrContainsOp, | 96 | 97 | string_ops.StrContainsRegexOp, | | 98 | + json_ops.JSONDecode, | 97 | 99 | ) | 98 | 100 | _COMPATIBLE_AGG_OPS = ( | 99 | 101 | agg_ops.SizeOp, | |
|