Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make plot updates work outside ipython notebook #155

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions qcodes/plots/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we support .halt() as the update widget does? I'm not sure if we make the use of this everywhere we should right now in notebooks, but there should be some way to stop the updater when it's no longer needed.


class BasePlot:
Expand All @@ -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):
'''
Expand Down
35 changes: 31 additions & 4 deletions qcodes/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand All @@ -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':

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)


Expand Down