You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zeppelin.apache.org by bz...@apache.org on 2016/08/22 13:37:49 UTC

zeppelin git commit: ZEPPELIN-1328 - z.show in python interpreter does not display PNG images in python 3

Repository: zeppelin
Updated Branches:
  refs/heads/master d5528f00c -> 582981677


ZEPPELIN-1328 - z.show in python interpreter does not display PNG images in python 3

### What is this PR for?
Support for plotting PNG images via matplotlib inline for the python interpreter was recently added (#1329). However, these changes did not work for python3 since it handles strings differently. This PR aims to make the inline plotting compatible with both python 2 and 3.

### What type of PR is it?
Bug Fix

### What is the Jira issue?
* [ZEPPELIN-1328](https://issues.apache.org/jira/browse/ZEPPELIN-1328)

### How should this be tested?
In a python interpreteter cell, make sure the following produce an image:
```python
%python
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(5)
plt.plot(x)
z.show(plt, fmt='png') # Repeat for fmt='svg'
```
This should be tested for both python2 and 3 interpreters (via the interpreter settings page).

### Questions:
* Does the licenses files need update? No
* Is there breaking changes for older versions? No
* Does this needs documentation? No

Author: Alex Goodman <ag...@users.noreply.github.com>

Closes #1343 from agoodm/ZEPPELIN-1328 and squashes the following commits:

772313f [Alex Goodman] Redo io import structure to make z.show() work for both matplotlib plots and pandas dataframes in python2/3
6a8f3ab [Alex Goodman] Add python3 support for matplotlib inline plotting in python interpreter


Project: http://git-wip-us.apache.org/repos/asf/zeppelin/repo
Commit: http://git-wip-us.apache.org/repos/asf/zeppelin/commit/58298167
Tree: http://git-wip-us.apache.org/repos/asf/zeppelin/tree/58298167
Diff: http://git-wip-us.apache.org/repos/asf/zeppelin/diff/58298167

Branch: refs/heads/master
Commit: 582981677c43e194b3693b0e495ea4315ae64ef8
Parents: d5528f0
Author: Alex Goodman <ag...@users.noreply.github.com>
Authored: Thu Aug 18 21:09:13 2016 -0700
Committer: Alexander Bezzubov <bz...@apache.org>
Committed: Mon Aug 22 22:37:43 2016 +0900

----------------------------------------------------------------------
 python/src/main/resources/bootstrap.py | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/zeppelin/blob/58298167/python/src/main/resources/bootstrap.py
----------------------------------------------------------------------
diff --git a/python/src/main/resources/bootstrap.py b/python/src/main/resources/bootstrap.py
index 9e93839..16950fd 100644
--- a/python/src/main/resources/bootstrap.py
+++ b/python/src/main/resources/bootstrap.py
@@ -20,11 +20,11 @@
 import sys
 import signal
 import base64
-
+from io import BytesIO
 try:
-    import StringIO as io
+    from StringIO import StringIO
 except ImportError:
-    import io as io
+    from io import StringIO
 
 def intHandler(signum, frame):  # Set the signal handler
     print ("Paragraph interrupted")
@@ -117,6 +117,7 @@ class PyZeppelinContext(object):
     
     def __init__(self):
         self.max_result = 1000
+        self.py3 = bool(sys.version_info >= (3,))
     
     def input(self, name, defaultValue=""):
         print(self.errorMsg)
@@ -141,14 +142,14 @@ class PyZeppelinContext(object):
         """Pretty prints DF using Table Display System
         """
         limit = len(df) > self.max_result
-        header_buf = io.StringIO("")
+        header_buf = StringIO("")
         header_buf.write(str(df.columns[0]))
         for col in df.columns[1:]:
             header_buf.write("\t")
             header_buf.write(str(col))
         header_buf.write("\n")
         
-        body_buf = io.StringIO("")
+        body_buf = StringIO("")
         rows = df.head(self.max_result).values if limit else df.values
         for row in rows:
             body_buf.write(str(row[0]))
@@ -168,13 +169,18 @@ class PyZeppelinContext(object):
                         fmt='png', **kwargs):
         """Matplotlib show function
         """
-        img = io.StringIO()
         if fmt == 'png':
+            img = BytesIO()
             p.savefig(img, format=fmt)
             html = "%html <img src={img} width={width}, height={height}>"
-            img_str = "data:image/png;base64,"
+            img_str = b"data:image/png;base64,"
             img_str += base64.b64encode(img.getvalue().strip())
+            # Need to do this for python3 compatibility
+            if self.py3:
+                img_str = img_str.decode('ascii')
+                
         elif fmt == 'svg':
+            img = StringIO()
             p.savefig(img, format=fmt)
             html = "%html <div style='width:{width};height:{height}'>{img}<div>"
             img_str = img.getvalue()