ipython / ipykernel

IPython Kernel for Jupyter
https://ipykernel.readthedocs.io/en/stable/
BSD 3-Clause "New" or "Revised" License
653 stars 368 forks source link

[feature-request] Define custom startup code for specific kernel #1268

Closed gianfa closed 1 month ago

gianfa commented 2 months ago

Hello, it would be nice if one could define some code to execute at startup of a specific kernel, in a more elegant way. Currently, to my knowledge, the only direct way is by modifying the argv of the kernel.json associated with the specific kernel. This has the downside of being a bit hacky.

It would be nice to have a (yet another) config file, or an entrypoint in jupyter_lab_config.py. For example:

c.KernelExecute={
    "my_kernel_1": "~/jupyterlab/my_kernel-1.py"
}

Or, via CLI:

jupyter kernelspec execute -k my_kernel -f "~/jupyterlab/my_kernel-1.py"

Alternatively, a simple and clear tutorial on the subject might also be fine :)

Thank you for the great work behind Jupyter!

minrk commented 2 months ago

If you are using IPython, IPython has the notion of profiles, which are for this kind of thing. Profiles can load different configurations, including startup scripts (startup scripts can be placed in $(ipython profile locate [name])/startup/). The ipython kernel install command which installs kernelspecs lets you specify the profile to load.

For example, to create a profile called sample, add some startup scripts, and register it as a kernel:

# create new profile, named 'sample'
ipython profile create sample
# add any scripts to the `startup` directory, which load on IPython start
cp myscript.py $(ipython profile locate sample)/startup/myscript.py
# register this profile as its own kernel
ipython kernel install --sys-prefix --name sample-profile --profile sample
gianfa commented 1 month ago

oh, I see. It actually sounds like what I was looking for, but I didn't quite understand this segregation between profiles yet. Thanks so much for the clear example! (and for the great work behind jupyter!).