Commit 9eab2df8 authored by Ayush Tiwari's avatar Ayush Tiwari

erp5_dat_notebook bt5: Add Base_savePlot to JupyterCompile extension which...

erp5_dat_notebook bt5: Add Base_savePlot to JupyterCompile extension which would help creating an ERP5 Image object from matplotlib plots
parent 1d1b1906
...@@ -188,7 +188,10 @@ def UpdateLocalVariableDict(self, existing_dict): ...@@ -188,7 +188,10 @@ def UpdateLocalVariableDict(self, existing_dict):
def Base_displayImage(self, image_object=None): def Base_displayImage(self, image_object=None):
""" """
External function to display Image objects to jupyter frontend. External function to display Image objects to jupyter frontend.
XXX: This function is intented to be called from Base_executeJupyter
or Jupyter frontend.That's why printing string and returning None
Parameters Parameters
---------- ----------
...@@ -199,7 +202,7 @@ def Base_displayImage(self, image_object=None): ...@@ -199,7 +202,7 @@ def Base_displayImage(self, image_object=None):
Output Output
----- -----
Returns base64 encoded string of the plot on which it has been called. Prints base64 encoded string of the plot on which it has been called.
""" """
if image_object: if image_object:
...@@ -231,3 +234,56 @@ def Base_displayImage(self, image_object=None): ...@@ -231,3 +234,56 @@ def Base_displayImage(self, image_object=None):
# value from stdout and using return we would get that value as string inside # value from stdout and using return we would get that value as string inside
# an string which is unfavourable. # an string which is unfavourable.
print figdata print figdata
return None
def Base_savePlot(self, plot=None, reference=None):
"""
Saves generated plots from matplotlib in ERP5 Image module
XXX: Use only if bt5 'erp5_wendelin' installed
This function is intented to be called from Base_executeJupyter
or Jupyter frontend.
Parameters
----------
plot : Matplotlib plot object
reference: Reference of Image object which would be generated
Id and reference should be always unique
Output
------
Returns None, but saves the plot object as ERP5 image in Image Module with
reference same as that of data_array_object.
"""
# As already specified in docstring, this function should be called from
# Base_executeJupyter or Jupyter Frontend which means that it would pass
# through exec and hence the printed result would be caught in a string and
# that's why we are using print and returning None.
if not reference:
print 'No reference specified for Image object'
return None
if not plot:
print 'No matplotlib plot object specified'
return None
filename = '%s.png'%reference
# Save plot data in buffer
buff = StringIO()
plot.savefig(buff, format='png')
buff.seek(0)
data = buff.getvalue()
# Add new Image object in erp5 with id and reference
import time
image_id = reference+str(time.time())
self.newContent(
portal_type='Image',
id=image_id,
reference=reference,
data=data,
filename=filename)
return None
\ No newline at end of file
...@@ -50,7 +50,7 @@ ...@@ -50,7 +50,7 @@
<string>W: 82, 6: Use of exec (exec-used)</string> <string>W: 82, 6: Use of exec (exec-used)</string>
<string>W:111, 6: Use of exec (exec-used)</string> <string>W:111, 6: Use of exec (exec-used)</string>
<string>W:117, 6: Use of exec (exec-used)</string> <string>W:117, 6: Use of exec (exec-used)</string>
<string>W:209, 4: Using the global statement (global-statement)</string> <string>W:212, 4: Using the global statement (global-statement)</string>
</tuple> </tuple>
</value> </value>
</item> </item>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment