|
10 | 10 | import os |
11 | 11 | import random |
12 | 12 | import socket |
| 13 | +import threading |
| 14 | +import types |
13 | 15 |
|
14 | 16 | import numpy as np |
15 | 17 |
|
|
30 | 32 | from matplotlib._pylab_helpers import Gcf |
31 | 33 | from matplotlib import _png |
32 | 34 |
|
| 35 | +# TODO: This should really only be set for the IPython notebook, but |
| 36 | +# I'm not sure how to detect that. |
| 37 | +try: |
| 38 | + __IPYTHON__ |
| 39 | +except: |
| 40 | + _in_ipython = False |
| 41 | +else: |
| 42 | + _in_ipython = True |
| 43 | + |
33 | 44 |
|
34 | 45 | def draw_if_interactive(): |
35 | 46 | """ |
@@ -57,7 +68,30 @@ def mainloop(self): |
57 | 68 |
|
58 | 69 | WebAggApplication.start() |
59 | 70 |
|
60 | | -show = Show() |
| 71 | + |
| 72 | +if not _in_ipython: |
| 73 | + show = Show() |
| 74 | +else: |
| 75 | + def show(): |
| 76 | + class RawHTML(object): |
| 77 | + def __init__(self, content): |
| 78 | + self._content = content |
| 79 | + |
| 80 | + def _repr_html_(self): |
| 81 | + return self._content |
| 82 | + |
| 83 | + result = [] |
| 84 | + import matplotlib._pylab_helpers as pylab_helpers |
| 85 | + for manager in pylab_helpers.Gcf().get_all_fig_managers(): |
| 86 | + result.append(ipython_inline_display(manager.canvas.figure)) |
| 87 | + return RawHTML('\n'.join(result)) |
| 88 | + |
| 89 | + |
| 90 | +class ServerThread(threading.Thread): |
| 91 | + def run(self): |
| 92 | + tornado.ioloop.IOLoop.instance().start() |
| 93 | + |
| 94 | +server_thread = ServerThread() |
61 | 95 |
|
62 | 96 |
|
63 | 97 | def new_figure_manager(num, *args, **kwargs): |
@@ -127,6 +161,16 @@ def __init__(self, *args, **kwargs): |
127 | 161 | # messages from piling up. |
128 | 162 | self._pending_draw = None |
129 | 163 |
|
| 164 | + # TODO: I'd like to dynamically add the _repr_html_ method |
| 165 | + # to the figure in the right context, but then IPython doesn't |
| 166 | + # use it, for some reason. |
| 167 | + |
| 168 | + # Add the _repr_html_ member to the figure for IPython inline |
| 169 | + # support |
| 170 | + # if _in_ipython: |
| 171 | + # self.figure._repr_html_ = types.MethodType( |
| 172 | + # ipython_inline_display, self.figure, self.figure.__class__) |
| 173 | + |
130 | 174 | def show(self): |
131 | 175 | # show the figure window |
132 | 176 | show() |
@@ -199,7 +243,7 @@ def get_diff_image(self): |
199 | 243 | self._png_is_old = False |
200 | 244 | return self._png_buffer.getvalue() |
201 | 245 |
|
202 | | - def get_renderer(self): |
| 246 | + def get_renderer(self, cleared=None): |
203 | 247 | # Mirrors super.get_renderer, but caches the old one |
204 | 248 | # so that we can do things such as prodce a diff image |
205 | 249 | # in get_diff_image |
@@ -253,7 +297,6 @@ def handle_event(self, event): |
253 | 297 | elif e_type == 'key_release': |
254 | 298 | self.key_release_event(key) |
255 | 299 | elif e_type == 'toolbar_button': |
256 | | - print('Toolbar button pressed: ', event['name']) |
257 | 300 | # TODO: Be more suspicious of the input |
258 | 301 | getattr(self.toolbar, event['name'])() |
259 | 302 | elif e_type == 'refresh': |
@@ -435,6 +478,8 @@ def get(self): |
435 | 478 | manager = Gcf.get_fig_manager(1) |
436 | 479 | canvas = manager.canvas |
437 | 480 |
|
| 481 | + self.set_header('Content-Type', 'application/javascript') |
| 482 | + |
438 | 483 | t = tornado.template.Template(tpl) |
439 | 484 | self.write(t.generate( |
440 | 485 | toolitems=NavigationToolbar2WebAgg.toolitems, |
@@ -614,3 +659,27 @@ def start(cls): |
614 | 659 | print("Server stopped") |
615 | 660 |
|
616 | 661 | cls.started = True |
| 662 | + |
| 663 | + |
| 664 | +def ipython_inline_display(figure): |
| 665 | + import matplotlib._pylab_helpers as pylab_helpers |
| 666 | + |
| 667 | + WebAggApplication.initialize() |
| 668 | + if not server_thread.is_alive(): |
| 669 | + server_thread.start() |
| 670 | + |
| 671 | + with open(os.path.join( |
| 672 | + WebAggApplication._mpl_dirs['web_backend'], |
| 673 | + 'ipython_inline_figure.html')) as fd: |
| 674 | + tpl = fd.read() |
| 675 | + |
| 676 | + fignum = figure.number |
| 677 | + manager = pylab_helpers.Gcf.get_fig_manager(fignum) |
| 678 | + |
| 679 | + t = tornado.template.Template(tpl) |
| 680 | + return t.generate( |
| 681 | + prefix=WebAggApplication.url_prefix, |
| 682 | + fig_id=fignum, |
| 683 | + toolitems=NavigationToolbar2WebAgg.toolitems, |
| 684 | + canvas=figure.canvas, |
| 685 | + port=WebAggApplication.port) |
0 commit comments