From 5c070d0b9507d4d99abdcb58ddf26d9636c022dc Mon Sep 17 00:00:00 2001 From: eendebakpt Date: Mon, 9 May 2016 10:42:45 +0200 Subject: [PATCH 1/5] make plot updates work outside ipython notebook --- qcodes/plots/base.py | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/qcodes/plots/base.py b/qcodes/plots/base.py index 0b7830579346..644e09142ead 100644 --- a/qcodes/plots/base.py +++ b/qcodes/plots/base.py @@ -5,6 +5,36 @@ from qcodes.widgets.widgets import HiddenUpdateWidget +import threading + + +def in_ipynb(): + try: + cfg = get_ipython().config + if cfg['IPKernelApp']['parent_appname'] == 'ipython-notebook': + return True + else: + return False + except NameError: + return False + +import time +import logging +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: ''' @@ -25,8 +55,13 @@ def __init__(self, interval=1, data_keys='xyz'): if interval: self.interval = 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 add(self, *args, updater=None, **kwargs): ''' From 7b07d7a15c7e87bdb656610ec862c0e9142f9ca8 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Tue, 10 May 2016 12:08:19 +0200 Subject: [PATCH 2/5] add checks on environment --- qcodes/utils/helpers.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/qcodes/utils/helpers.py b/qcodes/utils/helpers.py index bcad2bbae92f..561c5472494c 100644 --- a/qcodes/utils/helpers.py +++ b/qcodes/utils/helpers.py @@ -9,6 +9,14 @@ import multiprocessing as mp +def is_spyder(): + ''' Return True if we are running in the Spyder environment ''' + return bool(any('SPYDER' in name for name in os.environ)) + +def is_interactive(): + import __main__ as main + return not hasattr(main, '__file__') + def in_notebook(): ''' is this code in a process directly connected to a jupyter notebook? From 16407b98f500a4836c2469c1330c32a09c68f967 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Tue, 10 May 2016 12:12:17 +0200 Subject: [PATCH 3/5] added helper functions --- qcodes/plots/base.py | 11 ++--------- qcodes/utils/helpers.py | 10 ++++++---- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/qcodes/plots/base.py b/qcodes/plots/base.py index 644e09142ead..b3c33a030e47 100644 --- a/qcodes/plots/base.py +++ b/qcodes/plots/base.py @@ -4,19 +4,12 @@ from IPython.display import display from qcodes.widgets.widgets import HiddenUpdateWidget +from qcodes.utils import in_notebook, in_ipynb, in_spyder import threading -def in_ipynb(): - try: - cfg = get_ipython().config - if cfg['IPKernelApp']['parent_appname'] == 'ipython-notebook': - return True - else: - return False - except NameError: - return False + import time import logging diff --git a/qcodes/utils/helpers.py b/qcodes/utils/helpers.py index 561c5472494c..448411bc2327 100644 --- a/qcodes/utils/helpers.py +++ b/qcodes/utils/helpers.py @@ -7,16 +7,18 @@ import sys import io import multiprocessing as mp +import os -def is_spyder(): - ''' Return True if we are running in the Spyder environment ''' - return bool(any('SPYDER' in name for name in os.environ)) - def is_interactive(): 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_notebook(): ''' is this code in a process directly connected to a jupyter notebook? From 9859784ca5b9e30369f153cc82bbf3d3865b4206 Mon Sep 17 00:00:00 2001 From: alexcjohnson Date: Tue, 31 May 2016 23:16:24 +0200 Subject: [PATCH 4/5] lint --- qcodes/plots/base.py | 12 ++++-------- qcodes/utils/helpers.py | 14 +++++++++----- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/qcodes/plots/base.py b/qcodes/plots/base.py index 129f595590dd..581ab14dbd47 100644 --- a/qcodes/plots/base.py +++ b/qcodes/plots/base.py @@ -2,19 +2,15 @@ Live plotting in Jupyter notebooks ''' from IPython.display import display - -from qcodes.widgets.widgets import HiddenUpdateWidget -from qcodes.utils import in_notebook, in_ipynb, in_spyder - import threading +import time +import logging +from qcodes.widgets.widgets import HiddenUpdateWidget +from qcodes.utils import in_ipynb - -import time -import logging class QCodesTimer(threading.Thread): - def __init__(self, fn, interval=1, **kwargs): super().__init__(**kwargs) self.fn = fn diff --git a/qcodes/utils/helpers.py b/qcodes/utils/helpers.py index a66ea0d7894a..1961ae6de9f8 100644 --- a/qcodes/utils/helpers.py +++ b/qcodes/utils/helpers.py @@ -26,8 +26,9 @@ 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) @@ -35,20 +36,23 @@ def tprint(string, dt=1, tag='default'): 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 True if we are running in the Spyder environment.""" return bool(any('SPYDER' in name for name in os.environ)) + 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) From 7c77790dfc96c8a2ad8698ce4fafe487a9268343 Mon Sep 17 00:00:00 2001 From: alexcjohnson Date: Tue, 31 May 2016 23:48:09 +0200 Subject: [PATCH 5/5] bring back in_ipynb --- qcodes/plots/base.py | 2 +- qcodes/utils/helpers.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/qcodes/plots/base.py b/qcodes/plots/base.py index 581ab14dbd47..48d2b27728fc 100644 --- a/qcodes/plots/base.py +++ b/qcodes/plots/base.py @@ -7,7 +7,7 @@ import logging from qcodes.widgets.widgets import HiddenUpdateWidget -from qcodes.utils import in_ipynb +from qcodes.utils.helpers import in_ipynb class QCodesTimer(threading.Thread): diff --git a/qcodes/utils/helpers.py b/qcodes/utils/helpers.py index 1961ae6de9f8..e2d54c982022 100644 --- a/qcodes/utils/helpers.py +++ b/qcodes/utils/helpers.py @@ -8,6 +8,7 @@ import os import numpy as np import json +from IPython import get_ipython _tprint_times = {} @@ -46,6 +47,18 @@ def in_spyder(): 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(): """ Return True if we are running in ipython or jypyter.