observable-notebook-kit
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseObservable Notebook Kit
Observable Notebook Kit
Observable Notebook Kit is an open-source CLI and Vite plugin for building static sites from Observable Notebooks.
Observable Notebook Kit 是一款开源的CLI和Vite插件,用于从Observable Notebooks构建静态站点。
File Format
文件格式
Notebooks are files with this structure:
.htmlhtml
<!doctype html>
<notebook theme="air">
<title>My Notebook</title>
<script type="text/markdown">
# Hello
</script>
<script type="module" pinned>
1 + 2
</script>
</notebook>笔记本是具有以下结构的文件:
.htmlhtml
<!doctype html>
<notebook theme="air">
<title>My Notebook</title>
<script type="text/markdown">
# Hello
</script>
<script type="module" pinned>
1 + 2
</script>
</notebook><notebook>
Attributes
<notebook><notebook>
标签属性
<notebook>- - colorway:
theme(default),air,coffee,cotton,deep-space,glacier,ink,midnight,near-midnight,ocean-floor,parchment,slate,starksun-faded - - disallow editing
readonly
- - 配色方案:
theme(默认)、air、coffee、cotton、deep-space、glacier、ink、midnight、near-midnight、ocean-floor、parchment、slate、starksun-faded - - 禁止编辑
readonly
<script>
(Cell) Attributes
<script><script>
(单元格)属性
<script>- - cell language (required)
type - - show source code
pinned - - suppress display
hidden - - variable name to expose (for non-JS cells)
output - - database name (SQL cells)
database - - output format (data loader cells)
format - - cell identifier
id
- - 单元格语言(必填)
type - - 显示源代码
pinned - - 隐藏输出
hidden - - 要暴露的变量名(非JS单元格适用)
output - - 数据库名称(SQL单元格适用)
database - - 输出格式(数据加载器单元格适用)
format - - 单元格标识符
id
Cell Types (type
values)
type单元格类型(type
取值)
type| Type | Language |
|---|---|
| JavaScript |
| TypeScript |
| Markdown |
| HTML |
| SQL |
| TeX/LaTeX |
| DOT (Graphviz) |
| Node.js data loader |
| Python data loader |
| R data loader |
Use 4 spaces indentation inside (auto-trimmed). Escape as .
<script></script><\/script>| 类型 | 语言 |
|---|---|
| JavaScript |
| TypeScript |
| Markdown |
| HTML |
| SQL |
| TeX/LaTeX |
| DOT (Graphviz) |
| Node.js 数据加载器 |
| Python 数据加载器 |
| R 数据加载器 |
在标签内使用4个空格缩进(会自动修剪)。将转义为。
<script></script><\/script>CLI Commands
CLI命令
Install:
npm add @observablehq/notebook-kit安装:
npm add @observablehq/notebook-kitPreview
预览
sh
notebooks preview --root docs
notebooks preview --root docs --template docs/custom.tmplsh
notebooks preview --root docs
notebooks preview --root docs --template docs/custom.tmplBuild
构建
sh
notebooks build --root docs -- docs/*.htmlOutput goes to .
.observable/distsh
notebooks build --root docs -- docs/*.html输出文件将生成到目录。
.observable/distDownload
下载
Convert Observable notebook URL to local HTML:
sh
notebooks download https://observablehq.com/@d3/bar-chart > bar-chart.html将Observable笔记本URL转换为本地HTML文件:
sh
notebooks download https://observablehq.com/@d3/bar-chart > bar-chart.htmlQuery
查询
Run database query manually:
sh
notebooks query --database duckdb 'SELECT 1 + 2'手动运行数据库查询:
sh
notebooks query --database duckdb 'SELECT 1 + 2'JavaScript in Notebooks
笔记本中的JavaScript
Expression vs Program Cells
表达式单元格与程序单元格
Expression cell (implicit display):
js
1 + 2Program cell (explicit display):
js
const foo = 1 + 2;
display(foo);表达式单元格(自动显示结果):
js
1 + 2程序单元格(手动控制显示):
js
const foo = 1 + 2;
display(foo);Key Built-ins
核心内置函数
- - render value to cell output
display(value) - - display input, return value generator
view(input) - - load local files (same/sub directory only)
FileAttachment("path") - - promise for cleanup on re-run
invalidation - - promise when cell visible
visibility - - current page width
width - - current time
now
- - 将值渲染到单元格输出
display(value) - - 显示输入组件,返回值生成器
view(input) - - 加载本地文件(仅支持当前目录或子目录)
FileAttachment("path") - - 重新运行时用于清理的Promise
invalidation - - 单元格变为可见时的Promise
visibility - - 当前页面宽度
width - - 当前时间
now
Standard Library
标准库
- - Observable Inputs
Inputs - - Observable Plot
Plot - - D3.js
d3 - ,
htl,html- Hypertext Literalsvg - ,
tex- tagged template literalsmd
- - Observable 输入组件库
Inputs - - Observable 可视化库
Plot - - D3.js
d3 - ,
htl,html- 超文本字面量工具svg - ,
tex- 标签模板字面量md
Imports
导入
js
import lib from "npm:package-name";
import {fn} from "jsr:@scope/package";js
import lib from "npm:package-name";
import {fn} from "jsr:@scope/package";Reactivity
响应式特性
Top-level variables trigger re-runs. Promises auto-await; generators yield latest value.
js
const x = view(Inputs.range([0, 100]));js
x ** 2 // re-runs when x changes顶级变量变更会触发单元格重新运行。Promise会自动等待;生成器会返回最新值。
js
const x = view(Inputs.range([0, 100]));js
x ** 2 // 当x变化时自动重新运行Interpolation
插值
Use in Markdown/HTML cells:
${...}md
The answer is ${x * 2}.在Markdown/HTML单元格中使用进行插值:
${...}md
答案是${x * 2}。Database Connectors
数据库连接器
See references/databases.md for full configuration.
完整配置请查看references/databases.md。
Quick Setup
快速设置
SQL cells use attribute. Default is (in-memory).
databaseduckdbhtml
<script type="application/sql" database="duckdb" output="results">
SELECT * FROM 'data.parquet'
</script>SQL单元格使用属性指定数据库。默认使用(内存数据库)。
databaseduckdbhtml
<script type="application/sql" database="duckdb" output="results">
SELECT * FROM 'data.parquet'
</script>Supported Databases
支持的数据库
- DuckDB ()
@duckdb/node-api - SQLite (Node.js 24+ built-in)
- Postgres ()
postgres - Snowflake ()
snowflake-sdk - BigQuery ()
@google-cloud/bigquery - Databricks ()
@databricks/sql
Install drivers separately:
npm add @duckdb/node-api- DuckDB(需安装)
@duckdb/node-api - SQLite(Node.js 24+ 内置支持)
- Postgres(需安装)
postgres - Snowflake(需安装)
snowflake-sdk - BigQuery(需安装)
@google-cloud/bigquery - Databricks(需安装)
@databricks/sql
需单独安装驱动:
npm add @duckdb/node-apiQuery Results
查询结果
- Auto-cached in
.observable/cache - Re-run via Play button or Shift+Enter in Desktop
- Delete cache file to refresh in Notebook Kit
- 自动缓存到目录
.observable/cache - 在桌面端可通过播放按钮或Shift+Enter重新运行查询
- 在Notebook Kit中可删除缓存文件以刷新结果
Dynamic Queries
动态查询
js
const db = DatabaseClient("duckdb");
const data = await db.sql`SELECT * FROM t WHERE x = ${value}`;js
const db = DatabaseClient("duckdb");
const data = await db.sql`SELECT * FROM t WHERE x = ${value}`;Data Loaders
数据加载器
Cells that run at build time via interpreter. Output cached in .
.observable/cache在构建时通过解释器运行的单元格。输出结果缓存到目录。
.observable/cacheFormats
输出格式
Text: , , , ,
Binary: , , ,
Image: , , , ,
Other:
textjsoncsvtsvxmlarrowparquetblobbufferpngjpeggifwebpsvghtml文本格式:、、、、
二进制格式:、、、
图片格式:、、、、
其他格式:
textjsoncsvtsvxmlarrowparquetblobbufferpngjpeggifwebpsvghtmlNode.js
Node.js
html
<script type="application/vnd.node.javascript" format="json" output="data">
process.stdout.write(JSON.stringify({hello: "world"}));
</script>Requires Node.js 22.12+. Sandboxed with read-only access to notebook directory.
html
<script type="application/vnd.node.javascript" format="json" output="data">
process.stdout.write(JSON.stringify({hello: "world"}));
</script>要求Node.js 22.12+版本。处于沙箱环境,仅对笔记本目录有只读权限。
Python
Python
html
<script type="text/x-python" format="text" output="result">
print("hello", end="")
</script>Requires Python 3.12+. Uses if present.
.venvhtml
<script type="text/x-python" format="text" output="result">
print("hello", end="")
</script>要求Python 3.12+版本。若存在则使用该虚拟环境。
.venvR
R
html
<script type="text/x-r" format="json" output="data">
cat(jsonlite::toJSON(list(x = 1:3)))
</script>html
<script type="text/x-r" format="json" output="data">
cat(jsonlite::toJSON(list(x = 1:3)))
</script>Page Templates
页面模板
Custom templates wrap built notebooks:
html
<!doctype html>
<html>
<head>
<style>@import url("observable:styles/index.css");</style>
</head>
<body>
<main></main> <!-- notebook renders here -->
</body>
</html>Use with CLI.
--template path.tmpl自定义模板可包裹构建后的笔记本:
html
<!doctype html>
<html>
<head>
<style>@import url("observable:styles/index.css");</style>
</head>
<body>
<main></main> <!-- 笔记本将渲染到此处 -->
</body>
</html>在CLI中使用参数指定模板。
--template path.tmplProject Setup
项目设置
Typical :
package.jsonjson
{
"dependencies": {
"@observablehq/notebook-kit": "^"
},
"scripts": {
"docs:preview": "notebooks preview --root docs",
"docs:build": "notebooks build --root docs -- docs/*.html"
}
}典型的配置:
package.jsonjson
{
"dependencies": {
"@observablehq/notebook-kit": "^"
},
"scripts": {
"docs:preview": "notebooks preview --root docs",
"docs:build": "notebooks build --root docs -- docs/*.html"
}
}Configuration Files
配置文件
- - database configs (keep out of git)
.observable/databases.json - - query/data loader results
.observable/cache/ - - built site
.observable/dist/
Recommended :
.gitignore.observable- - 数据库配置文件(请勿提交到git)
.observable/databases.json - - 查询/数据加载器结果缓存目录
.observable/cache/ - - 构建后的站点目录
.observable/dist/
推荐的配置:
.gitignore.observable