Commit 8607a0ad authored by Ayush Tiwari's avatar Ayush Tiwari

erp5_data_notebook bt5: External Method to Display Matplotlib Image.

mime_type for results for executed jupyter_code from JupyterCompile extension
has also been defined on the server side itself.

This would help server to have more command over what Content-Type  would be
displayed to jupyter frontend.
parent 6487e06a
...@@ -7,6 +7,8 @@ import sys ...@@ -7,6 +7,8 @@ import sys
import ast import ast
import types import types
mime_type = 'text/plain'
def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict): def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict):
""" """
Function to execute jupyter code and update the local_varibale dictionary. Function to execute jupyter code and update the local_varibale dictionary.
...@@ -146,6 +148,7 @@ def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict): ...@@ -146,6 +148,7 @@ def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict):
'result_string': result_string, 'result_string': result_string,
'local_variable_dict': local_variable_dict, 'local_variable_dict': local_variable_dict,
'status': status, 'status': status,
'mime_type': mime_type,
'evalue': evalue, 'evalue': evalue,
'ename': ename, 'ename': ename,
'traceback': tb_list, 'traceback': tb_list,
...@@ -175,3 +178,36 @@ def UpdateLocalVariableDict(self, existing_dict): ...@@ -175,3 +178,36 @@ def UpdateLocalVariableDict(self, existing_dict):
new_dict['imports'][key] = val new_dict['imports'][key] = val
return new_dict return new_dict
def Base_displayMatplotlibImage(self, plot_object=None):
"""
External function to display Matplotlib Plot objects to jupyter function.
Parameters
----------
plot_object : Any matplotlib object from which we can create a plot.
Can be <matplotlib.lines.Line2D>, <matplotlib.text.Text>, etc.
Output
-----
Returns base64 encoded string of the plot on which it has been called.
"""
if plot_object:
from io import BytesIO
import base64
# Create a ByteFile on the server which would be used to save the plot
figfile = BytesIO()
# Save plot as 'png' format in the ByteFile
plot_object.savefig(figfile, format='png')
figfile.seek(0)
# Encode the value in figfile to base64 string so as to serve it jupyter frontend
figdata_png = base64.b64encode(figfile.getvalue())
# Chanage global variable 'mime_type' to 'image/png'
global mime_type
mime_type = 'image/png'
return figdata_png
\ No newline at end of file
...@@ -46,9 +46,10 @@ ...@@ -46,9 +46,10 @@
<key> <string>text_content_warning_message</string> </key> <key> <string>text_content_warning_message</string> </key>
<value> <value>
<tuple> <tuple>
<string>W: 73, 6: Use of exec (exec-used)</string> <string>W: 75, 6: Use of exec (exec-used)</string>
<string>W:102, 6: Use of exec (exec-used)</string> <string>W:104, 6: Use of exec (exec-used)</string>
<string>W:108, 6: Use of exec (exec-used)</string> <string>W:110, 6: Use of exec (exec-used)</string>
<string>W:209, 4: Using the global statement (global-statement)</string>
</tuple> </tuple>
</value> </value>
</item> </item>
......
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="ExternalMethod" module="Products.ExternalMethod.ExternalMethod"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_function</string> </key>
<value> <string>Base_displayMatplotlibImage</string> </value>
</item>
<item>
<key> <string>_module</string> </key>
<value> <string>JupyterCompile</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Base_displayMatplotlibImage</string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
...@@ -137,6 +137,7 @@ ename = final_result[\'ename\']\n ...@@ -137,6 +137,7 @@ ename = final_result[\'ename\']\n
evalue = final_result[\'evalue\']\n evalue = final_result[\'evalue\']\n
traceback = final_result[\'traceback\']\n traceback = final_result[\'traceback\']\n
status = final_result[\'status\']\n status = final_result[\'status\']\n
mime_type = final_result[\'mime_type\']\n
\n \n
# Call to function to update persistent mapping object with new local variables\n # Call to function to update persistent mapping object with new local variables\n
# and save the variables in the Active Result pertaining to the current Data Notebook\n # and save the variables in the Active Result pertaining to the current Data Notebook\n
...@@ -148,7 +149,8 @@ result = {\n ...@@ -148,7 +149,8 @@ result = {\n
u\'ename\': ename,\n u\'ename\': ename,\n
u\'evalue\': evalue,\n u\'evalue\': evalue,\n
u\'traceback\': traceback,\n u\'traceback\': traceback,\n
u\'status\': status\n u\'status\': status,\n
u\'mime_type\': mime_type\n
}\n }\n
\n \n
# Catch exception while seriaizing the result to be passed to jupyter frontend\n # Catch exception while seriaizing the result to be passed to jupyter frontend\n
......
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