jupyter-analysis-tools 1.7.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,89 @@
1
+ # -*- coding: utf-8 -*-
2
+ # widgets.py
3
+
4
+ import os
5
+
6
+ import ipywidgets as ui
7
+
8
+
9
+ def showBoolStatus(value, description, invertcolor=False):
10
+ """Pretty prints the status of a boolean variable *value* along with
11
+ the provided description in a green color for True and in a red color
12
+ for False values. *invertcolor* allows to flip the color assignment."""
13
+ from IPython.display import HTML, display
14
+
15
+ statuscolor = "darkgreen"
16
+ if value ^ invertcolor:
17
+ statuscolor = "darkred"
18
+ descr = description[0].lower() + description[1:-1]
19
+ if description[-1].isalnum():
20
+ descr += description[-1]
21
+ statustext = '<h4 style="color: {};">Yes, {}!</h4>'.format(statuscolor, descr)
22
+ if value:
23
+ statustext = '<h4 style="color: {};">Do not {}!</h4>'.format(statuscolor, descr)
24
+ display(
25
+ HTML(
26
+ f'<div style="border-style: solid; border-color: {statuscolor};'
27
+ " border-width: 1px; padding: 0em 1em .5em 1em; margin: 1em 0em;"
28
+ f' width: {len(descr) * 0.75}em;">'
29
+ + statustext
30
+ + "</div>"
31
+ )
32
+ )
33
+
34
+
35
+ class PathSelector:
36
+ def __init__(self, start_dir, select_file=True):
37
+ self.file = None
38
+ self.select_file = select_file
39
+ self.cwd = start_dir
40
+ self.select = ui.SelectMultiple(options=["init"], value=(), rows=10, description="")
41
+ self.accord = ui.Accordion(children=[self.select])
42
+
43
+ self.accord.selected_index = None # Start closed (showing path only)
44
+ self.refresh(self.cwd)
45
+ self.select.observe(self.on_update, "value")
46
+
47
+ def on_update(self, change):
48
+ if len(change["new"]) > 0:
49
+ self.refresh(change["new"][0])
50
+
51
+ def refresh(self, item):
52
+ path = os.path.abspath(os.path.join(self.cwd, item))
53
+
54
+ if os.path.isfile(path):
55
+ if self.select_file:
56
+ self.accord.set_title(0, path)
57
+ self.file = path
58
+ self.accord.selected_index = None
59
+ else:
60
+ self.select.value = ()
61
+
62
+ else: # os.path.isdir(path)
63
+ self.file = None
64
+ self.cwd = path
65
+
66
+ # Build list of files and dirs
67
+ keys = ["[..]"]
68
+ for item in os.listdir(path):
69
+ if item[0] == ".":
70
+ continue
71
+ elif os.path.isdir(os.path.join(path, item)):
72
+ keys.append("[" + item + "]")
73
+ else:
74
+ keys.append(item)
75
+
76
+ # Sort and create list of output values
77
+ keys.sort(key=str.lower)
78
+ vals = []
79
+ for k in keys:
80
+ if k[0] == "[":
81
+ vals.append(k[1:-1]) # strip off brackets
82
+ else:
83
+ vals.append(k)
84
+
85
+ # Update widget
86
+ self.accord.set_title(0, path)
87
+ self.select.options = list(zip(keys, vals))
88
+ with self.select.hold_trait_notifications():
89
+ self.select.value = ()