Skip to content

Commit 256d956

Browse files
committed
Disable tqdm for IDLE environments
1 parent 42cfe6c commit 256d956

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

src/turtlethread/visualise.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,32 @@
66

77
USE_SPHINX_GALLERY = False
88

9+
# Check if in IDLE, disable if so
10+
import sys
11+
12+
class non_idle_tqdm:
13+
def __init__(self, iterable, *args, **kwargs):
14+
self.iterable = iterable
15+
if 'idlelib.run' not in sys.modules:
16+
self.tqdm = tqdm
17+
else:
18+
self.tqdm = None
19+
20+
def __iter__(self):
21+
if self.tqdm is not None:
22+
return iter(self.tqdm(self.iterable))
23+
else:
24+
return iter(self.iterable)
25+
26+
class no_tqdm:
27+
def __init__(self, iterable, *args, **kwargs):
28+
self.iterable = iterable
29+
30+
def __iter__(self):
31+
return iter(self.iterable)
32+
33+
def __call__(self, *args, **kwargs):
34+
return iter(self.iterable)
935

1036
def get_dimensions(stitches):
1137
if len(stitches) == 0: return 0, 0
@@ -216,11 +242,15 @@ def visualise_pattern(pattern, turtle=None, width=800, height=800, scale=1, spee
216242
raise_error = False
217243
threads = list(pattern.threadlist)
218244
thread_idx = 0
245+
progressbar = non_idle_tqdm
219246
if skip:
220247
print("Rendering... (show animation with skip=False)")
248+
progressbar = no_tqdm
221249
else:
222250
print("Visualising... (skip with skip=True)")
223-
for x, y, command in tqdm(pattern.stitches):
251+
if len(pattern.stitches) < 50:
252+
progressbar = no_tqdm
253+
for x, y, command in progressbar(pattern.stitches):
224254
x = scale * x
225255
y = scale * y
226256
if command == JUMP:
@@ -304,7 +334,10 @@ def density(stitches):
304334
def density_from_points(pts, dist=0.5, num=20):
305335
print("Checking density of stitches... (skip with check_density=False)")
306336
adjmat = [ [ -1 for j in range(len(pts))] for i in range(len(pts))]
307-
for i in tqdm(range(len(pts))):
337+
progressbar = non_idle_tqdm
338+
if len(pts) < 5000:
339+
progressbar = no_tqdm
340+
for i in progressbar(range(len(pts))):
308341
adjmat[i][i] = 1 # close enough
309342
for j in range(i+1, len(pts)):
310343
dx = pts[i][0] - pts[j][0]

0 commit comments

Comments
 (0)