IPython kernel forwarding C++ cells to clang-repl
Project description
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, "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
python -m venv .venv
source .venv/bin/activate
python -m pip install -e .
jupyter kernelspec list
# interactive console
jupyter console --kernel clang_repl
# demo notebook
jupyter notebook demo.ipynb
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
clang_repl-1.0.1.tar.gz
(5.7 kB
view details)
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 clang_repl-1.0.1.tar.gz.
File metadata
- Download URL: clang_repl-1.0.1.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c6916a79f9ab469016a31dce636c556a5913458871956e0f934dc459fe35866e
|
|
| MD5 |
3797f57d8530d63fdbf0247b215e5fa4
|
|
| BLAKE2b-256 |
7d1071bca421b9606dfc7c9af53caaf548538ad141eee9000afd547a6078b30f
|
File details
Details for the file clang_repl-1.0.1-py3-none-any.whl.
File metadata
- Download URL: clang_repl-1.0.1-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4eec6ef6ebe339adca55bcbf08e905fbd79f0574a67ff1cdc4b6d6a2528039e6
|
|
| MD5 |
5019e5fe723bc1e40da7bcd1b83b07a0
|
|
| BLAKE2b-256 |
c0bda70baadcf48055a7c1061bb29888d7255122eca2678b0fcc571b4205de58
|