Skip to content

Add bootstrapCode config option #172

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

Closed
wants to merge 1 commit into from
Closed

Conversation

juntyr
Copy link
Contributor

@juntyr juntyr commented Feb 27, 2025

The new bootstrapCode extension config option allows JupyterLite hosts to run custom Python code before the kernel starts. This should help downstream projects with the commonly wanted feature of "running code before the first cell".

Related to #88

Copy link
Contributor

lite-badge 👈 Try it on ReadTheDocs

@juntyr
Copy link
Contributor Author

juntyr commented Mar 6, 2025

ping @jtpio

@jtpio
Copy link
Member

jtpio commented Mar 12, 2025

Thanks @juntyr.

Having it implemented via such option sounds like a fine approach.

Also when there is something to help better see the logs (jupyterlite/jupyterlite#1498), it will be easier for users to troubleshoot issues if something goes wrong with this extra bootstrapCode.

@bollwyvl
Copy link
Contributor

Again, this is a feature that also does not exist in JupyterLab, and for which there are user space extensions.

For "pre-installing" packages, there are also other approaches such as jupyterlite-pyodide-lock, which put everything known packages into pyodide-lock.json such that they will be imported automatically.

@juntyr
Copy link
Contributor Author

juntyr commented Mar 12, 2025

Again, this is a feature that also does not exist in JupyterLab, and for which there are user space extensions.

Could you please point towards one? If there’s an existing extension that can do that I’d be very happy to use it and close this PR.

For "pre-installing" packages, there are also other approaches such as jupyterlite-pyodide-lock, which put everything known packages into pyodide-lock.json such that they will be imported automatically.

That just preloads them, but doesn’t run them. What this PR tries to provide is a way to run custom code during bootstrap, which is not yet possible otherwise (as far as I know). At the moment, I have to use some very hacky patches to IPython to get some code to run after the interpreter is setup but before the user gains control. This is very valuable for applying behind-the-scenes magic that the user doesn’t need to see.

@bollwyvl
Copy link
Contributor

Here is a discourse discussion with more context (and related links at the bottom):

https://discourse.jupyter.org/t/setting-initialization-cells-for-jupyterlab/13345

@juntyr
Copy link
Contributor Author

juntyr commented Mar 12, 2025

Are any of these solutions not tied to notebooks? I’m looking for something that can be configured by the host of JupyterLite, not something that needs to be configured by the notebook author

@juntyr juntyr closed this Mar 14, 2025
@juntyr juntyr deleted the bootstrap branch March 14, 2025 08:36
@jtpio
Copy link
Member

jtpio commented Mar 14, 2025

@juntyr would you still like to document your approach, for example in https://jupyterlite.readthedocs.io/en/stable/howto/index.html#configuring-the-pyodide-kernel? So it can be useful to other people too.

@juntyr
Copy link
Contributor Author

juntyr commented Mar 14, 2025

Right now the approach requires controlling the Pyodide build, as I need to make emscripten embed the IPython config files into the filesystem. The rest is already documented by https://modelpredict.com/how-to-write-reliable-scripts-ipython/, I just copied its code.

So in the end we have:

  1. I now have an assets folder in my Pyodide fork here https://github.com/climet-eu/pyodide/tree/058717a2b1311b9efbd3166e5c5ff35a12d75bf6/assets, which contains files that should be included in the filesystem
  2. I modified Pyodide's Makefile to embed the folder here https://github.com/climet-eu/pyodide/blob/058717a2b1311b9efbd3166e5c5ff35a12d75bf6/Makefile#L129
  3. The asset folder contains an ipython_config.py file that adds the extensions here https://github.com/climet-eu/pyodide/blob/058717a2b1311b9efbd3166e5c5ff35a12d75bf6/assets/home/pyodide/.ipython/profile_default/ipython_config.py
  4. The extension, which can be a file in the same assets folder, exposes the following interface
def load_ipython_extension(ip):
    # do stuff here

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

Successfully merging this pull request may close these issues.

3 participants