PKR!Mp^ipython_blocking.py""" ipython_blocking offers a context manager and IPython magic to capture cell execution. That is useful when you want a "blocking widget" or other situations where cell execution is paused until some trigger is met (like widget form validation) """ import sys import time import types import ipywidgets as widgets from IPython.core import magic_arguments from IPython.core.magic import ( Magics, magics_class, line_magic, ) __version__ = '0.1.1.post1' ### General context manager usage: # ctx = CaptureExecution() # with ctx: # while True: # if : # break # ctx.step() class CaptureExecution: "A context manager to capture execute_request events then replay them after exiting the manager" def __init__(self): self.captured_events = [] self.shell = get_ipython() self.kernel = self.shell.kernel def step(self): self.kernel.do_one_iteration() def capture_event(self, stream, ident, parent): "A 'capture' function to register instead of the default execute_request handling" self.captured_events.append((stream, ident, parent)) def start_capturing(self): "Overwrite the kernel shell handler to capture instead of executing new cell-execution requests" self.kernel.shell_handlers['execute_request'] = self.capture_event def stop_capturing(self): "revert the kernel shell handler to the default execute_request behavior" self.kernel.shell_handlers['execute_request'] = self.kernel.execute_request def replay_captured_events(self): "Called at end of context -- replays all captured events once the default execution handler is in place" # need to flush before replaying so messages show up in current cell not replay cells sys.stdout.flush() sys.stderr.flush() for stream, ident, parent in self.captured_events: # Using kernel.set_parent is the key to getting the output of the replayed events # to show up in the cells that were captured instead of the current cell self.kernel.set_parent(ident, parent) self.kernel.execute_request(stream, ident, parent) self.captured_events = [] def __enter__(self): self.start_capturing() self.shell.execution_count += 1 # increment execution count to avoid collision error def __exit__(self, exc_type, exc_value, traceback): if exc_type is not None: # let the error propogate up, such as a keyboard interrupt while capturing cell execution return False self.stop_capturing() self.replay_captured_events() @magics_class class CaptureMagic(Magics): def capture(self, breaking_func, timeout=None): if timeout: timeout = int(timeout) start = time.time() ctx = CaptureExecution() with ctx: while True: if breaking_func(): break if timeout: if (time.time() - start) >= timeout: break ctx.step() @line_magic @magic_arguments.magic_arguments() @magic_arguments.argument('break_value', help='Widget object or function that defines a break from the blocking context') @magic_arguments.argument('-t', '--timeout', default=None, help="Timeout in seconds to stop capturing") def block(self, line): line = line.strip() args = magic_arguments.parse_argstring(self.block, line) obj = get_ipython().user_ns[args.break_value] ### Support the following options for a break value: ### 1) a callable function that will break when the function returns True ### 2) a ValueWidget, which will break when the value changes ### 3) a ButtonWidget, which will break when it is clicked if isinstance(obj, (types.FunctionType, types.MethodType)): func = obj elif isinstance(obj, widgets.ValueWidget): starting_value = obj.value func = lambda: obj.value != starting_value elif isinstance(obj, widgets.Button): obj._has_been_clicked = False def handler(w): w._has_been_clicked = True obj.on_click(handler) func = lambda: obj._has_been_clicked else: raise Exception('The positional argument to %block should be a ValueWidget, Button, or a function/method') return self.capture(func, args.timeout) def load_ipython_extensions(): get_ipython().register_magics(CaptureMagic) PKQM3I.ipython_blocking-0.1.1.post1.dist-info/LICENSEBSD 3-Clause License Copyright (c) 2018, Matt Kafonek All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENC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'֩P²0SVL}8:ãiZq˻S) j#!B< w~ P[ ߂J`Ķ .Cd e?ciHhb;%%,5(%7< f|Y86yU 0W q̸u^m)-Z@laja 5V7F.ӐS9 CRƇɰCS|cFg'Q6C!X k+J@yU-:l)?i86"RÅ@XR`j\Mf' 5m) NYvpp<ı2^{ieèr Eh)B-p 7 u\X1ҍu6C67nE )[B½dP>ItT~=h|ëFYDqR4 4.UhʘSXE3pE%&:}P0?ѿ4k$|0q[Q wE&C}w\'?Y,}]+nHڮjv e2N6zE+3h}x8E}%K0(("7Q\ЏGaP4'LLPT`:e3hPPS9oER o.>!d8h'UE@J"p3VPG"hPd ›9\4<D8U `VoSC$ADoA(wN9`&#<$?Ņa eoJ:3Ѹ杻O !"UY*#&q1r>:`zf:ιMA#GބT ¨NP]V+́#;;3%0ԥ<a %(9 %8D 6%;.H!ɒ Nybt5::Lq(.9k;,F kFzcFrLpnzdʁ2vHv`N̆ltNP QDgmvϸ^hLim!fi^| onLos+&Xq!q>Lӹվєtz~}P@% ̚Fې:'*{:ڃvH x 0}fpԳ֊&Cjd C݁denRUG lV观6⚱ePvPK!Ho-ipython_blocking-0.1.1.post1.dist-info/RECORDлr@gY+R QP`Q„ OToĪƤi{MK@_%iP; v2$!*QÞ)8"oW'䳺g\M2uhf4~9N!}pۨyRF^bC{7\\Y3riZloU`Ir!"K1*m*K aа6+$J86^GZ̯8b %U*\gPKR!Mp^ipython_blocking.pyPKQM3I.ipython_blocking-0.1.1.post1.dist-info/LICENSEPK!HJVSa,"ipython_blocking-0.1.1.post1.dist-info/WHEELPK!Hv5 "/ipython_blocking-0.1.1.post1.dist-info/METADATAPK!Ho-'ipython_blocking-0.1.1.post1.dist-info/RECORDPKb(