|
14 | 14 |
|
15 | 15 | import atexit |
16 | 16 | import datetime |
| 17 | +import functools |
17 | 18 | import json |
18 | 19 | import logging |
19 | 20 | import os |
|
25 | 26 | import uuid |
26 | 27 | import tqdm |
27 | 28 | from packaging import version |
28 | | -from tqdm import tqdm as cli_tqdm |
29 | | -from tqdm.notebook import tqdm as notebook_tqdm |
30 | 29 | from types import MethodType |
31 | 30 | from typing import Any, cast, ClassVar, Dict, Iterable, Optional, Union |
32 | 31 |
|
@@ -991,6 +990,28 @@ def clearProgressHandlers_wrapper_method(_, *args, **kwargs): |
991 | 990 | clearProgressHandlers_wrapper_method, self |
992 | 991 | ) |
993 | 992 |
|
| 993 | + @staticmethod |
| 994 | + @functools.lru_cache(maxsize=1) |
| 995 | + def get_tqdm_bar(): |
| 996 | + """ |
| 997 | + Return a tqdm implementation that works in the current environment. |
| 998 | +
|
| 999 | + - Uses CLI tqdm for interactive terminals. |
| 1000 | + - Uses the notebook tqdm if available, otherwise falls back to CLI tqdm. |
| 1001 | + """ |
| 1002 | + from tqdm import tqdm as cli_tqdm |
| 1003 | + |
| 1004 | + if environment.is_interactive_terminal(): |
| 1005 | + return cli_tqdm |
| 1006 | + |
| 1007 | + try: |
| 1008 | + import ipywidgets |
| 1009 | + from tqdm.notebook import tqdm as notebook_tqdm |
| 1010 | + |
| 1011 | + return notebook_tqdm |
| 1012 | + except ImportError: |
| 1013 | + return cli_tqdm |
| 1014 | + |
994 | 1015 | def _register_progress_execution_handler(self): |
995 | 1016 | from pyspark.sql.connect.shell.progress import StageInfo |
996 | 1017 |
|
@@ -1019,9 +1040,8 @@ def handler( |
1019 | 1040 | if total_tasks == 0: |
1020 | 1041 | return |
1021 | 1042 |
|
1022 | | - tqdm_pbar = notebook_tqdm |
1023 | | - if environment.is_interactive_terminal(): |
1024 | | - tqdm_pbar = cli_tqdm |
| 1043 | + # Get correct tqdm (notebook or CLI) |
| 1044 | + tqdm_pbar = self.get_tqdm_bar() |
1025 | 1045 |
|
1026 | 1046 | # Use a lock to ensure only one thread can access and modify |
1027 | 1047 | # the shared dictionaries at a time. |
|
0 commit comments