288914b: viewrendered plugin supports @pyplot nodes

54 views
Skip to first unread message

Edward K. Ream

unread,
Sep 28, 2016, 10:55:19 AM9/28/16
to leo-editor
pyplot is the python interface to matplotlib.  Imo, better pyplot support will give Leo most of the capabilities of the Jupyter notebook.

This project came about as the result of a thought experiment, namely using Jupyter as a prototype for Leo as a web app.  But I soon realized it would be a whole lot easier to add pyplot support to Leo than to add all of Leo to Jupyter ;-)

Here is the checkin log:

QQQQQ
Added preliminary support for @pyplot drawing in viewrendered plugin:

- @pyplot nodes are assumed to contain pyplot scripts.
   The update code automagically imports the following, so scripts don't have to:
  
        import matplotlib
        import matplotlib.pyplot as plt
        import numpy as np
        import matplotlib.animation as animation
   
- Added leo/plugins/pyplot_backend.py.
  At present, the backend draws to separate Qt dialogs.
  We would prefer that all figures get drawn in the VR pane.
 
More work is coming.
QQQQQ

Here is an example: The headline is @pyplot basic_example, the body is:

# http://matplotlib.org/1.5.1/examples/animation/basic_example.html
if 0:
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation

fig2 = plt.figure()
x = np.arange(-9, 10)
y = np.arange(-9, 10).reshape(-1, 1)
base = np.hypot(x, y)
ims = []
for add in np.arange(15):
    ims.append((plt.pcolor(x, y,
                           base + add,
                           norm=plt.Normalize(0, 30)),))
animation.ArtistAnimation(fig2, ims,
    interval=50,
    repeat_delay=3000,
    blit=True)

if 0:
    plt.ion()
    # sets interactive mode. Prevents this message:
    # QCoreApplication::exec: The event loop is already running
plt.show()

Notes:

1. The commented-out code is essential if you ran the script with Ctrl-B.  But it's not necessary if you view the the node with the viewrendered plugin.

2. At present, the vr plugin executes the script when you select the node.  It does not re-render the node as text changes.  That would result in lots of syntax errors. To re-render the node, execute the vr-update command.

3. The file leo/plugins/pyplot_backend.py is not a real plugin.  It provides a pyplot backend to the vr plugin.  At present, it works only with PyQt4.  Eventually, it should work with PyQt5.  Perhaps more importantly, the backend should embed pyplot figures in the VR pane.  That's next.  It may be a bit tricky.

Edward

Edward K. Ream

unread,
Sep 28, 2016, 12:42:48 PM9/28/16
to leo-editor
On Wednesday, September 28, 2016 at 9:55:19 AM UTC-5, Edward K. Ream wrote:

> Added preliminary support for @pyplot drawing in viewrendered plugin

There are many possible improvements, the most important of which will be caching of results in uA's. This will allow the vr plugin to instantly show the results of long-running computations.

It surely is possible to place multiple figures in a single vr pyplot canvas.  After all, you can do this in the standard Python interactive interpreter.  There are many pyplot back ends.  It may be that one already does most the work required.

At present, leo/plugins/pyplot_backend.py gains access to symbols via the standard Qt4 backend: matplotlib.backends.backend_qt4agg.py This avoids cut/pasting code, but it may cause other problems later.  For now it's good enough for a prototype.

EKR

Edward K. Ream

unread,
Sep 28, 2016, 12:45:22 PM9/28/16
to leo-editor
On Wednesday, September 28, 2016 at 11:42:48 AM UTC-5, Edward K. Ream wrote:

>> Added preliminary support for @pyplot drawing in viewrendered plugin

> There are many possible improvements, the most important of which will be caching of results in uA's. This will allow the vr plugin to instantly show the results of long-running computations.

Imo, storing pyplot scripts in Leo is almost infinitely better than the Jupyter way.  You could easily keep thousands of scripts in Leo.  Not really possible with Jupyter.  But this won't matter unless people can easily preview and share .leo files.

EKR

Edward K. Ream

unread,
Sep 29, 2016, 1:37:16 PM9/29/16
to leo-editor
On Wednesday, September 28, 2016 at 9:55:19 AM UTC-5, Edward K. Ream wrote:

> Added preliminary support for @pyplot drawing in viewrendered plugin:

As of recent revs, the pyplot graphs are embedded in the vr pane. This allows an outline to contain unlimited graphs, and puts Leo on a par with Jupyter as far as graphics capabilities goes.

The code works with both PyQt 4 and 5. The standard pyplot toolbar is fully functional, but the status area is not function yet with PyQt 5. You can pan and zoom, save the graphs, etc.

To do

1. Cache graphs so that they can be retrieved without possibly lengthy recalculations.
2. Support MathJax in the VR pane. This is needed to render Jupyter cells correctly.

Please try this out.  It's impressive.

EKR



Edward K. Ream

unread,
Sep 30, 2016, 7:41:16 AM9/30/16
to leo-editor
On Thursday, September 29, 2016 at 12:37:16 PM UTC-5, Edward K. Ream wrote:

To do

1. Cache graphs so that they can be retrieved without possibly lengthy recalculations.
2. Support MathJax in the VR pane. This is needed to render Jupyter cells correctly.

Actually, the VR pane handles MathJax well, with some glitches. Here are some notes.  All apply both to Linux and Windows:

- Leo renders MathJax significantly better when using PyQt5.
- With PyQt5, you have to resize the VR pane in order to see the output!  This looks like QWebView bug.  I'll attempt a workaround later today.
- The MathJax fonts are tiny.  As a workaround, I added this setting:

    @int qweb_view_font_size = 30

Rev d46162d adds a MathJax example to test.leo. Here is another example.  Note the </script> incantations that must be present:

<!DOCTYPE html>
<html>
<head>
<title>MathJax TeX Test Page</title>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>
<script type="text/javascript" async
  src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_CHTML">
</script>
</head>
<body>
When $a \ne 0$, there are two solutions to \(ax^2 + bx + c = 0\) and they are
$$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$
</body>
</html>


EKR

Edward K. Ream

unread,
Oct 1, 2016, 8:03:56 AM10/1/16
to leo-editor
On Friday, September 30, 2016 at 6:41:16 AM UTC-5, Edward K. Ream wrote:

> - With PyQt5, you have to resize the VR pane in order to see the output!

Completely fixed at 563813803.  On Qt5 only, it seems necessary to hide the QWebView before showing it.  Imo, this is a bug, but happily the workaround takes only one line of code.

EKR
Reply all
Reply to author
Forward
0 new messages