ines / juniper

🍇 Edit and execute code snippets in the browser using Jupyter kernels
https://ines.github.io/juniper/
MIT License
224 stars 38 forks source link

install requirements on page open #7

Open erohmensing opened 4 years ago

erohmensing commented 4 years ago

@ines thanks so much for this repo, it's really helpful for interactive documentation!

Is it possible to specify a parameter that will launch the container and download the requirements on page open, instead of when the runnable is first clicked? Our requirements take some time to install, so if we could do this in the background while they're reading content instead of making users wait ~a minute for everything to start up when they want to run the code, that would be ideal.

If its not possible, consider it a feature request 🙂

ashtonmv commented 3 years ago

Hi @erohmensing! Not sure if you are still interested in this, but I agree a background load is a generally useful idea. I couldn't find any "built-in" parameters, but one (imperfect) solution could be to add a function like this to your HTML file. It's basically Juniper's execute function, but without the "execute" part:

function startKernel(juniperInstance) {
    juniperInstance._event('requesting-kernel');
    if (juniperInstance._kernel && juniperInstance.isolateCells) {
        juniperInstance._kernel.restart();
    }
    new Promise((resolve, reject) =>
    juniperInstance.getKernel().then(resolve).catch(reject))
    .then(kernel => {
        juniperInstance._kernel = kernel;
    })
    .catch(() => {
        juniperInstance._event('failed');
        juniperInstance._kernel = null;
        if (juniperInstance.useStorage && typeof window !== 'undefined') {
            juniperInstance._fromStorage = false;
            window.localStorage.removeItem(juniperInstance.storageKey);
        }
    })
}

Then you can initialize your Juniper and start its kernel whenever you want (e.g. on document.ready):

$( document ).ready(function() {
    const juniper = new Juniper({
        your_juniper_settings
    });
    startKernel(juniper);
});

N.B. you might also wish to dispatch some messages to the console in order to convince yourself that anything is actually happening. In case you want to trigger something when your kernel is ready to go:

document.addEventListener('juniper', event => {
    if (event.detail.status == 'ready') {
        // You're all set! Do whatever you want here.
        }
    }
})

Hope this helps!

erohmensing commented 3 years ago

Much appreciated! Thanks!