diff --git a/qcodes/plots/base.py b/qcodes/plots/base.py index 2c7dcc824dc..48d2b27728f 100644 --- a/qcodes/plots/base.py +++ b/qcodes/plots/base.py @@ -2,8 +2,27 @@ Live plotting in Jupyter notebooks ''' from IPython.display import display +import threading +import time +import logging from qcodes.widgets.widgets import HiddenUpdateWidget +from qcodes.utils.helpers import in_ipynb + + +class QCodesTimer(threading.Thread): + def __init__(self, fn, interval=1, **kwargs): + super().__init__(**kwargs) + self.fn = fn + self.interval = interval + + def run(self): + while 1: + logging.debug('QCodesTimer: start sleep') + time.sleep(self.interval) + # do something + logging.debug('QCodesTimer: run!') + self.fn() class BasePlot: @@ -27,8 +46,13 @@ def __init__(self, interval=1, data_keys='xyz'): self.interval = interval if interval: - self.update_widget = HiddenUpdateWidget(self.update, interval) - display(self.update_widget) + if in_ipynb(): + self.update_widget = HiddenUpdateWidget(self.update, interval) + display(self.update_widget) + else: + logging.info('create QCodesTimer: interval %.1f ' % interval) + self.update_widget = QCodesTimer(self.update, interval) + self.update_widget.start() def clear(self): ''' diff --git a/qcodes/utils/helpers.py b/qcodes/utils/helpers.py index ec98d01b4bc..e2d54c98202 100644 --- a/qcodes/utils/helpers.py +++ b/qcodes/utils/helpers.py @@ -5,8 +5,10 @@ import math import sys import io +import os import numpy as np import json +from IPython import get_ipython _tprint_times = {} @@ -25,20 +27,45 @@ def default(self, obj): else: return super(NumpyJSONEncoder, self).default(obj) + def tprint(string, dt=1, tag='default'): - """ Print progress of a loop every dt seconds """ + """Print progress of a loop every dt seconds.""" ptime = _tprint_times.get(tag, 0) if (time.time() - ptime) > dt: print(string) _tprint_times[tag] = time.time() +def is_interactive(): + """Return True if we are running in any interactive environment.""" + import __main__ as main + return not hasattr(main, '__file__') + + +def in_spyder(): + """Return True if we are running in the Spyder environment.""" + return bool(any('SPYDER' in name for name in os.environ)) + + +def in_ipynb(): + """Return True if we are running in an IPython / Jupyter notebook.""" + try: + cfg = get_ipython().config + if cfg['IPKernelApp']['parent_appname'] == 'ipython-notebook': + return True + else: + return False + except NameError: + return False + + def in_notebook(): - ''' - Returns True if the code is running with a ipython or jypyter + """ + Return True if we are running in ipython or jypyter. + This could mean we are connected to a notebook, but this is not guaranteed. see: http://stackoverflow.com/questions/15411967 - ''' + """ return 'ipy' in repr(sys.stdout)