如何在Jupyter笔记本中编程添加幻灯片元数据?

发布于 2025-01-28 00:39:45 字数 518 浏览 4 评论 0原文

jupyter笔记本电脑具有可视化单元格元数据的一种方式,因此当您使用nbconvert将笔记本电脑导出到幻灯片中时,可以参数化单元格的外观。

例子:

例如,我想在不使用GUI的情况下编程地将该元数据添加到单元中元数据工具。

我遇到此

参考/Jupyterbook.org/en/stable/content/metadata.html#add-tags-using-python-code

哪个可以编程地添加元数据以隐藏单元格代码。我希望能够做同样的事情,但是添加幻灯片元数据。例如,我不确定我应该遵循什么{tags:values}或语法。

Jupyter notebooks has a way of visualizing cell metadata so that you are able to parametrize how will cells look like when you export the notebook into slides using nbconvert.

Example:
enter image description here

For instance, I would like to programmatically add that metadata into cells without using the GUI, so that I can automate slide creation, specially from google colaboratory as it doesn't support the Edit Cell Metadata tool.

I've come across this reference

https://jupyterbook.org/en/stable/content/metadata.html#add-tags-using-python-code

which programmatically adds metadata to hide cell code. I would like to be able to do the same but adding slideshow metadata. For instance I am not sure what {tags:values} or syntax I should follow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺渁∝ 2025-02-04 00:39:45

您可以使用 nbformat 像您指向的示例一样执行此操作。

“ NBFormat软件包允许您通过编程阅读和解析笔记本文件。” - ,托尼·赫斯特(Tony Hirst)的描述

nbformat作为jupyter的一部分,因此在运行笔记本电脑的任何地方都可以运行。

我使用nbformat来执行与您的目标相似的事情在这里(在work-In-In-in-in-in-progress状态下)。最相关的部分是

import nbformat as nbf
a = nbf.v4.new_notebook()
for p in image_fn_pairs:
    content_for_md_cell = slideshow_cell_stub_text.replace(
        "THE_IMAGE1_PLACEHOLDER_TEXT_GOES_HERE",p[0]).replace(
        "THE_IMAGE2_PLACEHOLDER_TEXT_GOES_HERE",p[1])
    a.cells.append(nbf.v4.new_markdown_cell(content_for_md_cell))
# fix the metadata for each cell to be for a slide
slide_show_meta = {"slideshow": {"slide_type": "slide"}}
# a.cells = [c["metadata"] = slide_show_meta for c in a.cells]
meta_fixed_cells = []
for c in a.cells:
    c["metadata"] = slide_show_meta
    meta_fixed_cells.append(c)
a.cells = meta_fixed_cells

就您而言,听起来您已经在笔记本中拥有内容,因此,在您的情况下,您需要在笔记本上阅读,就像先这样的书籍:

import nbformat as nbf
ntbk = nbf.read("old_notebook.ipynb", nbf.NO_CONVERT)

然后像我的示例一样修复元数据。沿着这些线路:

new_ntbk = ntbk
# fix the metadata for each cell to be for a slide
slide_show_meta = {"slideshow": {"slide_type": "slide"}}
meta_fixed_cells = []
for c in ntbk.cells:
    c["metadata"] = slide_show_meta
    meta_fixed_cells.append(c)
new_ntbk.cells = meta_fixed_cells
nbf.write(new_ntbk, "notebook_with_slide_metadata.ipynb", version=nbf.NO_CONVERT)

您可以尝试我设置的自动化,启动幻灯片构建过程,并通过那里,然后单击启动活页夹。我认为,如果您只是在笔记本上运行内容,它将指导您通过每张幻灯片上的一些矩形并排制作幻灯片,作为图像的替身。

您还会看到我在,基于整个笔记本的元数据玩幻灯片自动打开笔记本时,即“ livereVeal”:{“ autoLaunch”:true,“ scroll”:true} e节。



nbconvert includes a preprocessor that will edit metadata, see here< /a>看起来会添加元数据。



如果您使用JupyterLab作为幻灯片开发工具,则可以安装Jupyterlab-Deck,该工具栏将添加一个“甲板”图标,使您可以在SlideCk Viewing中切换和向外切换。请参阅在此处使用。 (在通过Mybinder提供的会话甲板模式下,如果将鼠标移到屏幕顶部,我会看到工具栏。我认为这是因为shift+Esc不起作用。 '已更新。)

You can use nbformat to do this like the example you point to.

"The nbformat package allows you to programmatically read and parse notebook files." - SOURCE, Tony Hirst's description

nbformat comes as part as Jupyter so it runs wherever you have your notebooks running.

I use nbformat to do something similar to your goal here (in a work-in-progress state). The most pertinent part is here where I set the meta data for cells in the notebook to be a slide show. Here's the gist of that section:

import nbformat as nbf
a = nbf.v4.new_notebook()
for p in image_fn_pairs:
    content_for_md_cell = slideshow_cell_stub_text.replace(
        "THE_IMAGE1_PLACEHOLDER_TEXT_GOES_HERE",p[0]).replace(
        "THE_IMAGE2_PLACEHOLDER_TEXT_GOES_HERE",p[1])
    a.cells.append(nbf.v4.new_markdown_cell(content_for_md_cell))
# fix the metadata for each cell to be for a slide
slide_show_meta = {"slideshow": {"slide_type": "slide"}}
# a.cells = [c["metadata"] = slide_show_meta for c in a.cells]
meta_fixed_cells = []
for c in a.cells:
    c["metadata"] = slide_show_meta
    meta_fixed_cells.append(c)
a.cells = meta_fixed_cells

In your case, it sounds like you already have the content in your notebook, and so in your case you'll want to read in the notebook, like this first:

import nbformat as nbf
ntbk = nbf.read("old_notebook.ipynb", nbf.NO_CONVERT)

And then fix the metadata like my example. Something along these lines:

new_ntbk = ntbk
# fix the metadata for each cell to be for a slide
slide_show_meta = {"slideshow": {"slide_type": "slide"}}
meta_fixed_cells = []
for c in ntbk.cells:
    c["metadata"] = slide_show_meta
    meta_fixed_cells.append(c)
new_ntbk.cells = meta_fixed_cells
nbf.write(new_ntbk, "notebook_with_slide_metadata.ipynb", version=nbf.NO_CONVERT)

You can try the automated, work-in-progress slide building process I set up and referred to above by going there and clicking launch binder. I think if you just run things in the notebook that comes up it will guide you through making slides with some filled rectangles side-by-side on each slide as stand-in for images.

You'll also see I use a stub of a notebook in the script, that is based on a notebook stub to add in metadata for the entire notebook here so that it will play the slideshow automatically when the notebook is opened, i.e., the "livereveal": {"autolaunch": true, "scroll": true} section.



nbconvert includes a preprocessor that will edit metadata, see here where it looks like it will add the metadata.



If you use JupyterLab as your slide development tool, you can install jupyterlab-deck which will add a 'deck' icon to the toolbar that allows you to toggle in and out of the slidedeck viewing. See Usage here. (In deck mode of sessions served via MyBinder, I see the toolbar if I move the mouse towards the top of screen. I assume this is because shift+esc doesn't work. Or the documentation just hasn't been updated.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文