如何在 Jupyter Notebook 中使用 Mermaid?

如何在 Jupyter Notebook 中使用 Mermaid?

我想在 Jupyter Notebook 中使用 Mermaid 绘制流程图(原生的 Jupyter,而 不是 Visual Stdio Code 之类的移植版本。),但是无法实现这个功能。首先是我找到了这个博客:如何在jupyterlab里写mermaid语法支持的graph_vivisol的博客-CSDN博客

我按照网上现成的信息安装 jupyterlab-mermaid ,但是反馈找不到这个模块。pip install jupyterlab-mermaid 报错如下:

Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
ERROR: Could not find a version that satisfies the requirement jupyterlab-mermaid (from versions: none)
ERROR: No matching distribution found for jupyterlab-mermaid
WARNING: You are using pip version 21.2.4; however, version 23.2.1 is available.
You should consider upgrading via the 'D:\Python\Python39\python.exe -m pip install --upgrade pip' command.

我在 GitHub 上找到了这个项目:nb-mermaid : DEPRECATED Mermaid diagrams in the Jupyter Notebook,从项目的 About 上来看可能是已经废弃了的。

我按照项目的 README 执行了 pip install nb-mermaid,报错信息如下:

Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting nb-mermaid
  Downloading https://mirrors.aliyun.com/pypi/packages/b9/40/c721865ce3ce642a335b22cdc565586017636337eeab4a40e59ad27e2fd1/nb-mermaid-0.1.0.tar.gz (161 kB)
     |████████████████████████████████| 161 kB 1.3 MB/s
    ERROR: Command errored out with exit status 1:
     command: 'D:\Python\Python39\python.exe' -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\asus\\AppData\\Local\\Temp\\pip-install-f5bpcwhe\\nb-mermaid_56b617d41ad946938db0aa835dcc72a4\\setup.py'"'"'; __file__='"'"'C:\\Users\\asus\\AppData\\Local\\Temp\\pip-install-f5bpcwhe\\nb-mermaid_56b617d41ad946938db0aa835dcc72a4\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users\asus\AppData\Local\Temp\pip-pip-egg-info-z_phkng7'
         cwd: C:\Users\asus\AppData\Local\Temp\pip-install-f5bpcwhe\nb-mermaid_56b617d41ad946938db0aa835dcc72a4\
    Complete output (33 lines):
    Traceback (most recent call last):
      File "D:\Python\Python39\Lib\site-packages\jupyterpip\__init__.py", line 55, in cmdclass
        from notebook.nbextensions import install_nbextension
    ModuleNotFoundError: No module named 'notebook.nbextensions'

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "D:\Python\Python39\Lib\site-packages\IPython\utils\timing.py", line 27, in <module>
        import resource
    ModuleNotFoundError: No module named 'resource'

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\asus\AppData\Local\Temp\pip-install-f5bpcwhe\nb-mermaid_56b617d41ad946938db0aa835dcc72a4\setup.py", line 33, in <module>
        cmdclass=cmdclass(
      File "D:\Python\Python39\Lib\site-packages\jupyterpip\__init__.py", line 60, in cmdclass
        from IPython.html.nbextensions import install_nbextension
      File "D:\Python\Python39\Lib\site-packages\IPython\__init__.py", line 49, in <module>
        from .terminal.embed import embed
      File "D:\Python\Python39\Lib\site-packages\IPython\terminal\embed.py", line 19, in <module>
        from IPython.terminal.ipapp import load_default_config
      File "D:\Python\Python39\Lib\site-packages\IPython\terminal\ipapp.py", line 45, in <module>
        from IPython.core.magics import ScriptMagics
      File "D:\Python\Python39\Lib\site-packages\IPython\core\magics\__init__.py", line 22, in <module>
        from .execution import ExecutionMagics
      File "D:\Python\Python39\Lib\site-packages\IPython\core\magics\execution.py", line 45, in <module>
        from IPython.utils.timing import clock, clock2
      File "D:\Python\Python39\Lib\site-packages\IPython\utils\timing.py", line 64, in <module>
        clocku = clocks = clock = time.clock
    AttributeError: module 'time' has no attribute 'clock'
    ----------------------------------------
WARNING: Discarding https://mirrors.aliyun.com/pypi/packages/b9/40/c721865ce3ce642a335b22cdc565586017636337eeab4a40e59ad27e2fd1/nb-mermaid-0.1.0.tar.gz#sha256=69da43b8e94f4ef394395baf68ca2abfd4563955bbb36171b7da48ece05869d1 (from https://mirrors.aliyun.com/pypi/simple/nb-mermaid/). Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
ERROR: Could not find a version that satisfies the requirement nb-mermaid (from versions: 0.1.0)
ERROR: No matching distribution found for nb-mermaid
WARNING: You are using pip version 21.2.4; however, version 23.2.1 is available.
You should consider upgrading via the 'D:\Python\Python39\python.exe -m pip install --upgrade pip' command.

我试着给 Jupyter 安装扩展,但是没有找到 Mermaid 扩展选项。

img

我需要帮助!有人知道如何在 Jupyter 上使用 Mermaid 吗?

引用 皆我百晓生 小程序回复内容作答:
很抱歉,我了解到你在尝试安装nb-mermaid和jupyterlab-mermaid时遇到了问题。这些包可能已经过时或不再维护,所以你遇到了安装错误。

在Jupyter Notebook中使用Mermaid流程图的替代方法是使用Python的Mermaid库和Graphviz库。你可以按照以下步骤进行操作:

  1. 确保你已经安装了Graphviz库。你可以使用以下命令通过pip进行安装:

    pip install graphviz
    
  2. 安装pygraphviz库。你可以使用以下命令进行安装:

    pip install pygraphviz
    
  3. 在Jupyter Notebook中创建一个新的Notebook,并导入所需的库:

    import matplotlib.pyplot as plt
    import networkx as nx
    import pygraphviz
    
  4. 使用Mermaid语法创建一个流程图。你可以尝试以下示例:

    mermaid_code = """
    graph LR
        A --> B
        B --> C
        C --> D
        D --> E
    """
    
    # 使用pygraphviz和networkx库绘制流程图
    G = nx.nx_agraph.from_agraph(pygraphviz.AGraph(mermaid_code))
    nx.draw(G, with_labels=True)
    plt.show()
    

    运行上述代码后,将绘制一个简单的流程图。

请注意,这种方法使用了图形库,并不直接在Jupyter Notebook中使用Mermaid语法。然而,在Python中使用Graphviz库进行流程图绘制是一种常见做法,并且可以得到与Mermaid相似的结果。

希望这可以帮到你!如果你有任何其他问题,请随时提问。