Skip to content
Snippets Groups Projects
Commit 4f3196f7 authored by sheepmax's avatar sheepmax
Browse files

Added access to server files from python using open

parent 6cc7d465
No related branches found
No related tags found
3 merge requests!37Publish current website,!18Amagherini assignment1,!7Resolve "Thebe Lite support (experimental)"
......@@ -22,6 +22,3 @@ bibtex_bibfiles:
launch_buttons:
thebe: true
repository:
url: https://gitlab.tudelft.nl/mude/book # Online location of your book
branch: main # Which branch of the repository should be used when creating links (optional)
......@@ -45,6 +45,9 @@ parts:
- file: sandbox/SanPart/premath/00_01c_PreMath.md
- file: sandbox/SanPart/premath/00_02_PreMath.md
- file: sandbox/SanPart/premath/00_03_PreMath.md
- file: sandbox/it
sections:
- file: sandbox/it-benchmark_dont_execute.ipynb
- caption: MUDE Cookbook
chapters:
- file: cookbook/blank
......@@ -176,4 +179,4 @@ parts:
- file: old/coding/2-2/1_1_dont_execute.ipynb
- file: old/coding/2-2/1_2_dont_execute.ipynb
- file: old/coding/2-2/1-part-2.md
- file: old/coding/2-2/1-part-3.md
\ No newline at end of file
- file: old/coding/2-2/1-part-3.md
%% Cell type:markdown id: tags:
# Benchmarks
%% Cell type:code id: tags:
``` python
with open("reading_test.txt") as f:
print(f.read())
```
%% Cell type:code id: tags:
``` python
```
# IT Benchmarks
Some benchmarks for any IT stuff going on in here.
```{tableofcontents}
```
\ No newline at end of file
Hopefully we can read this!
......@@ -130,19 +130,71 @@ const loadStyleAsync = async (styleSource) => {
});
};
function override_pyodide_open(fs, home, server_path) {
const old_open = fs.open;
function new_open(path, flags, mode) {
console.log(`Params: ${path}, ${flags}, ${mode}`);
// Being called from writeFile, or in write mode, we don't want to load in that case
// This catches any errors caused by things like missing directories
if (flags === 577) {
console.log("Running old open");
return old_open(path, flags, mode);
}
console.log(`Params: ${path}, ${flags}, ${mode}`);
try {
return old_open(path, flags, mode);
} catch {
const fullPath = path;
// Remove '/home/pyodide' or '/home/drive' etc.
if (path.startsWith(home)) {
path = path.slice(home.length);
}
// Last entry is the file itself
let directories = fullPath.split("/").slice(0, -1);
let currentDirectory = "/";
for (const directory of directories) {
if (directory === "") continue;
currentDirectory += directory + "/";
try {
fs.mkdir(currentDirectory);
console.log(`Made up to: ${currentDirectory}`);
} catch {}
}
let request = new XMLHttpRequest();
// Fetch relative to where we are in the server, to simulate how it works in notebooks
// We will still put it in the directory requested by pyodide however, so the next call to open succeeds without tampering
const requestFrom = path[0] === "/" ? path : server_path + path;
console.log(`Requesting from: ${requestFrom}`);
request.open("GET", requestFrom, false);
request.send();
if (request.status !== 200) {
throw new fs.ErrnoError(44);
}
fs.writeFile(fullPath, request.response);
return old_open(fullPath, flags, mode);
}
}
fs.open = new_open;
}
var initThebe = async () => {
// Load thebe dynamically if it's not already loaded
if (typeof thebelab === "undefined") {
console.log("[sphinx-thebe]: Loading thebe...");
$(".thebe-launch-button ").css("display", "none");
// Provides nice things like a running animation and some padding
{
await loadStyleAsync("/thebe.css");
await loadStyleAsync("/_static/code.css");
}
$(".thebe-launch-button ").css("display", "block");
$(".thebe-launch-button ").text("Loading thebe...");
......@@ -153,7 +205,12 @@ var initThebe = async () => {
console.log("[sphinx-thebe]: Finished loading thebe...");
configureThebe();
modifyDOMForThebe();
thebelab.bootstrap(thebeLiteConfig);
await thebelab.bootstrap(thebeLiteConfig);
await thebelab.session.kernel.requestExecute({
code: `import js; from os import getcwd; import pyodide_js; js.fs = pyodide_js.FS; js.eval("""${override_pyodide_open.toString()}"""); js.eval(f"override_pyodide_open(fs, '{getcwd()}/', ${
location.pathname.split("/").slice(0, -1).join("/") + "/"
})")`,
});
} else {
console.log("[sphinx-thebe]: thebe already loaded...");
}
......@@ -168,4 +225,3 @@ var detectLanguage = (language) => {
}
return language;
};
......@@ -17,4 +17,8 @@ else
fi
fi
# Copy all non notebook, markdown or build files into the build for local access in pyodide etc.
find book/ | grep -v "^book/_.*\|.*\.\(md\|ipynb\)\|thebe_lite" | grep "\." | cut -c 6- | xargs -i sh -c 'echo "book/_build/html/{}" | grep -o "^.*/" | xargs -d "\n" mkdir -p; cp book/"{}" book/_build/html/"{}"'
$python_command -m http.server 8000 --directory book/_build/html &
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment