Skip to content

Commit

Permalink
[llvm][TableGen][Jupyter] Record current python when kernel is installed
Browse files Browse the repository at this point in the history
Previously the kernel.json would always point to `python3` even if you
installed using a python from a virtualenv. This meant that tools like VSCode
would try to run the kernel against the system python and fail.

Added a note to the readme about it. I've removed the need to
add to PYTHONPTHON as well, turns out it wasn't needed.

This fixes an issue reported in https://discourse.llvm.org/t/tablegen-the-playground-ipynb-file-is-not-working-as-expected/71745.

Reviewed By: awarzynski

Differential Revision: https://reviews.llvm.org/D154351
  • Loading branch information
DavidSpickett committed Jul 5, 2023
1 parent b9808e5 commit 535693f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
12 changes: 5 additions & 7 deletions llvm/utils/TableGen/jupyter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ that is not possible, there are Markdown versions next to the notebook files.

## TableGen Kernel

To use the kernel, first install it into jupyter:
To use the kernel, first install it into jupyter.

python3 -m tablegen_kernel.install

Then put this folder on your PYTHONPATH so jupyter can find it:
If you have installed Jupyter into a virtual environment, adjust `python3` to
be the interpreter for that environment. This will ensure that tools run the
kernel in the correct context.

```shell
export PYTHONPATH=$PYTHONPATH:<path to this dir>
```
python3 -m tablegen_kernel.install

Then run one of:

Expand Down
14 changes: 0 additions & 14 deletions llvm/utils/TableGen/jupyter/tablegen_kernel/assets/kernel.json

This file was deleted.

30 changes: 26 additions & 4 deletions llvm/utils/TableGen/jupyter/tablegen_kernel/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,39 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

import os
import json
import sys
import argparse
from tempfile import TemporaryDirectory
from jupyter_client.kernelspec import KernelSpecManager


def install_my_kernel_spec(user=True, prefix=None):
"""Install the kernel spec for user in given prefix."""
print("Installing llvm-tblgen IPython kernel spec")
pkgroot = os.path.dirname(__file__)
KernelSpecManager().install_kernel_spec(
os.path.join(pkgroot, "assets"), "tablegen", user=user, prefix=prefix
)

kernel_json = {
"argv": [
sys.executable, "-m", "tablegen_kernel", "-f", "{connection_file}"
],
"display_name": "LLVM TableGen",
"language": "tablegen",
"language_info": {
"name": "tablegen",
"codemirror_mode": "tablegen",
"mimetype": "text/x-tablegen",
"file_extension": ".td",
"pygments_lexer": "text"
}
}

with TemporaryDirectory() as tmpdir:
json_path = os.path.join(tmpdir, "kernel.json")
with open(json_path, 'w') as json_file:
json.dump(kernel_json, json_file)
KernelSpecManager().install_kernel_spec(
tmpdir, "tablegen", user=user, prefix=prefix
)


def _is_root():
Expand Down

0 comments on commit 535693f

Please sign in to comment.