Bokeh Sphinx 扩展——如何包含来自 CSV 文件的数据?

Bokeh Sphinx extension -- how to include data from CSV file?

我正在使用 Sphinx 创建一些文档,我想使用 bokeh.sphinxext 在文档中包含散景图。鉴于 this example,这似乎是一件容易的事。但是,我想用来生成绘图的数据存储在 CSV 文件中。我试过将我的 CSV 文件放入同一目录,并在同一 RST 文档中使用 .. include:: data.csv,但这没有用。

使用 Sphinx 创建散景图时如何引用外部文件?

.. include:: data.csv

.. bokeh-plot::

    import pandas as pd
    from bokeh.plotting import figure, output_file, show

    output_file("example.html")

    df = pd.read_csv('data.csv')

    p = figure(title="example", plot_width=300, plot_height=300)
    p.line(df['x'], df['y'], line_width=2)

    show(p)

由于扩展的运行方式,代码执行时的当前工作目录(即 os.getcwd() 函数返回的内容)是 Sphinx 项目的顶层。您将需要基于此构建数据文件的路径。例如。如果您的文件位于 Sphinx 项目的 source/docs 目录下,则可能是:

df = pd.read_csv(os.join('source', 'docs', 'data.csv')