@ky28059/react-jupyter-renderer

A fully-interactive Jupyter notebook renderer in React based on Pyodide and the JupyterLite project.

The Lorenz Differential Equations

Before we start, we import some preliminary libraries.

[2]:

We will also define the actual solver and plotting routine.

[3]:

We explore the Lorenz system of differential equations:

x˙=σ(yx)y˙=ρxyxzz˙=βz+xy\begin{aligned} \dot{x} & = \sigma(y-x) \\ \dot{y} & = \rho x - y - xz \\ \dot{z} & = -\beta z + xy \end{aligned}

Let's change (σ\sigma, β\beta, ρ\rho) and examine the trajectories.

[4]:

<Figure size 640x480 with 1 Axes>

For the default set of parameters, we see the trajectories swirling around two points, called attractors.

After interacting with the system, we can take the result and perform further computations. In this case, we compute the average positions in \(x\), \(y\) and \(z\).

[5]:

[6]:

(30, 3)

Creating histograms of the average positions (across different trajectories) show that, on average, the trajectories swirl about the attractors.

[7]:

[8]:

<Figure size 640x480 with 1 Axes>

[9]:

<Figure size 640x480 with 1 Axes>

[ ]:

See below for documentation on using the React library, or view a hosted notebook previewer here instead.

Installing

This library uses react-markdown to render Markdown notebook cells. Due to bundling issues, however, react-markdown is listed as a peer dependency and must be installed separately.

1npm i @ky28059/react-jupyter-renderer react-markdown

Usage

For most use cases, render notebooks using the <JupyterNotebook /> component:

1import { JupyterNotebook } from '@ky28059/react-jupyter-renderer';
2
3export default function App() {
4    // const notebook = ...
5
6    return (
7        <JupyterNotebook
8            notebook={notebook}
9            remarkPlugins={[remarkMath, remarkGfm]}
10            rehypePlugins={[rehypeKatex, rehypeRaw]}
11            wrapperClassName="py-6 h-[36rem] overflow-y-auto gap-4 text-sm"
12            markdownClassName="markdown mb-4"
13            codeCellClassName="group my-2"
14            executionCountClassName="font-mono group-data-active:text-sky-600 group-data-edited:text-orange-400!"
15            runButtonClassName="cursor-pointer text-right px-1"
16            codeEditorClassName="border border-black/10 bg-[rgb(245_245_245)] [&>textarea]:focus:outline-sky-600"
17            streamOutputClassName="mt-2 px-2 py-1 overflow-x-auto"
18            errorOutputClassName="mt-2 px-2 py-1 overflow-x-auto bg-red-500/20"
19            indicatorClassName="hidden group-data-active:block absolute left-1 w-2 rounded-xs h-full bg-sky-600 group-data-edited:bg-orange-400"
20        />
21    )
22}

For efficiency and customizability reasons, this library does not ship with any default component styles; instead, the appearance of notebooks can be controlled using the following props:

PropTypeValue
notebookNotebookThe Jupyter notebook to render. Note that to support the use case of embedding known / trusted notebooks, the input notebook data is not sanitized or checked before rendering.
trustedboolean?Whether to render potentially dangerous (HTML) output. Again, to support embedding known / trusted notebooks, this prop defaults to true.
remarkPluginsPluggable[]?Plugins for remark to be passed to react-markdown.
rehypePluginsPluggable[]?Plugins for rehype to be passed to react-markdown.
wrapperClassNamestring?The className to set on the wrapper <div> element.
markdownClassNamestring?The className to set on markdown cells.
codeCellClassNamestring?The className to set on code cells. If this cell has been edited since it was last executed, data-edited="true" will be set; similarly, if this cell is currently selected, data-active="true" will be set.
executionCountClassNamestring?The className to set on the execution count element; this is a child of the code cell element.
runButtonClassNamestring?The className to set on the run button element; this is a child of the code cell element.
codeEditorClassNamestring?The className to pass to the react-simple-code-editor code editor; this is a child of the code cell element.
streamOutputClassNamestring?The className to set on "stream output" (e.g. text/plain output, or prints to stdout).
errorOutputClassNamestring?The className to set on "error output" (e.g. exception stack traces, or prints to stderr).
indicatorClassNamestring?The className to set on the code cell "indicator", i.e. the blue or orange bar on JupyterLite displayed on the currently active code cell. Since the indicator is simply a <div> whose appearance is controlled via CSS, its behavior is fully customizable. If this prop is not provided, the indicator will not be rendered.

Alternatives

If you do not need to embed editable / interactive notebook cells (i.e. you only want to render a notebook statically), the following tools will likely be more lightweight:

If you do not need to embed notebooks into a custom environment (and just want to run them yourself), the following tools may be more featureful: