Skip to content

Commit 65e72d6

Browse files
committed
Move rate to rate_control and notebook check into separate file
1 parent 340fe0c commit 65e72d6

4 files changed

Lines changed: 50 additions & 0 deletions

File tree

‎vpython/_notebook_helpers.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def __checkisnotebook(): # returns True if running in Jupyter notebook
2+
try:
3+
shell = get_ipython().__class__.__name__
4+
if shell == 'ZMQInteractiveShell': # Jupyter notebook or qtconsole?
5+
return True
6+
elif shell == 'TerminalInteractiveShell': # Terminal running IPython?
7+
return False
8+
else:
9+
return False # Other type (?)
10+
except NameError:
11+
return False # Probably standard Python interpreter
12+
13+
_isnotebook = __checkisnotebook()

‎vpython/no_notebook.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import signal
1313
from urllib.parse import unquote
1414

15+
from .rate_control import rate
16+
1517

1618
# Check for Ctrl+C
1719
def signal_handler(signal, frame):

‎vpython/rate_control.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
import sys
44
import platform
55

6+
from ._notebook_helpers import _isnotebook
7+
8+
if _isnotebook:
9+
import IPython
10+
611
# Unresolved bug: rate(X) yields only about 0.8X iterations per second.
712

813
MIN_RENDERS = 10
@@ -210,3 +215,32 @@ def __call__(self, maxRate=100):
210215
self.rateCount += 1
211216

212217
self.lastEndRate = _clock()
218+
219+
220+
class _RateKeeper2(RateKeeper):
221+
222+
def __init__(self, interactPeriod=INTERACT_PERIOD, interactFunc=simulateDelay):
223+
self.rval = 30
224+
self.tocall = None
225+
super(_RateKeeper2, self).__init__(interactPeriod=interactPeriod, interactFunc=self.sendtofrontend)
226+
227+
def sendtofrontend(self):
228+
# This is called by the rate() function, through rate_control _RateKeeper callInteract().
229+
# See the function commsend() for details of how the browser is updated.
230+
231+
# Check if events to process from front end
232+
if _isnotebook:
233+
if IPython.__version__ >= '3.0.0' :
234+
kernel = get_ipython().kernel
235+
parent = kernel._parent_header
236+
ident = kernel._parent_ident
237+
kernel.do_one_iteration()
238+
kernel.set_parent(ident, parent)
239+
240+
def __call__(self, N): # rate(N) calls this function
241+
self.rval = N
242+
if self.rval < 1: raise ValueError("rate value must be greater than or equal to 1")
243+
super(_RateKeeper2, self).__call__(self.rval) ## calls __call__ in rate_control.py
244+
245+
# The rate function:
246+
rate = _RateKeeper2(interactFunc = simulateDelay(delayAvg = 0.001))

‎vpython/vpython.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import queue
2828
import json
2929
from . import __version__, __gs_version__
30+
from ._notebook_helpers import _isnotebook
3031

3132
vec = vector # synonyms in GlowScript
3233
ws_queue = queue.Queue()

0 commit comments

Comments
 (0)