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

Public Member Functions

def __init__ (self)
 
def update (self, text_or_percentage)
 
def show (self)
 

Public Attributes

 last_time
 The starting time of the process.
 
 last_percentage
 The starting percentage (obviously 0)
 
 progress_bar
 Widget of the progress bar itself.
 
 progress_label
 Label for the progress bar, shows progress in percent or status.
 
 progress_box
 Box widget that will be displayed, contains progress bar and status label.
 

Detailed Description

Viewer Object used to print data to the IPython Notebook.
Do not use it on your own.

Definition at line 66 of file viewer.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self)
Create a new progress bar viewer.

Definition at line 72 of file viewer.py.

72 def __init__(self):
73 """
74 Create a new progress bar viewer.
75 """
76 from IPython.core.display import display
77 from ipywidgets import FloatProgress, VBox, Layout, Label
78
79
80 self.last_time = time.time()
81
82 self.last_percentage = 0
83
84
85 self.progress_bar = FloatProgress(value=0, min=0, max=1,
86 layout=Layout(width="100%", height="40px"))
87
88 self.progress_label = Label()
89
90 self.progress_box = VBox([self.progress_bar, self.progress_label])
91
92 display(self.progress_box)
93

Member Function Documentation

◆ show()

def show (   self)
Display the widget

Reimplemented from IPythonWidget.

Definition at line 136 of file viewer.py.

136 def show(self):
137 """
138 Display the widget
139 """
140 from IPython.core.display import display
141
142 display(self.progress_box)
143
144

◆ update()

def update (   self,
  text_or_percentage 
)
Update the widget with a new event number

Definition at line 94 of file viewer.py.

94 def update(self, text_or_percentage):
95 """
96 Update the widget with a new event number
97 """
98
99 if isinstance(text_or_percentage, numbers.Number):
100 # text_or_percentage is percentage fraction
101 current_percentage = float(text_or_percentage)
102 current_time = time.time()
103
104 remaining_percentage = 1.0 - current_percentage
105
106 time_delta = current_time - self.last_time
107 percentage_delta = current_percentage - self.last_percentage
108
109 if percentage_delta > 0:
110 time_delta_per_percentage = 1.0 * time_delta / percentage_delta
111
112 # creates a human-readable time delta like '3 minutes 34 seconds'
113 attrs = ['years', 'months', 'days', 'hours', 'minutes', 'seconds']
114
115 def human_readable(delta): return [f'{int(getattr(delta, attr))} {getattr(delta, attr) > 1 and attr or attr[:-1]}'
116 for attr in attrs if getattr(delta, attr)]
117
118 times_list = human_readable(relativedelta(seconds=time_delta_per_percentage * remaining_percentage))
119 human_readable_str = " ".join(times_list)
120
121 display_text = f"{int(100 * current_percentage)} % Remaining time: {human_readable_str}"
122
123 self.progress_label.value = display_text
124 self.progress_bar.value = float(current_percentage)
125 else:
126 # text_or_percentage is status string
127 self.progress_label.value = f"Status: {text_or_percentage}"
128 if "finished" in str(text_or_percentage):
129 self.progress_bar.value = 1.0
130 self.progress_bar.bar_style = "success"
131 elif "failed" in str(text_or_percentage):
132 self.progress_bar.bar_style = "danger"
133 # color box red to see failure even when progress value is 0
134 self.progress_box.box_style = "danger"
135

Member Data Documentation

◆ last_percentage

last_percentage

The starting percentage (obviously 0)

Definition at line 82 of file viewer.py.

◆ last_time

last_time

The starting time of the process.

Definition at line 80 of file viewer.py.

◆ progress_bar

progress_bar

Widget of the progress bar itself.

Definition at line 85 of file viewer.py.

◆ progress_box

progress_box

Box widget that will be displayed, contains progress bar and status label.

Definition at line 90 of file viewer.py.

◆ progress_label

progress_label

Label for the progress bar, shows progress in percent or status.

Definition at line 88 of file viewer.py.


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