Skip to content

Commit 5c9db81

Browse files
authored
Merge pull request #931 from minrk/colors
misc IPython 9 compatibility
2 parents d9670ba + 943fa50 commit 5c9db81

File tree

6 files changed

+49
-16
lines changed

6 files changed

+49
-16
lines changed

ipyparallel/client/client.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
from ipyparallel import error, serialize, util
5050
from ipyparallel.serialize import PrePickled, Reference
5151
from ipyparallel.util import _OutputProducingThread as Thread
52+
from ipyparallel.util import _TermColors
5253

5354
from .asyncresult import AsyncHubResult, AsyncResult
5455
from .futures import MessageFuture, multi_future
@@ -161,11 +162,30 @@ def _plaintext(self) -> str:
161162
if not text_out:
162163
return ''
163164

165+
ip = get_ipython()
166+
if ip is None:
167+
colors = "NoColor"
168+
else:
169+
colors = ip.colors
170+
171+
if colors.lower() == "nocolor":
172+
out = normal = ""
173+
else:
174+
out = _TermColors.Red
175+
normal = _TermColors.Normal
176+
164177
if '\n' in text_out and not text_out.startswith('\n'):
165178
# add newline for multiline reprs
166179
text_out = '\n' + text_out
167180

168-
return f"Out[{self.metadata['engine_id']}:{self.execution_count}]: {text_out}"
181+
return ''.join(
182+
[
183+
out,
184+
f"Out[{self.metadata['engine_id']}:{self.execution_count}]: ",
185+
normal,
186+
text_out,
187+
]
188+
)
169189

170190
def _repr_pretty_(self, p, cycle):
171191
p.text(self._plaintext())

ipyparallel/engine/app.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,8 @@ def send_with_metadata(
758758
if self.use_mpi and self.init_mpi:
759759
app.exec_lines.insert(0, self.init_mpi)
760760
app.init_profile_dir()
761+
app.init_gui_pylab()
762+
app.init_extensions()
761763
app.init_code()
762764

763765
# redirect output at the end, only after start is called

ipyparallel/tests/clienttest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def generate_output():
5555
a rich displayable object.
5656
"""
5757

58-
from IPython.core.display import HTML, Math, display
58+
from IPython.display import HTML, Math, display
5959

6060
print("stdout")
6161
print("stderr", file=sys.stderr)

ipyparallel/tests/test_magics.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,18 @@ def test_cellpx_groupby_order(self):
173173
expected.extend(
174174
[
175175
r'\[output:\d+\]',
176-
'IPython.core.display.HTML',
176+
'IPython..+.HTML',
177177
]
178178
* len(v)
179179
)
180180
expected.extend(
181181
[
182182
r'\[output:\d+\]',
183-
'IPython.core.display.Math',
183+
'IPython..+.Math',
184184
]
185185
* len(v)
186186
)
187-
expected.extend([r'Out\[\d+:\d+\]:.*IPython\.core\.display\.Math'] * len(v))
187+
expected.extend([r'Out\[\d+:\d+\]:.*IPython\..+\.Math'] * len(v))
188188

189189
assert len(lines), len(expected) == io.stdout
190190
for line, expect in zip(lines, expected):
@@ -440,24 +440,28 @@ def test_result(self):
440440
ip.run_line_magic('pxresult', '')
441441
assert str(data[name]) in io.stdout
442442

443-
def test_px_pylab(self):
444-
"""%pylab works on engines"""
443+
def test_px_matplotlib(self):
444+
"""%matplotlib inline works on engines"""
445445
pytest.importorskip('matplotlib')
446446
ip = get_ipython()
447447
v = self.client[-1]
448448
v.block = True
449449
v.activate()
450450

451451
with capture_output() as io:
452-
ip.run_line_magic("px", "%pylab inline")
453-
454-
assert (
455-
"Populating the interactive namespace from numpy and matplotlib"
456-
in io.stdout
457-
)
452+
ip.run_line_magic(
453+
"px",
454+
"\n".join(
455+
[
456+
"%matplotlib inline",
457+
"import numpy as np",
458+
"import matplotlib.pyplot as plt",
459+
]
460+
),
461+
)
458462

459463
with capture_output(display=False) as io:
460-
ip.run_line_magic("px", "plot(rand(100))")
464+
ip.run_line_magic("px", "plt.plot(np.random.rand(100))")
461465
assert 'Out[' in io.stdout
462466
assert 'matplotlib.lines' in io.stdout
463467

ipyparallel/tests/test_view.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ def test_execute_magic(self):
614614
def test_execute_displaypub(self):
615615
"""execute tracks display_pub output"""
616616
view = self.client[:]
617-
view.execute("from IPython.core.display import *")
617+
view.execute("from IPython.display import *")
618618
ar = view.execute("[ display(i) for i in range(5) ]", block=True)
619619

620620
expected = [{'text/plain': str(j)} for j in range(5)]
@@ -625,7 +625,7 @@ def test_execute_displaypub(self):
625625
def test_apply_displaypub(self):
626626
"""apply tracks display_pub output"""
627627
view = self.client[:]
628-
view.execute("from IPython.core.display import *")
628+
view.execute("from IPython.display import *")
629629

630630
@interactive
631631
def publish():

ipyparallel/util.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,3 +836,10 @@ def __init__(self, target, **kwargs):
836836
def _wrapped_target(self, target, *args, **kwargs):
837837
_detach_thread_output(self.ident)
838838
return target(*args, **kwargs)
839+
840+
841+
# minimal subset of TermColors, removed from IPython
842+
# not for public consumption
843+
class _TermColors:
844+
Normal = '\033[0m'
845+
Red = '\033[0;31m'

0 commit comments

Comments
 (0)