@@ -54,6 +54,15 @@ def parse_trace_json(trace):
54
54
return as_json
55
55
56
56
57
+ def _optional_master_spec_json (master_spec ):
58
+ """Helper function to return 'null' or a master spec JSON string."""
59
+ if master_spec is None :
60
+ return 'null'
61
+ else :
62
+ return json_format .MessageToJson (
63
+ master_spec , preserving_proto_field_name = True )
64
+
65
+
57
66
def _container_div (height = '700px' , contents = '' ):
58
67
elt_id = str (uuid .uuid4 ())
59
68
html = """
@@ -64,7 +73,11 @@ def _container_div(height='700px', contents=''):
64
73
return elt_id , html
65
74
66
75
67
- def trace_html (trace , convert_to_unicode = True , height = '700px' , script = None ):
76
+ def trace_html (trace ,
77
+ convert_to_unicode = True ,
78
+ height = '700px' ,
79
+ script = None ,
80
+ master_spec = None ):
68
81
"""Generates HTML that will render a master trace.
69
82
70
83
This will result in a self-contained "div" element.
@@ -76,6 +89,8 @@ def trace_html(trace, convert_to_unicode=True, height='700px', script=None):
76
89
often pass the output of this function to IPython.display.HTML.
77
90
height: CSS string representing the height of the element, default '700px'.
78
91
script: Visualization script contents, if the defaults are unacceptable.
92
+ master_spec: Master spec proto (parsed), which can improve the layout. May
93
+ be required in future versions.
79
94
80
95
Returns:
81
96
unicode or str with HTML contents.
@@ -89,10 +104,14 @@ def trace_html(trace, convert_to_unicode=True, height='700px', script=None):
89
104
{div_html}
90
105
<script type='text/javascript'>
91
106
{script}
92
- visualizeToDiv({json}, "{elt_id}");
107
+ visualizeToDiv({json}, "{elt_id}", {master_spec_json} );
93
108
</script>
94
109
""" .format (
95
- script = script , json = json_trace , elt_id = elt_id , div_html = div_html )
110
+ script = script ,
111
+ json = json_trace ,
112
+ master_spec_json = _optional_master_spec_json (master_spec ),
113
+ elt_id = elt_id ,
114
+ div_html = div_html )
96
115
return unicode (as_str , 'utf-8' ) if convert_to_unicode else as_str
97
116
98
117
@@ -174,11 +193,13 @@ def initial_html(self, height='700px', script=None, init_message=None):
174
193
script = script , div_html = div_html )
175
194
return unicode (html , 'utf-8' ) # IPython expects unicode.
176
195
177
- def show_trace (self , trace ):
196
+ def show_trace (self , trace , master_spec = None ):
178
197
"""Returns a JS script HTML fragment, which will populate the container.
179
198
180
199
Args:
181
200
trace: binary-encoded MasterTrace string.
201
+ master_spec: Master spec proto (parsed), which can improve the layout. May
202
+ be required in future versions.
182
203
183
204
Returns:
184
205
unicode with HTML contents.
@@ -187,8 +208,10 @@ def show_trace(self, trace):
187
208
<meta charset="utf-8"/>
188
209
<script type='text/javascript'>
189
210
document.getElementById("{elt_id}").innerHTML = ""; // Clear previous.
190
- visualizeToDiv({json}, "{elt_id}");
211
+ visualizeToDiv({json}, "{elt_id}", {master_spec_json} );
191
212
</script>
192
213
""" .format (
193
- json = parse_trace_json (trace ), elt_id = self .elt_id )
214
+ json = parse_trace_json (trace ),
215
+ master_spec_json = _optional_master_spec_json (master_spec ),
216
+ elt_id = self .elt_id )
194
217
return unicode (html , 'utf-8' ) # IPython expects unicode.
0 commit comments