mirror of
https://github.com/apache/zeppelin
synced 2026-05-24 09:38:26 +00:00
ZEPPELIN-1318 - Add support for png images in z.show()
This commit is contained in:
parent
85d4df4f0c
commit
1efa0c975e
1 changed files with 18 additions and 6 deletions
|
|
@ -19,6 +19,7 @@
|
|||
# Remove interactive mode displayhook
|
||||
import sys
|
||||
import signal
|
||||
import base64
|
||||
|
||||
try:
|
||||
import StringIO as io
|
||||
|
|
@ -69,10 +70,10 @@ def help():
|
|||
plt.close()
|
||||
</pre>
|
||||
<div><br/> z.show function can take optional parameters
|
||||
to adapt graph width and height</div>
|
||||
to adapt graph dimensions (width and height) and format (png or svg)</div>
|
||||
<div><b>example </b>:
|
||||
<pre>z.show(plt,width='50px
|
||||
z.show(plt,height='150px') </pre></div>
|
||||
z.show(plt,height='150px', fmt='svg') </pre></div>
|
||||
|
||||
<h3>Pandas DataFrame</h3>
|
||||
<div> You need to have Pandas module installed
|
||||
|
|
@ -163,13 +164,24 @@ class PyZeppelinContext(object):
|
|||
#)
|
||||
body_buf.close(); header_buf.close()
|
||||
|
||||
def show_matplotlib(self, p, width="100%", height="100%", **kwargs):
|
||||
def show_matplotlib(self, p, width="100%", height="100%",
|
||||
fmt='png', **kwargs):
|
||||
"""Matplotlib show function
|
||||
"""
|
||||
img = io.StringIO()
|
||||
p.savefig(img, format="svg")
|
||||
html = "%html <div style='width:{width};height:{height}'>{image}<div>"
|
||||
print(html.format(width=width, height=height, image=img.getvalue()))
|
||||
if fmt == 'png':
|
||||
p.savefig(img, format=fmt)
|
||||
html = "%html <img src={img} width={width}, height={height}>"
|
||||
img_str = "data:image/png;base64,"
|
||||
img_str += base64.b64encode(img.getvalue().strip())
|
||||
elif fmt == 'svg':
|
||||
p.savefig(img, format=fmt)
|
||||
html = "%html <div style='width:{width};height:{height}'>{img}<div>"
|
||||
img_str = img.getvalue()
|
||||
else:
|
||||
raise ValueError("fmt must be 'png' or 'svg'")
|
||||
|
||||
print(html.format(width=width, height=height, img=img_str))
|
||||
img.close()
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue