Belle II Software  release-05-02-19
viewer.py
1 from hep_ipython_tools import viewer
2 import random
3 import string
4 
5 
7  """The css string for styling the notebook."""
8 
9 
10  css_string = """
11  #notebook {
12  background-color: rgba(20, 166, 255, 0.3);
13  background-image: url("http://www-ekp.physik.uni-karlsruhe.de/~nbraun/belle.svg");
14  background-repeat: no-repeat;
15  background-position: right bottom;
16  }
17 
18  #notebook-container {
19  width: 90%;
20  }
21 
22  #menubar-container {
23  width: 90%;
24  }
25 
26  #header-container {
27  width: 90%;
28  }
29  """
30 
31 
33 
34  """
35  Viewer object for the ipython_handler_basf2 path.
36  Do not use it on your own.
37  """
38 
39  def __init__(self, path, standalone=False):
40  """
41  Create a new path viewer object with the path from the process
42  """
43 
44  try:
45  self.path = path.modules()
46  except:
47  self.path = path
48 
49 
50  self.standalone = standalone
51 
52  def create(self):
53  """
54  Create the widget
55  """
56 
57  import ipywidgets as widgets
58 
59  if self.path is None:
60  return widgets.HTML("No modules in path.")
61 
62  a = widgets.Accordion()
63  children = []
64 
65  for i, element in enumerate(self.path):
66  html = ModuleViewer(element, standalone=self.standalone)
67  children.append(html.create())
68  if isinstance(html.module.name, str):
69  a.set_title(i, html.module.name)
70  else:
71  a.set_title(i, html.module.name())
72 
73  a.children = children
74 
75  return a
76 
77 
79  """
80  A widget for showing module parameter with their content (not standalone)
81  or with their description (standalone).
82  """
83 
84  def __init__(self, module, standalone=True):
85  """ Init with a module as a string or a registered one. """
86 
87 
88  if isinstance(module, str):
89  import basf2.core as _basf2
90  self.module = _basf2.register_module(module)
91  else:
92  self.module = module
93 
94 
95  self.standalone = standalone
96 
97 
98  self.table_beginning_html = """<table style="margin-left: auto; margin-right: auto;
99  border-collapse: separate; border-spacing: 0px;">"""
100 
101 
102  self.td_html = "style=\"padding: 10px;\""
103 
104 
105  self.table_row_parameters = """<tr><td {td_style}>{param.name}</td>
106  <td{color_text} {td_style}>{param.values}</td>
107  <td style="color: gray; {td_style}>{param.default}</td></tr>"""
108 
109 
110  self.table_row_help = """<tr><td {td_style}>{param.name}</td>
111  <td {td_style}>{param.type}</td>
112  <td {td_style}>{param.values}</td>
113  <td style="color: gray; {td_style}>{param.description}</td></tr>"""
114 
115 
116  self.table_row_html_single = """<tr><td colspan="4" {td_style}>{text}</td></tr>"""
117 
118 
119  self.table_title_html = """<thead><td colspan="4" style="text-align: center;
120  font-size: 18pt;" {td_style}>{module_name} ({package})</td></thead>"""
121 
122  def get_color_code(self, param):
123  """
124  Handy function for getting a color based on a parameter:
125  if it has the default value, no color,
126  if not, red color.
127  """
128  if str(param.values) != str(param.default) and str(param.default) != "":
129  color_text = " style='color: red;'"
130  else:
131  color_text = ""
132  return color_text
133 
134  def create(self):
135  """
136  Show the widget.
137  """
138  import ipywidgets as widgets
139 
140  html = widgets.HTML()
141  html.value = self.table_beginning_html
142 
143  if self.standalone:
144  if isinstance(self.module.name, str):
145  module_name = self.module.name
146  else:
147  module_name = self.module.name()
148 
149  html.value += self.table_title_html.format(module_name=module_name, package=self.module.package(),
150  td_style=self.td_html)
151 
152  html.value += self.table_row_html_single.format(text=self.module.description(), td_style=self.td_html)
153 
154  if len(self.module.available_params()) == 0:
155  html.value += self.table_row_html_single.format(text="No parameters available.", td_style=self.td_html)
156  else:
157  for param in self.module.available_params():
158  color_text = self.get_color_code(param)
159 
160  if self.standalone:
161  table_row_html = self.table_row_help
162  else:
163  table_row_html = self.table_row_parameters
164 
165  html.value += table_row_html.format(param=param, color_text=color_text, td_style=self.td_html)
166  html.value += "</table>"
167 
168  return html
169 
170 
172  """Show the dependencies in a nice and fancy way :-)"""
173 
174  def __init__(self, store_arrays_with_dependencies_JSON):
175  """Create a new dependency viewer."""
176 
177  self.store_arrays_with_dependencies_JSON = store_arrays_with_dependencies_JSON
178 
179 
180  self.random_name = ''.join(random.choice(string.ascii_letters) for _ in range(10))
181 
182  self.element_name = "dependencies_" + self.random_name
183 
184 
185  self.d3_include_string = """<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>"""
186 
187 
188  self.d3_element_string = """<div id="{element_name}"></div>""".format(element_name=self.element_name)
189 
190 
191  self.style_template = """<style>
192  /* d3 related settings */
193  .node {
194  font: 300 14px "Helvetica Neue", Helvetica, Arial, sans-serif;
195  fill: #bbb;
196  cursor: pointer;
197  }
198 
199  .node:hover {
200  fill: #000;
201  }
202 
203  .link {
204  stroke: rgba(20, 166, 255, 0.3);
205  stroke-width: 2px;
206  fill: none;
207  pointer-events: none;
208  }
209 
210  .node:hover,
211  .node--source,
212  .node--target {
213  font-weight: 700;
214  }
215 
216  .node--source {
217  fill: #2ca02c;
218  }
219 
220  .node--target {
221  fill: #d62728;
222  }
223 
224  .link--source,
225  .link--target {
226  stroke-opacity: 1;
227  stroke-width: 4px;
228  }
229 
230  .link--source {
231  stroke: #d62728;
232  }
233 
234  .link--target {
235  stroke: #2ca02c;
236  }
237  </style>"""
238 
239 
240  self.nodes_template = """<script>var test_nodes = JSON.parse('{nodes_json}');</script>""".format(
241  nodes_json=self.store_arrays_with_dependencies_JSON)
242 
243 
245  <script>
246  var diameter = 960;
247  var radius = diameter / 2;
248  var innerRadius = radius - 120;
249 
250  var cluster = d3.layout.cluster()
251  .size([360, innerRadius])
252  .sort(null)
253  .value(function(d) { return d.size; });
254 
255  var bundle = d3.layout.bundle();
256 
257  var line = d3.svg.line.radial()
258  .interpolate("bundle")
259  .tension(.85)
260  .radius(function(d) { return d.y; })
261  .angle(function(d) { return d.x / 180 * Math.PI; });
262 
263  var svg = d3.select("#""" + self.element_name + """").append("svg")
264  .attr("width", diameter)
265  .attr("height", diameter)
266  .append("g")
267  .attr("transform", "translate(" + radius + "," + radius + ")");
268 
269  var link = svg.append("g").selectAll(".link");
270  var node = svg.append("g").selectAll(".node");
271 
272  var nodes = cluster.nodes(test_nodes);
273  var links = relations(nodes);
274 
275  link = link
276  .data(bundle(links))
277  .enter()
278  .append("path")
279  .each(function(d) { d.source = d[0], d.target = d[d.length - 1]; })
280  .attr("class", "link")
281  .attr("d", line);
282 
283  node = node
284  .data(nodes.filter(function(n) { return !n.children; }))
285  .enter()
286  .append("text")
287  .attr("class", "node")
288  .attr("dy", ".31em")
289  .attr("transform", function(d) {
290  return "rotate(" + (d.x - 90) +
291  ")translate(" + (d.y + 8) + ",0)"
292  + (d.x < 180 ? "" : "rotate(180)");})
293  .style("text-anchor", function(d) { return d.x < 180 ? "start" : "end"; })
294  .text(function(d) { return d.key; })
295  .on("mouseover", mouseovered)
296  .on("mouseout", mouseouted);
297 
298  function mouseovered(d) {
299  node.each(function(n) { n.target = n.source = false; });
300 
301  link.classed("link--target", function(l) { if (l.target === d) return l.source.source = true; })
302  .classed("link--source", function(l) { if (l.source === d) return l.target.target = true; })
303  .filter(function(l) { return l.target === d || l.source === d; })
304  .each(function() { this.parentNode.appendChild(this); });
305 
306  node.classed("node--target", function(n) { return n.target; })
307  .classed("node--source", function(n) { return n.source; });
308  }
309 
310  function mouseouted(d) {
311  link.classed("link--target", false)
312  .classed("link--source", false);
313 
314  node.classed("node--target", false)
315  .classed("node--source", false);
316  }
317 
318  d3.select(self.frameElement).style("height", diameter + "px");
319 
320  // Return a list of imports for the given array of nodes.
321  function relations(nodes) {
322  var map = {},
323  relation = [];
324 
325  // Compute a map from name to node.
326  nodes.forEach(function(d) {
327  map[d.name] = d;
328  });
329 
330  // For each import, construct a link from the source to target node.
331  nodes.forEach(function(d) {
332  if (d.relation) d.relation.forEach(function(i) {
333  relation.push({source: map[d.name], target: map[i]});
334  });
335  });
336 
337  return relation;
338  }
339  </script>
340  """
341 
342  def create(self):
343  """
344  Create the widget.
345  """
346  import ipywidgets as widgets
347  html = widgets.HTML(self.viewer_template)
348 
349  return html
hep_ipython_tools.ipython_handler_basf2.viewer.PathViewer.create
def create(self)
Definition: viewer.py:52
hep_ipython_tools.ipython_handler_basf2.viewer.PathViewer.standalone
standalone
In the standalone mode, the basic parameters of the modules are shown.
Definition: viewer.py:50
hep_ipython_tools.ipython_handler_basf2.viewer.DependencyViewer.d3_element_string
d3_element_string
Template for the element itself.
Definition: viewer.py:188
hep_ipython_tools.viewer.IPythonWidget
Definition: viewer.py:13
hep_ipython_tools.ipython_handler_basf2.viewer.DependencyViewer.element_name
element_name
The name representing the object for javascript.
Definition: viewer.py:182
basf2.core
Definition: core.py:1
hep_ipython_tools.ipython_handler_basf2.viewer.ModuleViewer.table_row_parameters
table_row_parameters
Template for the row of parameters.
Definition: viewer.py:105
hep_ipython_tools.ipython_handler_basf2.viewer.DependencyViewer
Definition: viewer.py:171
hep_ipython_tools.ipython_handler_basf2.viewer.DependencyViewer.store_arrays_with_dependencies_JSON
store_arrays_with_dependencies_JSON
A JSON from the processing of dependencies.
Definition: viewer.py:177
hep_ipython_tools.ipython_handler_basf2.viewer.DependencyViewer.viewer_template
viewer_template
Template for the full HTML.
Definition: viewer.py:244
hep_ipython_tools.ipython_handler_basf2.viewer.PathViewer.path
path
The path to show.
Definition: viewer.py:45
hep_ipython_tools.ipython_handler_basf2.viewer.DependencyViewer.random_name
random_name
Part of the name representing the object for javascript.
Definition: viewer.py:180
hep_ipython_tools.ipython_handler_basf2.viewer.ModuleViewer.table_beginning_html
table_beginning_html
Template for the table beginning.
Definition: viewer.py:98
hep_ipython_tools.ipython_handler_basf2.viewer.ModuleViewer.get_color_code
def get_color_code(self, param)
Definition: viewer.py:122
hep_ipython_tools.ipython_handler_basf2.viewer.ModuleViewer.table_title_html
table_title_html
Template for the table title.
Definition: viewer.py:119
hep_ipython_tools.ipython_handler_basf2.viewer.DependencyViewer.d3_include_string
d3_include_string
Tenplate for the include string.
Definition: viewer.py:185
hep_ipython_tools.ipython_handler_basf2.viewer.ModuleViewer
Definition: viewer.py:78
hep_ipython_tools.ipython_handler_basf2.viewer.StylingWidget
Definition: viewer.py:6
hep_ipython_tools.ipython_handler_basf2.viewer.ModuleViewer.module
module
The module to show.
Definition: viewer.py:90
hep_ipython_tools.ipython_handler_basf2.viewer.ModuleViewer.__init__
def __init__(self, module, standalone=True)
Definition: viewer.py:84
hep_ipython_tools.ipython_handler_basf2.viewer.ModuleViewer.table_row_html_single
table_row_html_single
Template for the simple row.
Definition: viewer.py:116
hep_ipython_tools.ipython_handler_basf2.viewer.PathViewer
Definition: viewer.py:32
hep_ipython_tools.ipython_handler_basf2.viewer.ModuleViewer.table_row_help
table_row_help
Template for the row with help.
Definition: viewer.py:110
hep_ipython_tools.ipython_handler_basf2.viewer.DependencyViewer.create
def create(self)
Definition: viewer.py:342
hep_ipython_tools.ipython_handler_basf2.viewer.PathViewer.__init__
def __init__(self, path, standalone=False)
Definition: viewer.py:39
hep_ipython_tools.ipython_handler_basf2.viewer.DependencyViewer.nodes_template
nodes_template
Template for inserting the node JSON.
Definition: viewer.py:240
hep_ipython_tools.ipython_handler_basf2.viewer.DependencyViewer.__init__
def __init__(self, store_arrays_with_dependencies_JSON)
Definition: viewer.py:174
hep_ipython_tools.ipython_handler_basf2.viewer.DependencyViewer.style_template
style_template
Template for the style.
Definition: viewer.py:191
hep_ipython_tools.ipython_handler_basf2.viewer.ModuleViewer.td_html
td_html
Template for the table cell.
Definition: viewer.py:102
hep_ipython_tools.ipython_handler_basf2.viewer.ModuleViewer.create
def create(self)
Definition: viewer.py:134
hep_ipython_tools.ipython_handler_basf2.viewer.ModuleViewer.standalone
standalone
In the standalone mode, the basic parameters of the modules are shown.
Definition: viewer.py:95