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

How to run automatic demos based on Jupyter #228

Open
nthiery opened this issue Mar 22, 2017 · 15 comments
Open

How to run automatic demos based on Jupyter #228

nthiery opened this issue Mar 22, 2017 · 15 comments

Comments

@nthiery
Copy link
Contributor

nthiery commented Mar 22, 2017

For ODK's formal review (and certainly elsewhere), we will want to have self-running demos that our reviewers can glance through. A nice way to author such demos would be to produce a collection of Jupyter notebooks. And then have some "run demo" gadget that would take each notebook in turn, and execute the cells one by one at a slow pace (every 10s maybe?). And then pause longer and switch to the next demo. That ideally with the option to pause, play around with the notebook (hopefully not breaking it too much), and restart the demo.

Does something like this already exist? If not, is this something that could be implemented before the project review (last week of April)?

We discussed this a bit with @defeo; one way to model the "collection of notebooks" would be to put them all in a folder. Another would be to build a linked list (possibly going in round) by addinng an hyperlink at the end of each notebook pointing to the next one.

cc: @minrk, @takluyver (feel free to redirect that discussion to some appropriate Jupyter channel).

@nthiery
Copy link
Contributor Author

nthiery commented Mar 22, 2017

This could actually be relevant for #151.

@minrk
Copy link
Contributor

minrk commented Mar 22, 2017

I'm not aware of such a thing. The slowly step through the notebook (ignoring user intervention) is pretty simple. Switching to other notebooks and allowing it to pause/resume when users interact with it would be a bit more complicated.

@defeo
Copy link
Contributor

defeo commented Mar 22, 2017 via email

@minrk
Copy link
Contributor

minrk commented Mar 22, 2017

Here's a bit of javascript that will cause the notebook to step through each cell every ten seconds until the end:

function step() {
    // don't do anything if explicitly paused
    // or another window has focus
    if (window.paused || !document.hasFocus()) {
        return;
    }
    require(['base/js/namespace'], function (Jupyter) {
        var nb = Jupyter.notebook;
        if (nb.keyboard_manager.mode === 'edit') {
            // don't auto-execute while a cell is being edited
            return;
        }
        if (nb.get_selected_index() + 1 === nb.ncells()
            && nb.get_selected_cell().get_text().trim() === '') {
            // at the end
            console.log("at end");
        } else {
            Jupyter.notebook.execute_cell_and_select_below();
        }
    });
}

if (window.stepInterval) {
    clearInterval(window.stepInterval);
    delete window.stepInterval;
}

window.stepInterval = setInterval(step, 10000);

This could be loaded in an extension to run such a demo for a single notebook. Switching between tabs automatically would be more challenging without running afoul of pop-up blockers.

@nthiery
Copy link
Contributor Author

nthiery commented Mar 22, 2017 via email

@minrk
Copy link
Contributor

minrk commented Mar 22, 2017

@nthiery Sure. I'll try to put up a demo notebook soon.

@nthiery
Copy link
Contributor Author

nthiery commented Mar 23, 2017 via email

@jdemeyer
Copy link
Contributor

It might be useful to have support for ipywidgets too, for example showing some interact and change the widgets. I know that one can easily modify widgets from Javascript, so this shouldn't be too hard.

@nthiery
Copy link
Contributor Author

nthiery commented Mar 24, 2017 via email

@minrk
Copy link
Contributor

minrk commented Mar 24, 2017

@nthiery if you use interactive instead of interact, you have a handle on the widgets.

@nthiery
Copy link
Contributor Author

nthiery commented Mar 24, 2017

I was precisely trying this out; thanks for the tip though! This indeed works smoothly:

    from ipywidgets import interact, interactive
    from IPython.display import display
    import time

    def f(z=(0,10)):
        return z^2
    I = interactive(f); display(I)
    slider = I.children[0]
    for i in range(10): time.sleep(1); slider.value=i

This clutters a tiny bit the notebook but nothing bad.

@jdemeyer
Copy link
Contributor

You can do it with @interact too:

from ipywidgets import interact
import time

@interact
def f(z=(0,10)):
    return z^2
slider = f.widget.children[0]
for i in range(10): time.sleep(1); slider.value=i

@nthiery
Copy link
Contributor Author

nthiery commented Mar 24, 2017 via email

@minrk
Copy link
Contributor

minrk commented Apr 12, 2017

I made this as an example of stepping through a notebook automatically.

@fangohr
Copy link
Member

fangohr commented Apr 19, 2017

@minrk - it just works! Very cool. 👍

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

5 participants