1+ """
2+ PythonDisplay()
3+
4+ Like TextDisplay() but prints to Python's stdout.
5+ """
6+ struct PythonDisplay <: AbstractDisplay end
7+
8+ function Base. display (d:: PythonDisplay , m:: MIME , @nospecialize (x))
9+ istextmime (m) || throw (MethodError (display, (d, m, x)))
10+ buf = IOBuffer ()
11+ io = IOContext (buf, :limit => true )
12+ try
13+ show (io, m, x)
14+ catch
15+ throw (MethodError (display, (d, m, x)))
16+ end
17+ data = String (take! (buf))
18+ pyprint (data)
19+ return
20+ end
21+
22+ function Base. display (d:: PythonDisplay , @nospecialize (x))
23+ display (d, MIME (" text/plain" ), x)
24+ end
25+
26+ """
27+ IPythonDisplay()
28+
29+ For displaying multimedia with IPython's display mechanism.
30+ """
131struct IPythonDisplay <: AbstractDisplay end
232
333function Base. display (d:: IPythonDisplay , m:: MIME , @nospecialize (x))
434 ipy = pyimport (" IPython" )
535 buf = IOBuffer ()
36+ io = IOContext (buf, :limit => true )
637 dict = pydict ()
738 try
8- show (buf , m, x)
39+ show (io , m, x)
940 catch
1041 throw (MethodError (display, (d, m, x)))
1142 end
@@ -22,10 +53,11 @@ function Base.display(d::IPythonDisplay, @nospecialize(x))
2253 return
2354 end
2455 buf = IOBuffer ()
56+ io = IOContext (buf, :limit => true )
2557 dict = pydict ()
2658 for m in Utils. mimes_for (x)
2759 try
28- show (buf , MIME (m), x)
60+ show (io , MIME (m), x)
2961 catch
3062 continue
3163 end
@@ -42,10 +74,7 @@ function init_ipython()
4274 if C. CTX. is_embedded && CONFIG. auto_ipython_display
4375 is_ipython = (" IPython" in pysysmodule. modules) && ! pyisnone (pysysmodule. modules[" IPython" ]. get_ipython ())
4476 if is_ipython
45- # We used to set `Base.stdout` to `sys.stdout` and ensure it is flushed after each execution.
46- # But `Base.stdout` is expected to be a "real" file in some places (e.g. when spawning tasks).
47- # set displays so that Base.display() renders in ipython
48- # pushdisplay(TextDisplay(Base.stdout))
77+ pushdisplay (PythonDisplay ())
4978 pushdisplay (IPythonDisplay ())
5079 end
5180 end
0 commit comments