Compose simple and intuitive Jupyter server extension hooks.
Project description
Jupyter Kernel Hook
A simple way to turn your package into a Jupyter server extension that automatically preload into an IPython kernel.
You can install this package via pip install jupyter-kernel-hook
.
Example
Add the following to your package's __init__.py
:
# my_package/__init__.py
def _jupyter_server_extension_paths():
return [{
"module": "my_package"
}]
def load_jupyter_server_extension(nb_app):
from jupyter_kernel_hook import create_startup_script
create_startup_script(
nb_app,
"my_package"
)
Now when a user runs jupyter serverextension enable --py my_package
, your package will be enabled as a server extension that is preloaded within every IPython kernel. So now the user does not need to run import my_package
to run it for each kernel session.
Features
- Simplicity.
jupyter_kernel_hook
does for you the annoying part of preloading your packages in a sane way. - Intuitive behavior. Your startup script can be enabled and disabled through
jupyter serverextension enable --py my_package
.- When your extension is enabled, it will preload (and invoke an optional function call) in each IPython kernel.
- When your extension is disabled, it is never preloaded / imported.
- Performance. Everything is lazy-loaded to reduce overhead and required dependencies. (
jupyter_kernel_hook
is imported in various contexts-- the server starting up, kernels starting up-- and different contexts require different things.)
Advanced
Preload IPython Extension
Jupyter Kernel Hook is used to make Jupyter server extensions. A natural use case, though, is to turn a native IPython extension into a Jupyter Notebook extension, such as those that add cell magic to the IPython runtime.
You can call a load_ipython_extension
function like this:
# my_package/__init__.py
from .core import MyPackageMagics
def _jupyter_server_extension_paths():
return [{
"module": "my_package"
}]
def load_jupyter_server_extension(nb_app):
from jupyter_kernel_hook import create_startup_script
create_startup_script(
nb_app,
"my_package:load_ipython_extension(ip)"
)
def load_ipython_extension(ip):
ip.register_magics(MyPackageMagics)
And the startup script will make sure that the ip
object is defined.
(Note: You can run any arbitrary function call, not just load_ipython_extension
.)
Add Package to Kernel's Global Namespace
By default, the global namespace of the IPython kernel is untouched by the generated script.
You can, however, add the package to the global namespace like this:
create_startup_script(
nb_app,
"my_package:load_ipython_extension(ip)",
add_to_globals=True
)
In this case, my_package
will be available in the IPython kernel's globals.
(Warning: This doesn't work for nested packages/modules at the moment. Do that at your own risk.)
Jupyter Entry Point in Setup.py
This is a Jupyter thing in general, but it's quite handy for your packages.
Basically you can make it so your package, when installed, automatically enables the server extension. In conjunction with Jupyter Kernel Hook, this means your IPython extensions are automatically enabled with just a pip install!
Read more about this in the official Jupyter Notebook documentation.
my_package/__init__.py
:
(See above examples.)
setup.py
:
from setuptools import setup
# ...
setup(
# ...
install_requires=[
# ...
"jupyter-kernel-hook"
],
# ...
include_package_data=True,
data_files=[
("etc/jupyter/jupyter_notebook_config.d", [
"my_package_jupyter/my_package_jupyter_config.json"
])
],
zip_safe=False
# ...
)
my_package/my_package_jupyter_config.json
:
{
"NotebookApp": {
"nbserver_extensions": {
"my_package": true
}
}
}
MANIFEST.in
:
include my_package/my_package_jupyter_config.json
The Spider-Man Rule
With great power comes great responsibility.
The power to preload packages into IPython kernels is powerful and convenient at times, but it's also extremely easy to abuse.
It is recommended that you be very judicious about whether your package should have this behavior.
In general, merely wanting to make a package available in the global namespace is probably not a good reason to have your package preload into a kernel. Extending the behavior of the kernel (e.g. cell magic) or packages run within it (e.g. Pandas extensions) are better reasons for having this behavior.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file jupyter_kernel_hook-0.1.1.tar.gz
.
File metadata
- Download URL: jupyter_kernel_hook-0.1.1.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 14ad740d19c1eeaa41a40992146b71fc6afac7cfffcb063c0b6b971034d011f8 |
|
MD5 | f9f8f0a67f39aa6e71cb04996db88a4b |
|
BLAKE2b-256 | 6a2be3c1f3d4bdff34da165479e849959ad30897b0e5d4f259d14f4ed99ca706 |
File details
Details for the file jupyter_kernel_hook-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: jupyter_kernel_hook-0.1.1-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf47bf8c34426115f89a86c2d09de35dca199bdabcf4f6792fb2b67535fd238f |
|
MD5 | b630ddcd7cee0b49fa3642af9cdb37be |
|
BLAKE2b-256 | 82c55d5b739fef3645d7bc032bef0281d1fa7c4027e71f757153c7a4c6de99e1 |