clang-repl based kernel for Jupyter notebooks
Overview
- This is a kernel enabling using C++ in a Jupyter Notebook
- It bases on a minimalistic instrumentation of the interactive clang-repl prompt using
- this showcase for
ipykernel.kernelbase.kernelas blueprint for the overall project structure, and python-pexpectfor instrumenting theclang-repl>prompt.
- this showcase for
Motivation
- The motivation is to enable notebook-based learning material for C++.
- The focus is not on language interoperability between C++ and Python.
- Exploring the latest capabilities of
clang-replusing different C++ language standards/settings in a convenient way. - Existing projects providing a form of interactive C++ in notebooks do currently not support C++20/C++23 and use fixed/patched LLVM versions.
Details
- It is required that
clang-replis installed on the (backend) system - On launch, the kernel starts an interactive
clang-replsession. - The default settings and initial includes/libs can be configured by placing a
.clang-replfile in the users home directory, the defaults (if not.clang-replfile is present) are listed below[defaults] repl = "clang-repl" args = ["-std=c++20", "-ferror-limit=3", "-O1"] includes = ["vector", "iostream"] libs = [] timeout = 10 debug = false
- The kernel performs the following steps for each source cell
0. Check if the
clang-replsession is alive- Inspect first line of the cell if starting with these magic commands
%status: print kernel status%lib: forward first line of cell directly toclang-repl
- Comment the first line (by prepending
//) if it starts with% - Transform the cell content if the first line contains a
%main: the cell content is wrapped and run via a unique global function, e.g.void mainUUID(){ ... }; mainUUID(); - The (transformed) cell content is forwarded to
clang-replby always using a single line command realized via a indirection of, e.g. this form:#include /tmp/cell-e3tp24ne.repl - The result of the interactive session (i.e. incremental compile + execute) is awaited (using a timeout) and printed as output of the cell.
- If the cell additionally contained a
%undoin the first line (and the incremental compile + execute was successful) the cell is "undone" via sending a subsequent%undodirectly toclang-repl
- Inspect first line of the cell if starting with these magic commands
Installation
python -m venv .venv
source .venv/bin/activate
python -m pip install clang-repl
# demo notebook
jupyter notebook demo.ipynb
# new empty notebook
echo '{ "cells": [], "nbformat": 4, "nbformat_minor": 4, "metadata": {} }' > empty.ipynb
jupyter notebook --MultiKernelManager.default_kernel_name=clang_repl empty.ipynb
Installation (for development)
git clone https://github.com/pmanstet/clang_repl_kernel.git
cd clang_repl_kernel
# optional: use a fixed python version
# pyenv install 3.11.10
# ~/.pyenv/versions/3.11.10/bin/python -m venv .venv
python -m venv .venv
source .venv/bin/activate
python -m pip install -e .
jupyter kernelspec list
# interactive console
jupyter console --kernel clang_repl --debug
# demo notebook
jupyter notebook demo.ipynb
Metadata
Release files for clang-repl 1.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| clang_repl-1.0.4.tar.gz | 8.7 kB | Without provenance | Details for clang_repl-1.0.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| clang_repl-1.0.4-py3-none-any.whl | Python 3 | none | any | Details for clang_repl-1.0.4-py3-none-any.whl |
Total release size: 18.5 kB
Release files / clang_repl-1.0.4.tar.gz
| Download URL | clang_repl-1.0.4.tar.gz |
|---|---|
| Size | 8.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c1ef942e8e4bd30552016063b3821cc94cc3e74344fd7c47424d2e38c97bea97
|
|
BLAKE2b-256 checksum How to use checksums |
11cce4fa8e96d672148bc579a806cc2b83dbed9d80f9bf8c04815b66233f46e4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.1
|
Release files / clang_repl-1.0.4-py3-none-any.whl
| Download URL | clang_repl-1.0.4-py3-none-any.whl |
|---|---|
| Size | 9.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d8083e45f970c16371c9b87cff9bcde10fc54299b0b529cbaaa4f0eb6246173b
|
|
BLAKE2b-256 checksum How to use checksums |
94c804c18e27da0adbe17dc21cbc4c2c6e9c13be7899cb14864cdbbb697aa4d9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.1
|
Security for clang-repl 1.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.