Belle II Software development
DependencyViewer Class Reference
Inheritance diagram for DependencyViewer:
IPythonWidget

Public Member Functions

def __init__ (self, store_arrays_with_dependencies_JSON)
 
def create (self)
 

Public Attributes

 store_arrays_with_dependencies_JSON
 A JSON from the processing of dependencies.
 
 random_name
 Part of the name representing the object for javascript.
 
 element_name
 The name representing the object for javascript.
 
 d3_include_string
 Tenplate for the include string.
 
 d3_element_string
 Template for the element itself.
 
 style_template
 Template for the style.
 
 nodes_template
 Template for inserting the node JSON.
 
 viewer_template
 Template for the full HTML.
 

Detailed Description

Show the dependencies in a nice and fancy way :-)

Definition at line 178 of file viewer.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  store_arrays_with_dependencies_JSON 
)
Create a new dependency viewer.

Definition at line 181 of file viewer.py.

181 def __init__(self, store_arrays_with_dependencies_JSON):
182 """Create a new dependency viewer."""
183
184 self.store_arrays_with_dependencies_JSON = store_arrays_with_dependencies_JSON
185
186
187 self.random_name = ''.join(random.choice(string.ascii_letters) for _ in range(10))
188
189 self.element_name = "dependencies_" + self.random_name
190
191
192 self.d3_include_string = """<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>"""
193
194
195 self.d3_element_string = f"""<div id="{self.element_name}"></div>"""
196
197
198 self.style_template = """<style>
199 /* d3 related settings */
200 .node {
201 font: 300 14px "Helvetica Neue", Helvetica, Arial, sans-serif;
202 fill: #bbb;
203 cursor: pointer;
204 }
205
206 .node:hover {
207 fill: #000;
208 }
209
210 .link {
211 stroke: rgba(20, 166, 255, 0.3);
212 stroke-width: 2px;
213 fill: none;
214 pointer-events: none;
215 }
216
217 .node:hover,
218 .node--source,
219 .node--target {
220 font-weight: 700;
221 }
222
223 .node--source {
224 fill: #2ca02c;
225 }
226
227 .node--target {
228 fill: #d62728;
229 }
230
231 .link--source,
232 .link--target {
233 stroke-opacity: 1;
234 stroke-width: 4px;
235 }
236
237 .link--source {
238 stroke: #d62728;
239 }
240
241 .link--target {
242 stroke: #2ca02c;
243 }
244 </style>"""
245
246
247 self.nodes_template = f"""<script>var test_nodes = JSON.parse('{self.store_arrays_with_dependencies_JSON}');</script>"""
248
249
250 self.viewer_template = self.d3_include_string + self.nodes_template + self.d3_element_string + self.style_template + """
251 <script>
252 var diameter = 960;
253 var radius = diameter / 2;
254 var innerRadius = radius - 120;
255
256 var cluster = d3.layout.cluster()
257 .size([360, innerRadius])
258 .sort(null)
259 .value(function(d) { return d.size; });
260
261 var bundle = d3.layout.bundle();
262
263 var line = d3.svg.line.radial()
264 .interpolate("bundle")
265 .tension(.85)
266 .radius(function(d) { return d.y; })
267 .angle(function(d) { return d.x / 180 * Math.PI; });
268
269 var svg = d3.select("#""" + self.element_name + """").append("svg")
270 .attr("width", diameter)
271 .attr("height", diameter)
272 .append("g")
273 .attr("transform", "translate(" + radius + "," + radius + ")");
274
275 var link = svg.append("g").selectAll(".link");
276 var node = svg.append("g").selectAll(".node");
277
278 var nodes = cluster.nodes(test_nodes);
279 var links = relations(nodes);
280
281 link = link
282 .data(bundle(links))
283 .enter()
284 .append("path")
285 .each(function(d) { d.source = d[0], d.target = d[d.length - 1]; })
286 .attr("class", "link")
287 .attr("d", line);
288
289 node = node
290 .data(nodes.filter(function(n) { return !n.children; }))
291 .enter()
292 .append("text")
293 .attr("class", "node")
294 .attr("dy", ".31em")
295 .attr("transform", function(d) {
296 return "rotate(" + (d.x - 90) +
297 ")translate(" + (d.y + 8) + ",0)"
298 + (d.x < 180 ? "" : "rotate(180)");})
299 .style("text-anchor", function(d) { return d.x < 180 ? "start" : "end"; })
300 .text(function(d) { return d.key; })
301 .on("mouseover", mouseovered)
302 .on("mouseout", mouseouted);
303
304 function mouseovered(d) {
305 node.each(function(n) { n.target = n.source = false; });
306
307 link.classed("link--target", function(l) { if (l.target === d) return l.source.source = true; })
308 .classed("link--source", function(l) { if (l.source === d) return l.target.target = true; })
309 .filter(function(l) { return l.target === d || l.source === d; })
310 .each(function() { this.parentNode.appendChild(this); });
311
312 node.classed("node--target", function(n) { return n.target; })
313 .classed("node--source", function(n) { return n.source; });
314 }
315
316 function mouseouted(d) {
317 link.classed("link--target", false)
318 .classed("link--source", false);
319
320 node.classed("node--target", false)
321 .classed("node--source", false);
322 }
323
324 d3.select(self.frameElement).style("height", diameter + "px");
325
326 // Return a list of imports for the given array of nodes.
327 function relations(nodes) {
328 var map = {},
329 relation = [];
330
331 // Compute a map from name to node.
332 nodes.forEach(function(d) {
333 map[d.name] = d;
334 });
335
336 // For each import, construct a link from the source to target node.
337 nodes.forEach(function(d) {
338 if (d.relation) d.relation.forEach(function(i) {
339 relation.push({source: map[d.name], target: map[i]});
340 });
341 });
342
343 return relation;
344 }
345 </script>
346 """
347

Member Function Documentation

◆ create()

def create (   self)
Create the widget.

Reimplemented from IPythonWidget.

Definition at line 348 of file viewer.py.

348 def create(self):
349 """
350 Create the widget.
351 """
352 import ipywidgets as widgets
353 html = widgets.HTML(self.viewer_template)
354
355 return html

Member Data Documentation

◆ d3_element_string

d3_element_string

Template for the element itself.

Definition at line 195 of file viewer.py.

◆ d3_include_string

d3_include_string

Tenplate for the include string.

Definition at line 192 of file viewer.py.

◆ element_name

element_name

The name representing the object for javascript.

Definition at line 189 of file viewer.py.

◆ nodes_template

nodes_template

Template for inserting the node JSON.

Definition at line 247 of file viewer.py.

◆ random_name

random_name

Part of the name representing the object for javascript.

Definition at line 187 of file viewer.py.

◆ store_arrays_with_dependencies_JSON

store_arrays_with_dependencies_JSON

A JSON from the processing of dependencies.

Definition at line 184 of file viewer.py.

◆ style_template

style_template

Template for the style.

Definition at line 198 of file viewer.py.

◆ viewer_template

viewer_template

Template for the full HTML.

Definition at line 250 of file viewer.py.


The documentation for this class was generated from the following file: