如何使我的狮身人面像指令将资产添加到_static文件夹?

如何解决如何使我的狮身人面像指令将资产添加到_static文件夹?

我正在构建自定义的狮身人面像扩展名和指令,以在狮身人面像和ReadTheDocs上呈现交互式图表。图表的实际数据位于.json文件中。

.rst文档中,包括图表的样子:

.. chart:: charts/test.json
    :width: 400px
    :height: 250px

    This is the caption of the chart,yay...

我要采取的步骤是:

  1. 使用与图表大小相同的占位符(加载微调器)呈现文档

  2. 使用jQuery(在添加的javascript文件中)获取json文件(在这种情况下为charts/test.json)的URI(我将其作为属性添加到指令中dom节点的属性)>

  3. 使用jQuery提取文件并解析JSON

  4. 成功获取数据后,使用plotly库将其呈现为图表,并使用jQuery删除占位符


指令如下:


class PlotlyChartDirective(Directive):
    """ Top-level plotly chart directive """

    has_content = True

    def px_value(argument):
        # This is not callable as self.align.  We cannot make it a
        # staticmethod because we're saving an unbound method in
        # option_spec below.
        return directives.length_or_percentage_or_unitless(argument,'px')

    required_arguments = 1
    optional_arguments = 0

    option_spec = {
        # TODO allow static images for PDF renders   'altimage': directives.unchanged,'height': px_value,'width': px_value,}

    def run(self):
        """ Parse a plotly chart directive """

        self.assert_has_content()
        env = self.state.document.settings.env
        # Ensure the current chart ID is initialised in the environment
        if 'next_plotly_chart_id' not in env.temp_data:
            env.temp_data['next_plotly_chart_id'] = 0

        id = env.temp_data['next_plotly_chart_id']

        # Handle the URI of the *.json asset
        uri = directives.uri(self.arguments[0])

        # Create the main node container and store the URI of the file which will be collected later
        node = nodes.container()
        node['classes'] = ['sphinx-plotly']

        # Increment the ID counter ready for the next chart
        env.temp_data['next_plotly_chart_id'] += 1

        # Only if its a supported builder do we proceed (otherwise return an empty node)
        if env.app.builder.name in get_compatible_builders(env.app):

            chart_node = nodes.container()
            chart_node['classes'] = ['sphinx-plotly-chart',f"sphinx-plotly-chart-id-{id}",f"sphinx-plotly-chart-uri-{uri}"]

            placeholder_node = nodes.container()
            placeholder_node['classes'] = ['sphinx-plotly-placeholder',f"sphinx-plotly-placeholder-{id}"]
            placeholder_node += nodes.caption('','Loading...')

            node += chart_node
            node += placeholder_node

            # Add optional chart caption and legend (inspired by Figure directive)
            if self.content:
                caption_node = nodes.Element()  # Anonymous container for parsing
                self.state.nested_parse(self.content,self.content_offset,caption_node)
                first_node = caption_node[0]
                if isinstance(first_node,nodes.paragraph):
                    caption = nodes.caption(first_node.rawsource,'',*first_node.children)
                    caption.source = first_node.source
                    caption.line = first_node.line
                    node += caption
                elif not (isinstance(first_node,nodes.comment) and len(first_node) == 0):
                    error = self.state_machine.reporter.error(
                        'Chart caption must be a paragraph or empty comment.',nodes.literal_block(self.block_text,self.block_text),line=self.lineno)
                    return [node,error]
                if len(caption_node) > 1:
                    node += nodes.legend('',*caption_node[1:])

        return [node]

无论我在FigureImage指令的源代码中寻找什么(我以此为依据),我都无法弄清楚如何从输入中复制原始图像位置,进入构建目录中的静态文件夹。

没有将参数中指定的*.json文件复制到我的指令中,我总是得到一个找不到的文件!

我一直在努力寻找支持方法(我假设Sphinx app实例将具有add_static_file()方法,就像它具有add_css_file()方法一样)。

我还尝试添加文件列表以复制到app实例,然后在构建结束时复制资产(但由于无法将属性添加到{{1 }}类)

胡说八道

在自定义指令中,如何将资产(其路径由该指令的参数指定)复制到构建的Sphinx目录中?

解决方法

我意识到在指令的_static方法中直接使用sphinx的copyfile实用程序(仅在文件被更改的情况下才进行复制,所以很快)会很简单。

查看如何获取run()src_uri并直接在此更新的build_uri中复制文件:

Directive

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-