From 1dd7e616471007db774ab28164a8a4bad3a25370 Mon Sep 17 00:00:00 2001 From: Christopher Hogan Date: Fri, 20 Dec 2019 13:12:32 -0600 Subject: [PATCH] Add progress bar for notebooks (#1078) * Add progress bar for notebooks * Update progress bar in quiet mode --- python/simulation.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/python/simulation.py b/python/simulation.py index 7fc935966..89621117b 100644 --- a/python/simulation.py +++ b/python/simulation.py @@ -26,6 +26,14 @@ except NameError: basestring = str +try: + from ipywidgets import FloatProgress + from IPython.display import display + do_progress = True +except ImportError: + do_progress = False + + # Send output from Meep, ctlgeom, and MPB to Python's stdout mp.cvar.master_printf_callback = mp.py_master_printf_wrap mp.set_ctl_printf_callback(mp.py_master_printf_wrap) @@ -1371,6 +1379,10 @@ def stop_cond(sim): step_funcs = list(step_funcs) step_funcs.append(display_progress(t0, t0 + stop_time, self.progress_interval)) + + if do_progress: + self.progress = FloatProgress(value=t0, min=t0, max=t0 + stop_time, description="0% done ") + display(self.progress) else: assert callable(cond[i]), "Stopping condition {} is not an integer or a function".format(cond[i]) @@ -1388,6 +1400,10 @@ def stop_cond(sim): for func in step_funcs: _eval_step_func(self, func, 'finish') + if do_progress: + self.progress.value = t0 + stop_time + self.progress.description = "100% done " + if mp.cvar.verbosity > 0: print("run {} finished at t = {} ({} timesteps)".format(self.run_index, self.meep_time(), self.fields.t)) self.run_index += 1 @@ -2591,14 +2607,21 @@ def display_progress(t0, t, dt): def _disp(sim): t1 = mp.wall_time() - if t1 - closure['tlast'] >= dt and mp.cvar.verbosity > 0: + if t1 - closure['tlast'] >= dt: msg_fmt = "Meep progress: {}/{} = {:.1f}% done in {:.1f}s, {:.1f}s to go" val1 = sim.meep_time() - t0 val2 = val1 / (0.01 * t) val3 = t1 - t_0 val4 = (val3 * (t / val1) - val3) if val1 != 0 else 0 - print(msg_fmt.format(val1, t, val2, val3, val4)) + + if do_progress: + sim.progress.value = val1 + sim.progress.description = "{}% done ".format(int(val2)) + + if mp.cvar.verbosity > 0: + print(msg_fmt.format(val1, t, val2, val3, val4)) closure['tlast'] = t1 + return _disp