feat: shortcode for Jupyter Notebook

This commit is contained in:
Xin 2024-03-29 22:03:14 +00:00
parent ef536af9e8
commit 24de8cb34e
6 changed files with 115 additions and 1 deletions

View File

@ -0,0 +1,31 @@
.hextra-jupyter-code-cell {
* {
scrollbar-gutter: auto;
}
@apply hx-mt-6;
.hextra-jupyter-code-cell-source {
@apply hx-text-sm;
.chroma,
pre {
@apply hx-m-0;
}
}
.hextra-jupyter-code-cell-outputs-container {
@apply hx-text-xs hx-overflow-hidden hx-border hx-border-t-0 hx-rounded-b-xl dark:hx-border-neutral-800;
.hextra-jupyter-code-cell-outputs {
@apply hx-overflow-auto hx-max-h-[50vh];
pre {
@apply hx-p-4;
}
}
}
}
.hextra-jupyter-code-cell:has(.hextra-jupyter-code-cell-outputs) .hextra-jupyter-code-cell-source .chroma {
@apply hx-rounded-b-none;
}

View File

@ -1,4 +1,4 @@
.hextra-scrollbar { .hextra-scrollbar, .hextra-scrollbar * {
scrollbar-width: thin; /* Firefox */ scrollbar-width: thin; /* Firefox */
scrollbar-color: oklch(55.55% 0 0 / 40%) transparent; /* Firefox */ scrollbar-color: oklch(55.55% 0 0 / 40%) transparent; /* Firefox */

View File

@ -9,6 +9,7 @@
@import "components/navbar.css"; @import "components/navbar.css";
@import "components/scrollbar.css"; @import "components/scrollbar.css";
@import "components/code-copy.css"; @import "components/code-copy.css";
@import "components/jupyter.css";
html { html {
@apply hx-text-base hx-antialiased; @apply hx-text-base hx-antialiased;

View File

@ -0,0 +1,10 @@
---
title: Jupyter Notebook
math: true
---
{{< callout type="info" >}}
Experimental page to include Jupyter Notebooks in the site.
{{< /callout >}}
{{< jupyter "Intro.ipynb" >}}

View File

@ -60,12 +60,18 @@
{{- end -}} {{- end -}}
<script> <script>
// TODO: make render options configurable // TODO: make render options configurable
// Reference: https://katex.org/docs/autorender#api
document.addEventListener("DOMContentLoaded", function () { document.addEventListener("DOMContentLoaded", function () {
renderMathInElement(document.body, { renderMathInElement(document.body, {
delimiters: [ delimiters: [
{ left: "$$", right: "$$", display: true }, { left: "$$", right: "$$", display: true },
{ left: "$", right: "$", display: false }, { left: "$", right: "$", display: false },
{ left: "\\(", right: "\\)", display: false }, { left: "\\(", right: "\\)", display: false },
{ left: "\\begin{equation}", right: "\\end{equation}", display: true },
{left: "\\begin{align}", right: "\\end{align}", display: true},
{left: "\\begin{alignat}", right: "\\end{alignat}", display: true},
{left: "\\begin{gather}", right: "\\end{gather}", display: true},
{left: "\\begin{CD}", right: "\\end{CD}", display: true},
{ left: "\\[", right: "\\]", display: true }, { left: "\\[", right: "\\]", display: true },
], ],
throwOnError: false, throwOnError: false,

View File

@ -0,0 +1,66 @@
{{/* Render Jupyter Notebook */}}
{{- $path := .Get 0 -}}
{{- $data := "" -}}
{{- $page := .Page -}}
{{/* https://gohugo.io/functions/transform/unmarshal/ */}}
{{- with .Page.Resources.Get $path -}}
{{- with unmarshal .Content -}}
{{- $data = . -}}
{{- end -}}
{{- else -}}
{{- errorf "Resource not found: %s" $path -}}
{{- end -}}
{{- $language := index $data "metadata" "language_info" "name" | default "python" -}}
{{- with index $data "cells" -}}
{{- range $cell := . -}}
{{- if eq (index $cell "cell_type") "code" -}}
{{- $source := index $cell "source" -}}
{{- $sourceContent := (cond (reflect.IsSlice $source) (delimit $source "") $source) -}}
<div class="hextra-jupyter-code-cell hextra-scrollbar">
<div class="hextra-jupyter-code-cell-source hextra-code-block">
{{- partial "components/codeblock" (dict "lang" $language "content" $sourceContent) -}}
</div>
{{- $outputs := index $cell "outputs" -}}
{{- with $outputs -}}
<div class="hextra-jupyter-code-cell-outputs-container">
<div class="hextra-jupyter-code-cell-outputs">
{{- range $output := . -}}
{{- if eq (index $output "output_type") "display_data" -}}
{{- $data := index $output "data" -}}
{{- $image := index $data "image/png" -}}
{{- if $image -}}
<img src="data:image/png;base64,{{- $image -}}" alt="image" />
{{- end -}}
{{- else if eq (index $output "output_type") "stream" -}}
{{- $text := index $output "text" -}}
{{- $textContent := (cond (reflect.IsSlice $text) (delimit $text "") $text) -}}
<pre class="not-prose">{{- $textContent -}}</pre>
{{- else if eq (index $output "output_type") "execute_result" -}}
{{- $data := index $output "data" -}}
{{- $text := index $data "text/plain" -}}
{{- $textContent := (cond (reflect.IsSlice $text) (delimit $text "") $text) -}}
<pre class="not-prose">{{- $textContent -}}</pre>
{{- $html := index $data "text/html" -}}
{{- if $html -}}
{{- $htmlText := delimit $html "" -}}
<div>
{{- $htmlText | safeHTML -}}
</div>
{{- end -}}
{{- end -}}
{{- end -}}
</div>
</div>
{{- end -}}
</div>
{{- else if eq (index $cell "cell_type") "markdown" -}}
{{- $source := index $cell "source" -}}
{{- $sourceContent := (cond (reflect.IsSlice $source) (delimit $source "") $source) -}}
{{- $sourceContent | $page.RenderString -}}
{{- end -}}
{{- end -}}
{{- end -}}