11.11.3 Jupyter笔记本

Jupyter笔记本是一种流行的技术,用于以全面的方式将代码、文本和图形输出显示在一起。Octave可以通过以下函数将结果发布到Jupyter笔记本jupyter_notebook.

 
: notebook = jupyter_notebook (notebook_filename)
: notebook = jupyter_notebook (notebook_filename, options)

运行并填充来自GNU Octave的Jupyter笔记本文件notebook_filename

同时支持文本和图形Octave输出。

此类具有公共属性notebook,这是JSON格式的Jupyter Notebook的结构体。此属性是临时公开的,以启用高级笔记本操作。

注:Jupyter笔记本版本(nbformat)不支持低于4.0的版本。

可选的第二个参数options是一个包含字段的结构体:

  • tmpdir设置临时起作用目录。

%plot以下设置支持magic:

  • %plot -f <format>%plot --format <format>:指定图像存储格式。支持的格式有:
    • PNG(默认)
    • SVG(注意:如果SVG图像没有出现在笔记本中,则很可能与Jupyter notebook的安全机制有关,并且需要明确“信任”它们)。
    • JPG
  • %plot -r <number>%plot --resolution <number>:指定图像分辨率。
  • %plot -w <number>%plot --width <number>:指定图像宽度。
  • %plot -h <number>%plot --height <number>:指定图像高度。

示例:

## Run all cells and generate the filled notebook

## Instantiate an object from a notebook file
notebook = jupyter_notebook ("myNotebook.ipynb");
## Run the code and embed the results in the notebook property
notebook.run_all ();
## Generate a new notebook by overwriting the original notebook
notebook.generate_notebook ("myNotebook.ipynb");

## Run just the second cell and generate the filled notebook

## Instantiate an object from a notebook file
notebook = jupyter_notebook ("myNotebook.ipynb");
## Run the code and embed the results in the notebook property
notebook.run (2)
## Generate a new notebook in a new file
notebook.generate_notebook ("myNewNotebook.ipynb");

## Generate an Octave script from a notebook

## Instantiate an object from a notebook file
notebook = jupyter_notebook ("myNotebook.ipynb");
## Generate the Octave script
notebook.generate_octave_script ("jup_script.m");

详见: jsondecode, jsonencode.

广告

版权所有 © 2024-2025 Octave中文网

ICP备案/许可证号:黑ICP备2024030411号-2