Skip to content

Commit 74fa3f5

Browse files
committed
fix loading question resources in pyodide
1 parent 575746e commit 74fa3f5

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

dist/programming.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,8 @@ Numbas.addExtension('programming', ['display', 'util', 'jme'], function(programm
467467
});
468468
}
469469

470+
const root_url = new URL('.', window.location);
471+
options.root_url = root_url.toString();
470472
this.pyodidePromise.then(worker => {
471473
worker.postMessage({
472474
command: 'preload',

dist/standalone_scripts/pyodide_worker.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ async function init(options) {
3333
async function preload(options) {
3434
await init();
3535

36-
const {packages, files, context_id} = options;
36+
const {packages, files, context_id, root_url} = options;
3737

3838
if(packages) {
3939
self.pyodide.loadPackage(packages);
4040
}
4141

4242
if(files) {
43-
load_files(files, context_id);
43+
load_files(root_url, files, context_id);
4444
}
4545
}
4646

@@ -71,24 +71,27 @@ function use_context(context_id) {
7171
init_filesystem(default_context.files.concat(context.files));
7272
}
7373

74-
/**
75-
* Load a file from the question resources, and return a blob which can be used in the worker's resources filesystem.
76-
*
77-
* @param {string} name
78-
* @returns {Object} ``name`` and ``data: Blob``.
79-
*/
80-
async function load_file(name) {
81-
const res = await fetch('../../../resources/question-resources/'+name);
82-
const blob = await res.blob();
83-
return {name, data: blob};
84-
}
85-
8674
/**
8775
* Load a list of files from the question resources, and store them in the context with the given ID.
8876
*/
89-
async function load_files(filenames, context_id) {
77+
async function load_files(root_url, filenames, context_id) {
9078
const context = get_context(context_id);
9179

80+
root_url += 'resources/question-resources/';
81+
82+
/**
83+
* Load a file from the question resources, and return a blob which can be used in the worker's resources filesystem.
84+
*
85+
* @param {string} name
86+
* @returns {Object} ``name`` and ``data: Blob``.
87+
*/
88+
async function load_file(name) {
89+
const url = root_url + name;
90+
const res = await fetch(url);
91+
const data = await res.blob();
92+
return {name, data};
93+
}
94+
9295
await Promise.all(filenames.map(async name => {
9396
const file = await load_file(name);
9497
context.files.push(file);

0 commit comments

Comments
 (0)