-
Notifications
You must be signed in to change notification settings - Fork 233
/
output.py
65 lines (51 loc) · 1.29 KB
/
output.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import base64
import json
import os
import pprint
from io import BytesIO
from IPython.core.display import display, HTML
from matplotlib import pyplot as plt
from optimus.helpers.check import is_str
def output_image(fig, path):
"""
Output a png file
:param fig:
:param path: Matplotlib figure
:return: Base64 encode image
"""
fig.savefig(path, format='png')
plt.close()
def output_base64(fig):
"""
Output a matplotlib as base64 encode
:param fig: Matplotlib figure
:return: Base64 encode image
"""
fig_file = BytesIO()
plt.savefig(fig_file, format='png')
# rewind to beginning of file
fig_file.seek(0)
fig_png = base64.b64encode(fig_file.getvalue())
plt.close(fig)
return fig_png.decode('utf8')
def print_html(html):
"""
Display() helper to print html code
:param html: html code to be printed
:return:
"""
if "DATABRICKS_RUNTIME_VERSION" in os.environ:
displayHTML(result)
else:
display(HTML(html))
def print_json(value):
"""
Print a human readable json
:param value: json to be printed
:return: json
"""
pp = pprint.PrettyPrinter(indent=2)
if is_str(value):
value = value.replace("'", "\"")
value = json.loads(value)
pp.pprint(value)