A multiplexer for the Jupyter kernel protocol using JSON Lines (JSONL) over stdin/stdout.
Project description
cg-jupyter-mux
A command-line tool that acts as a multiplexer for the Jupyter kernel protocol. It communicates with a specified Jupyter kernel, reading JSON Lines messages from standard input and writing JSON Lines messages (from selected channels) to standard output.
This tool allows programmatic interaction with Jupyter kernels using a simple stdin/stdout interface, suitable for integration into other tools or automated workflows.
Features
- Starts Jupyter Kernels: Launches a kernel specified by name (e.g.,
python3,bash). - JSON Lines Interface: Reads input messages and writes output messages using the JSON Lines format (one valid JSON object per line).
- Async I/O: Uses
asynciofor non-blocking communication with the kernel and stdin/stdout. - Input Routing: Automatically routes incoming messages from stdin to the appropriate kernel channel (
shellorstdin) based on the message'sheader.msg_type. - Filtered Output: Forwards messages received from the kernel's
iopub(broadcast) andstdin(input requests/replies) channels to stdout. Messages from theshellchannel (direct replies) are intentionally not written to stdout. - Graceful Shutdown: Handles
Ctrl+Cand EOF on stdin to attempt a clean shutdown of the kernel. - Configurable Verbosity: Use
-vfor detailed debug logging to stderr.
Usage
Command
cg-jupyter-mux [-v] <kernel_name>
<kernel_name>(Required): The name of the installed Jupyter kernel spec to start (e.g.,python3). Runjupyter kernelspec listto see available kernel names.-v,--verbose(Optional): Enable verbose debug logging, printed to stderr.
Input Format (stdin)
The tool expects JSON Lines on standard input. Each line must be a single, complete, valid JSON object representing a Jupyter protocol message.
- Structure: Standard Jupyter message format (dictionary with
header,parent_header,metadata,content,buffers). - Routing: The tool inspects the
header.msg_typefield to decide which kernel channel (shellorstdin) to send the message to. Common types include:- Sent to
shellchannel:execute_request,kernel_info_request,inspect_request,complete_request,shutdown_request,comm_open,comm_msg,comm_close, etc. - Sent to
stdinchannel:input_reply
- Sent to
Example Input Line (execute_request):
{"header": {"msg_id": "exec-abc-123", "username": "cli_user", "session": "session-xyz-789", "date": "2025-04-07T23:40:00Z", "msg_type": "execute_request", "version": "5.3"}, "parent_header": {}, "metadata": {}, "content": {"code": "print('Hello Kernel!')\nresult=1+2\nresult", "silent": false, "store_history": true, "user_expressions": {}, "allow_stdin": false}, "buffers": []}
Output Format (stdout)
The tool produces JSON Lines on standard output. Each line is a single, complete, valid JSON object representing a message received from the kernel.
- Source Channels: Only messages from the kernel's
iopubchannel (status updates, execution results, output streams, display data, errors) andstdinchannel (input requests likeinput_request) are output. - Filtered Channels: Messages received by the client on the
shellchannel (e.g.,execute_reply,kernel_info_reply) are not printed to stdout. - Date Format:
datetimeobjects within messages (likeheader.date) are serialized to ISO 8601 strings.
Example Output Line (stream message from iopub):
{"header": {"msg_id": "...", "username": "kernel", "session": "session-xyz-789", "date": "2025-04-07T23:40:01.123456Z", "msg_type": "stream", "version": "5.3"}, "parent_header": {"msg_id": "exec-abc-123", ...}, "metadata": {}, "content": {"name": "stdout", "text": "Hello Kernel!\n"}, "buffers": [], "channel": "iopub"}
Example Output Line (execute_result message from iopub):
{"header": {"msg_id": "...", "username": "kernel", "session": "session-xyz-789", "date": "2025-04-07T23:40:01.234567Z", "msg_type": "execute_result", "version": "5.3"}, "parent_header": {"msg_id": "exec-abc-123", ...}, "metadata": {}, "content": {"data": {"text/plain": "3"}, "execution_count": 1}, "buffers": [], "channel": "iopub"}
Stopping the Multiplexer
- EOF: Close the standard input stream that's piping data to
cg-jupyter-mux. The tool will detect EOF and initiate a graceful shutdown. - Ctrl+C: Send an interrupt signal (
SIGINT). The tool traps this and initiates a graceful shutdown.
Troubleshooting
jupyter_client.kernelspec.NoSuchKernel: No such kernel named <name>: Ensure the required kernel package (e.g.,ipykernelforpython3) is installed in the same Python environment wherecg-jupyter-muxis run. Verify usingjupyter kernelspec list. See Installation section.- JSON Decode Errors: Input must be strictly one valid JSON object per line. Check for syntax errors or multi-line objects.
License
This project is licensed under the MIT License. See the LICENSE file (if present) or the license field in pyproject.toml for details.
Project details
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cg_jupyter_mux-0.2.0.tar.gz.
File metadata
- Download URL: cg_jupyter_mux-0.2.0.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8393735836b4441a6678c1df1e7930445027e1bb82c3226195ebdc12f50fb3d9
|
|
| MD5 |
faa215ff277213d0c7e9e7e71988b156
|
|
| BLAKE2b-256 |
7ef52025296d17c45d8c7de83b0750a3df7efe8f814837dff247cdec3fa5b2bc
|
Provenance
The following attestation bundles were made for cg_jupyter_mux-0.2.0.tar.gz:
Publisher:
production_build.yml on CodeGra-de/CodeGra.de
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cg_jupyter_mux-0.2.0.tar.gz -
Subject digest:
8393735836b4441a6678c1df1e7930445027e1bb82c3226195ebdc12f50fb3d9 - Sigstore transparency entry: 681524115
- Sigstore integration time:
-
Permalink:
CodeGra-de/CodeGra.de@d41f81310e22bb7aef0ff4e835e98854309a6391 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/CodeGra-de
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
production_build.yml@d41f81310e22bb7aef0ff4e835e98854309a6391 -
Trigger Event:
push
-
Statement type:
File details
Details for the file cg_jupyter_mux-0.2.0-py3-none-any.whl.
File metadata
- Download URL: cg_jupyter_mux-0.2.0-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2d62e0f6d3101e2fd1e0fc3a87c264186cf44ff9b45839b7ad121990dd0ae3a2
|
|
| MD5 |
1e69e62efca3e4b6d55fdf3514ef16dc
|
|
| BLAKE2b-256 |
b362856a6db8f994f575e8163251dbf8b46fb28d0022cac56c752f3b166d5c39
|
Provenance
The following attestation bundles were made for cg_jupyter_mux-0.2.0-py3-none-any.whl:
Publisher:
production_build.yml on CodeGra-de/CodeGra.de
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cg_jupyter_mux-0.2.0-py3-none-any.whl -
Subject digest:
2d62e0f6d3101e2fd1e0fc3a87c264186cf44ff9b45839b7ad121990dd0ae3a2 - Sigstore transparency entry: 681524261
- Sigstore integration time:
-
Permalink:
CodeGra-de/CodeGra.de@d41f81310e22bb7aef0ff4e835e98854309a6391 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/CodeGra-de
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
production_build.yml@d41f81310e22bb7aef0ff4e835e98854309a6391 -
Trigger Event:
push
-
Statement type: