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

Support reading other properties than value? #6

Closed
bryceschober opened this issue Oct 2, 2020 · 3 comments
Closed

Support reading other properties than value? #6

bryceschober opened this issue Oct 2, 2020 · 3 comments

Comments

@bryceschober
Copy link

bryceschober commented Oct 2, 2020

Now my problem is that I want to use it with from ipyfilechooser import FileChooser, which doesn't inherit from ValueWidget or have a value property or something:

/opt/conda/lib/python3.7/site-packages/ipython_blocking/ipython_magic.py in block(self, line)
     57             func = lambda: obj._has_been_clicked
     58         else:
---> 59             raise Exception('The positional argument to %block should be a ValueWidget, Button, or a function/method')
     60         return self.capture(func, args.timeout)
     61 

If there were some way to point at a property: %block myobject.property, and somehow introspect that it was indeed a value or property to do change detection on, then it could be a good general-purpose solution. Also, FileChooser should probably actually inherit and provide a value, but that's another matter.

Yes, I know I can write a change-detecting function, as per the workaround in #5:

# previous cell
config_selector = FileChooser(str(root_working_dir), use_dir_icons=True)
display(config_selector)

# next cell
ctx = ipython_blocking.CaptureExecution()
with ctx:
    init_val = config_selector.selected
    while True:
        if init_val != config_selector.selected:
            break
        ctx.step()

...but I was hoping for an easy-to-read magic one-liner.

@bryceschober bryceschober changed the title Support reading other properties than value? Support reading other properties than value? Oct 2, 2020
@bryceschober
Copy link
Author

@kafonek After I requested it, ipyfilechooser now inherits from ValueWidget and implements get_interact_value(), but that seems to not be enough for ipython_blocking...

@kafonek
Copy link
Owner

kafonek commented Oct 7, 2020

@bryceschober for any kind of situation where you're working with non-standard widgets or have some complex validation (like multiple widgets being filled out), the best thing to do with ipython_blocking is define a function to block on. Basically at every step (kernel.do_one_iteration()) it will run your blocking function. If the function returns True, it breaks out of the blocking context. If not, it stays in the blocking context.

For FileChooser, try this out:

### cell 1
import ipyfilechooser
fc = ipyfilechooser.FileChooser()

def block_until_selected():
    return bool(fc.selected)

fc
### cell 2
import ipython_blocking

%block block_until_selected
### cell 3
### will not run until a file is selected in cell 1
fc.selected

@bryceschober
Copy link
Author

Yep, that ended up being simpler, thanks.

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

No branches or pull requests

2 participants