简体   繁体   English

如何在 jupyter notebook 中读取其他单元格

[英]How to read other cells in jupyter notebook

I am trying to write a magic function for jupyter notebook which will let me see the output of manim directly within the notebook.我正在尝试为 jupyter notebook 编写一个神奇的 function ,这将让我直接在笔记本中看到 manim 的manim

To achieve that I basically save the content of the cell into a temporary .py file and then call the manimce command within a subprocess with the temporary .py file and check the output to find the path of the video/gif created.为了实现这一点,我基本上将单元格的内容保存到临时.py文件中,然后使用临时.py文件在子进程中调用manimce命令,并检查 output 以找到创建的视频/gif 的路径。

My question is that, let's say I've got some other variables declared in other cells (or even import statements) and I use them in the cell where my magic function is invoked.我的问题是,假设我在其他单元格(甚至导入语句)中声明了一些其他变量,并且我在调用我的魔法 function 的单元格中使用它们。 How could I also save those variables (or basically the code) from other cells in the same temporary .py file so that I won't get an error?我怎样才能将其他单元格中的这些变量(或基本上是代码)保存在同一个临时.py文件中,以免出现错误? Or any other way that can be achieved?或者任何其他可以实现的方式? Hopefully the screenshot below better explains what I am trying to ask.希望下面的屏幕截图能更好地解释我要问的问题。

jupyter-notebook-screenshot

Here is the code I've used:这是我使用的代码:

# manimce_magic.py
from IPython.core.display import display, HTML
from IPython.core.magic import Magics, magics_class, line_magic, cell_magic, line_cell_magic
from IPython.core.magic import needs_local_scope
from pathlib import Path
from subprocess import Popen, PIPE
from tempfile import NamedTemporaryFile
import IPython.display
import os
import re

def find_path(output_string):
    output_string = output_string.decode('utf-8')
    output_string = re.sub(' ', '', ''.join(output_string.split('\n')))
    if m := re.search('(?<=Filereadyat).+(?=INFO)', output_string):
        return m.group(0)

@magics_class
class ManimceMagic(Magics):
    @needs_local_scope
    @cell_magic
    def manimce(self, line, cell, **kwargs):
        manimce_args = line.split()
        f = NamedTemporaryFile('r+', suffix='.py', delete=False)
        try:
            f.write(cell)
            f.close()

            args = ['manimce', f.name, *manimce_args]
            p = Popen(args, stdout=PIPE, stdin=PIPE, stderr=PIPE)
            output, err = p.communicate()
            path = find_path(output)
            display(IPython.display.Code(output.decode('utf-8'), language='python3'))
            display(p.returncode)
        finally:
            os.remove(f.name)

        if path:
            path = Path(path)
            relative_path = path.relative_to(Path.cwd())

            if '-i' in manimce_args:
                return IPython.display.Image(relative_path, width=854, height=480)
            else:
                return IPython.display.Video(relative_path, width=854, height=480, html_attributes='controls loop autoplay')

def load_ipython_extension(ipython):
    ipython.register_magics(ManimceMagic)

I can't answer your specific question, but perhaps this will be helpful nonetheless.我无法回答您的具体问题,但也许这仍然会有所帮助。 I believe the current version of manim comes with a manim magic command already.我相信当前版本的 manim 已经带有 manim 魔法命令。 I have tried it and it seems to work fine.我已经尝试过了,它似乎工作正常。 I make it work, I first run import jupyter_manim , and then in a new cell, I write my program, beginning with the magic command %%manim TestClass -p -ql , where TestClass is the name of the animation I am building.我让它工作,我首先运行import jupyter_manim ,然后在一个新单元格中,我编写我的程序,从魔术命令%%manim TestClass -p -ql ,其中TestClass是我正在构建的 animation 的名称。 The manim output appears right there in the Jupyter notebook. Manim output 出现在 Jupyter 笔记本中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM