跳转至

clientFilesserverFiles

每个课程中有多个位置可以存储文件以供客户端或服务器访问。这些可用于问题中使用的代码库、问题中嵌入的图像、考试期间可用的公式表或考试期间供参考的在线教科书。

ClientFiles 目录包含可从客户端 Web 浏览器访问的文件。这适用于学生应该有权访问的文件,例如静态图像、参考网页或公式表,或客户端上使用的代码库。

ServerFiles 目录只能从服务器上运行的代码访问,因此对于可以解决问题或生成随机问题实例的库很有用。学生的 Web 浏览器无法直接访问 serverFiles 目录中的文件。

请参阅如何在 Python 自动评分器中使用 serverFilesCourse 的示例

目录布局

clientFiles* 子目录可以与课程、问题、课程实例或评估关联,如下所示。 serverFilesCourse 子目录与整个课程相关联。

exampleCourse
+-- clientFilesCourse                     # client files for the entire course
|   +-- library.js
+-- serverFilesCourse                     # server files for the entire course
|   `-- secret1.js
+-- questions
|   `-- fossilFuels
|       +-- clientFilesQuestion           # client files for the fossilFuels question
|       |   `-- power-station.jpg
`-- courseInstances
    `-- Fa16
       +-- clientFilesCourseInstance      # client files for the Fall 2016 course instance
       |   `-- Fa16_rules.pdf
       `-- assessments
           `-- hw01
               `-- clientFilesAssessment  # client files for the Homework 1 assessment
                   `-- formulaSheet.pdf

访问控制

每个不同的 clientFiles* 目录都可以根据课程实例和评估的相同访问控制规则 进行访问。也就是说,有权访问某些课程实例的任何学生都可以访问 clientFilesCourse,而有权访问相应问题、课程实例或评估的学生可以访问 clientFilesQuestionclientFilesCourseInstanceclientFilesAssessment

允许学生访问客户文件

要允许学生访问文件,请将文件放置在适当的 clientFiles* 目录中。例如,要在问题中包含图像,请将图像放置在该问题的 clientFilesQuestion 目录中。如果图像在多个问题中使用,可以将其放置在课程的 clientFilesCourse 目录中。要在考试期间提供公式表,请将公式表放置在该考试的 clientFilesAssessment 目录中。

要允许学生查看问题中的文件内容,您可以使用以下方法之一引用该文件:

  1. pl-figure 元素可用于显示问题中来自 clientFilesQuestionclientFilesCourse 的图像。例如,如果图像名为 power-station.jpg,您可以将其保存在 clientFilesQuestion 中,然后在 question.html 文件中包含:

    <pl-figure file-name="power-station.jpg" alt="Power Station"></pl-figure>
    

    有关详细信息,请参阅pl-figure 文档

  2. pl-file-download 元素可用于为学生提供从 clientFilesQuestionclientFilesCourse 下载文件的链接。例如,要让学生访问起始代码文件,您可以将其保存在 clientFilesQuestion 中,然后在 question.html 文件中包含:

    <pl-file-download file-name="starting-code.py"></pl-file-download>
    

    此元素还可用于提供静态 PDF 文件或网页的链接。有关详细信息,请参阅pl-file-download 文档

  3. 要在问题中包含 clientFilesCourseclientFilesQuestion 的样式表和脚本,您可以将它们添加为问题配置文件中的依赖项。例如,要包含 clientFilesCourse 中名为 styles.css 的样式表,您可以将以下内容添加到 info.json 文件中:

    {
      "dependencies": { "clientFilesCourseStyles": ["styles.css"] }
    }
    

    有关如何将客户端文件包含为依赖项的更多详细信息,请参阅问题依赖项文档

  4. 某些元素对这些目录中的文件有特殊支持。例如,pl-file-editor 元素允许将自定义模式存储在 clientFilesCourseclientFilesQuestion 中,并使用 ace-mode 属性进行指定。有关详细信息,请参阅pl-file-editor 文档

  5. 要在更高级的上下文(例如 CSS 引用、音频/视频轨道或嵌入对象)中提供这些文件,您可以使用模式 {{ options.client_files_course_url }}/filename.ext{{ options.client_files_question_url }}/filename.ext 分别获取 clientFilesCourseclientFilesQuestion 中文件的 URL。例如,要包含 clientFilesQuestion 中的图像作为块的背景,您可以将以下内容添加到问题的 question.html 文件中:

    <div style="background-image: url('{{ options.client_files_question_url }}/power-station.jpg')">
      <!-- content here -->
    </div>
    

