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

Mention ipython_blocking in the async docs #2417

Open
jasongrout opened this issue May 21, 2019 · 8 comments
Open

Mention ipython_blocking in the async docs #2417

jasongrout opened this issue May 21, 2019 · 8 comments
Labels

Comments

@jasongrout
Copy link
Member

https://github.com/kafonek/ipython_blocking is a clever way to block execution on user input. It essentially monkey-patches the kernel execution on the kernel side to save execution messages until a condition is met, then executes the saved messages.

@kafonek - is it all right with you if we note that your package is another way to write a notebook where cell execution waits on user input? We would note that it is provided by a community package.

@kafonek
Copy link

kafonek commented May 21, 2019

@jasongrout yep that sounds great. I was planning on making a PR to the docs (and talk about potential improvements to the library) at the Dashboarding conference in two weeks. If you get to it before then, I'm sure we'll find other conversation topics.

I appreciate the kind words and mention, thanks.

@hainm
Copy link

hainm commented May 22, 2019

ipython_blocking is pretty handy. In current version of nglview (v2.1), user needs to sleep between cells to get the rendered image data.

# cell 1
x = view.render_image()

# cell 2
time.sleep(1)

# cell 3
print(x.value[:10])

with ipython_blocking, nglview doesn't need to sleep

# cell 1
c = CaptureExecution()
with c:
    x = view.render_image()
    while True:
        if x.value:
            break
        c.step()

# cell2 
print(x.value[:10])

By the way, it would be nicer if I can do this

with CaptureExecution() as c:
    bla bla
# `__enter__` just return `self`.

@kafonek
Copy link

kafonek commented May 22, 2019

@hainm, I can't think offhand how I would implement the with syntax there to accept different ways to break out of the context (e.g. on value change or on button push or on some other threshold). I think the %block magic addresses your use case of just setting up the context to exit when the widget has a .value.

# cell 1
import ipython_blocking # enables %block and %blockrun
x = view.render_image()

# cell 2
%block x

# cell 3
print(x.value[:10])

@hainm
Copy link

hainm commented May 22, 2019

hi @kafonek, thanks.
But your example only works if I run the whole notebook via "Restart & Run All".
If I run each cell manually, the notebook cell is frozen.

  • Run all

yes

  • Manually run

no

@hainm
Copy link

hainm commented May 22, 2019

If combining cell 1 and 2 into a single cell, your example work for both cases.

ok

@kafonek
Copy link

kafonek commented May 22, 2019

@hainm when you are running them manually, I think the image is getting rendered before you run the cell with %block x. The default behavior for %block is actually to capture cell execution until the value changes. You could instead pass in a function for just checking that the widget has a non-empty .value in general, %block lambda: x.value or a more verbose,

def exit_function():
    return x.value # or len(x.value) > 1 or some other validation function
%block exit_function

Your solution to put them in the same cell is also totally valid.

@hainm
Copy link

hainm commented May 24, 2019

thanks @kafonek, I am actually more interested in non-magic code so I could implement something in nglview. Here is an example using ipython_blocking under the hood. cheers.
nglviewer/nglview#811

@kafonek
Copy link

kafonek commented May 24, 2019

very cool, thanks for the heads up @hainm. cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants