Skip to content

Commit 31f2e82

Browse files
authored
Fixes the progress bar for jupyter notebooks (#67)
1 parent 5aeedff commit 31f2e82

File tree

5 files changed

+25
-13
lines changed

5 files changed

+25
-13
lines changed

.github/workflows/build_wheel.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ jobs:
3636
run: |
3737
brew install llvm libomp
3838
echo "export CC=/usr/local/opt/llvm/bin/clang" >> ~/.bashrc
39+
echo "export CXX=/usr/local/opt/llvm/bin/clang++" >> ~/.bashrc
3940
echo "export CFLAGS=\"$CFLAGS -I/usr/local/opt/libomp/include\"" >> ~/.bashrc
4041
echo "export CXXFLAGS=\"$CXXFLAGS -I/usr/local/opt/libomp/include\"" >> ~/.bashrc
4142
echo "export LDFLAGS=\"$LDFLAGS -Wl,-rpath,/usr/local/opt/libomp/lib -L/usr/local/opt/libomp/lib -lomp\"" >> ~/.bashrc
@@ -45,6 +46,7 @@ jobs:
4546
run: |
4647
brew install llvm libomp
4748
echo "export CC=/opt/homebrew/opt/llvm/bin/clang" >> ~/.bashrc
49+
echo "export CXX=/opt/homebrew/opt/llvm/bin/clang++" >> ~/.bashrc
4850
echo "export CFLAGS=\"$CFLAGS -I/opt/homebrew/opt/libomp/include\"" >> ~/.bashrc
4951
echo "export CXXFLAGS=\"$CXXFLAGS -I/opt/homebrew/opt/libomp/include\"" >> ~/.bashrc
5052
echo "export LDFLAGS=\"$LDFLAGS -Wl,-rpath,/opt/homebrew/opt/libomp/lib -L/opt/homebrew/opt/libomp/lib -lomp\"" >> ~/.bashrc

.github/workflows/run_tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ jobs:
4040
run: |
4141
brew install llvm libomp
4242
echo "export CC=/usr/local/opt/llvm/bin/clang" >> ~/.bashrc
43+
echo "export CXX=/usr/local/opt/llvm/bin/clang++" >> ~/.bashrc
4344
echo "export CFLAGS=\"$CFLAGS -I/usr/local/opt/libomp/include\"" >> ~/.bashrc
4445
echo "export CXXFLAGS=\"$CXXFLAGS -I/usr/local/opt/libomp/include\"" >> ~/.bashrc
4546
echo "export LDFLAGS=\"$LDFLAGS -Wl,-rpath,/usr/local/opt/libomp/lib -L/usr/local/opt/libomp/lib -lomp\"" >> ~/.bashrc

RATapi/run.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import time
22

3-
from tqdm import tqdm
3+
from tqdm.auto import tqdm
44

55
import RATapi.rat_core
66
from RATapi.inputs import make_input
@@ -16,18 +16,23 @@ class ProgressBar:
1616
----------
1717
display : bool, default: True
1818
Indicates if displaying is allowed
19-
2019
"""
2120

2221
def __init__(self, display=True):
22+
self.pbar = None
2323
self.display = display
24+
self.tqdm_kwargs = {"total": 100, "desc": "", "bar_format": "{l_bar}{bar}", "disable": not self.display}
25+
# Determine if the auto tqdm is standard or notebook
26+
from tqdm.asyncio import tqdm as asyncio_tqdm
27+
28+
if tqdm == asyncio_tqdm:
29+
self.tqdm_kwargs.update({"ncols": 90})
2430

2531
def __enter__(self):
2632
if self.display:
2733
RATapi.events.register(RATapi.events.EventTypes.Progress, self.updateProgress)
28-
self.pbar = tqdm(total=100, desc="", delay=1, bar_format="{l_bar}{bar}", ncols=90, disable=not self.display)
29-
self.pbar.delay = 0
30-
return self.pbar
34+
35+
return self
3136

3237
def updateProgress(self, event):
3338
"""Callback for the progress event.
@@ -37,13 +42,17 @@ def updateProgress(self, event):
3742
event: ProgressEventData
3843
The progress event data.
3944
"""
40-
45+
if self.pbar is None:
46+
self.pbar = tqdm(**self.tqdm_kwargs)
4147
value = event.percent * 100
4248
self.pbar.desc = event.message
4349
self.pbar.update(value - self.pbar.n)
4450

4551
def __exit__(self, _exc_type, _exc_val, _traceback):
46-
self.pbar.leave = False
52+
if self.pbar is not None:
53+
self.pbar.close()
54+
print("") # Print new line after bar
55+
4756
if self.display:
4857
RATapi.events.clear(RATapi.events.EventTypes.Progress, self.updateProgress)
4958

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def build_libraries(self, libraries):
177177
':python_version < "3.11"': ["StrEnum >= 0.4.15"],
178178
"Dev": ["pytest>=7.4.0", "pytest-cov>=4.1.0", "ruff>=0.4.10"],
179179
"Matlab_latest": ["matlabengine"],
180-
"Matlab_2023b": ["matlabengine == 23.2.1"],
180+
"Matlab_2023b": ["matlabengine == 23.2.3"],
181181
"Matlab_2023a": ["matlabengine == 9.14.3"],
182182
"Matlab_2022b": ["matlabengine == 9.13.9"],
183183
"Matlab_2022a": ["matlabengine == 9.12.19"],

tests/test_run.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,13 +329,13 @@ def test_progress_bar() -> None:
329329
event.message = "TESTING"
330330
event.percent = 0.2
331331
with ProgressBar() as bar:
332-
assert bar.n == 0
332+
assert bar.pbar is None
333333
notify(EventTypes.Progress, event)
334-
assert bar.desc == "TESTING"
335-
assert bar.n == 20
334+
assert bar.pbar.desc == "TESTING"
335+
assert bar.pbar.n == 20
336336

337337
event.message = "AGAIN"
338338
event.percent = 0.6
339339
notify(EventTypes.Progress, event)
340-
assert bar.desc == "AGAIN"
341-
assert bar.n == 60
340+
assert bar.pbar.desc == "AGAIN"
341+
assert bar.pbar.n == 60

0 commit comments

Comments
 (0)