Kernel selection in Quarto
Jorge Martínez Garrido
January 30, 2025
Abstract
This post expalins how to install a custom Jupyter kernel so it can be used later in Quarto. The final result is rendere by Hugo, ensuring up-to-date content in your website.
While reading about Quarto, I found that one can specify the kernel to be used, see the kernel selection section. This is possible thanks to nbclient, which executes the Jupyter notebooks.
Therefore, I thought it would be interesting to explore other available kernels in the Jupyter ecosystem. In particular, I wanted to explore the usage of the Jupyter C kernel.
Installing the Jupyter C kernel
The installation of the Jupyter C kernel is straightforward. First, you need to install the kernel itself. This can be done by running the following command:
python -m pip install jupyter-c-kernel
Later, register the kernel in your virtual environment:
install_c_kernel --prefix .venv
Verify the installation by running the following command:
jupyter kernelspec list
Result:
c .venv/share/jupyter/kernels/c
python3 .venv/share/jupyter/kernels/python3
A simple Quarto notebook
The key when creating a Quarto notebook is to specify the kernel to be used.
This is done by adding the jupyter: <kernel>
metadata to the notebook:
---
# Hugo metadata
title: "Title of the post"
date: "YYYY-MM-DD"
author: "John Doe"
categories: [...]
tags: [...]
# Quarto metadata
jupyter: c
---
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam nec purus...
Example code
This code computes the current date on the system. It is written in C. You can check that the output of this code differs from the publication date of the post, meaning that the post is actually executing everytime my website builds.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
time_t t = time(NULL);
struct tm* epoch = localtime(&t);
char epoch_str[11];
strftime(epoch_str, 11, "%Y-%m-%d", epoch);
printf("Epoch: %s\n", epoch_str);
return EXIT_SUCCESS;
}
Epoch: 2025-02-05