Warning

过去使用的常见模式是使用 clientFilesQuestion/filename.ext 等相对链接来访问 clientFilesQuestion 中的文件。此模式不受官方支持,并且可能无法在所有情况下工作,因此强烈建议您将其替换为 {{ options.client_files_question_url }}/filename.ext 模式。

clientFilesCourseInstanceclientFilesAssessment(以及 clientFilesCourse)中的文件可以使用评估文本提供给学生,该文本呈现在学生的评估概述页面中。这些可以使用 {{ client_files_course_instance }}/filename.ext{{ client_files_assessment }}/filename.ext{{ client_files_course }}/filename.ext 模式来呈现,这将呈现相应 clientFiles* 目录中文件的 URL。更多详细信息可以在评估文本文档中找到。

server.py 问题代码访问文件

server.py 问题代码中,您可以使用以下特殊值访问这些特殊目录中的文件:

目录 变量
问题目录本身 data["options"]["question_path"]
clientFilesQuestion data["options"]["client_files_question_path"]
clientFilesCourse data["options"]["client_files_course_path"]
serverFilesCourse data["options"]["server_files_course_path"]

如果您正在开发自定义元素,则 clientFilesCourseserverFilesCourse 的选项也可用。

因此,例如,如果您的问题生成代码需要存储在 serverFilesCourse 目录中的名为 data.csv 的数据文件,您可以使用以下代码访问它:

import os

def generate(data):
    data_file_path = os.path.join(data["options"]["server_files_course_path"], "data.csv")
    # Now you can read from data_file_path to access the contents of data.csv

???注意“使用相对路径”

Although `data["options"]["question_path"]` provides an absolute path to the question directory, the code is executed with the question directory as the current working directory, so you can also access files in the question directory using relative paths. Similarly, you can access files in `clientFilesQuestion` using relative paths, as the directory is guaranteed to be a subdirectory of the question directory. However, `clientFilesCourse` and `serverFilesCourse` are not subdirectories of the question directory and are not guaranteed to be found in any specific location relative to the question directory, so you must use the absolute paths provided in `data["options"]` to access files in those directories.

如果您的 serverFilesCourse 文件包含 Python 代码,您的 server.py 代码可以将该代码作为模块导入,而无需更新 Python 路径。执行 server.py 代码时,serverFilesCourse 目录会自动添加到 Python 路径中,因此您可以简单地导入该模块,就好像它在同一目录中一样。例如,如果 serverFilesCourse 中有一个名为 course_utils.py 的文件,则可以使用以下命令将其导入到 server.py 中:

import course_utils

如果您要在 server.py(或自定义元素)中的 render() 函数中渲染 HTML 内容,您还可以使用以下前缀来获取 clientFilesCourseclientFilesQuestion 中文件的 URL:

目录 前缀
clientFilesQuestion data["options"]["client_files_question_url"]
clientFilesCourse data["options"]["client_files_course_url"]
动态文件 data["options"]["client_files_question_dynamic_url"]

从外部评分者访问文件

使用外部评分的问题也可以访问serverFilesCourse中的内容。要访问这些文件,问题配置必须在 info.json 的外部评分配置中显式包含这些文件或目录。例如,要让外部评分者访问 serverFilesCourse 中名为 data.csv 的文件,您可以将以下内容添加到 info.json 文件中:

{
  "externalGradingOptions": {
    "serverFilesCourse": ["data.csv"]
  }
}

更多详细信息可以参见外部分级文档

从工作区访问文件

使用工作区的问题还可以配置为包含问题目录或 serverFilesCourse 中的文件。要访问这些文件,必须将它们添加到 server.py 中的 data["params"]["_workspace_files"] 字段。例如,要授予工作区对 serverFilesCourse 中名为 data.csv 的文件和 clientFilesQuestion 中名为 config.json 的文件的访问权限,您可以将以下代码添加到 server.py 中:

def generate(data):
    data["params"]["_workspace_files"] = [
        {"name": "config.json", "questionFile": "clientFilesQuestion/config.json"},
        {"name": "data.csv", "serverFilesCourseFile": "data.csv"},
    ]

有关更多详细信息,请参阅工作区文档