diff --git a/book/_config.yml b/book/_config.yml index 0d07020fc0d3a2b9de33189d55e11bc085e2eaec..aa068d9cab77df3e95991e5112f997cfc7aebdbc 100644 --- a/book/_config.yml +++ b/book/_config.yml @@ -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) diff --git a/book/_toc.yml b/book/_toc.yml index 66ab5224241be9d7b1466eb6ec02eeff8f7b2b20..225ffcb6561a38b79f4c43dfed30028b12721df5 100644 --- a/book/_toc.yml +++ b/book/_toc.yml @@ -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 diff --git a/book/sandbox/it-benchmark_dont_execute.ipynb b/book/sandbox/it-benchmark_dont_execute.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..4cb5a3dcbd222fb5882764b7bf53fa70c1b3588d --- /dev/null +++ b/book/sandbox/it-benchmark_dont_execute.ipynb @@ -0,0 +1,54 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Benchmarks" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "with open(\"reading_test.txt\") as f:\n", + " print(f.read())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.9" + }, + "vscode": { + "interpreter": { + "hash": "a67e065d7bac6c9a1464b57d1c6c9ee143117011e86b73963744a6c83e2c9c0b" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/book/sandbox/it.md b/book/sandbox/it.md new file mode 100644 index 0000000000000000000000000000000000000000..fee61fb3da70d23180f30263f8383207d7ff7e66 --- /dev/null +++ b/book/sandbox/it.md @@ -0,0 +1,6 @@ +# IT Benchmarks + +Some benchmarks for any IT stuff going on in here. + +```{tableofcontents} +``` \ No newline at end of file diff --git a/book/sandbox/reading_test.txt b/book/sandbox/reading_test.txt new file mode 100644 index 0000000000000000000000000000000000000000..f74068ca4a7f23ad27b1d057f76b4d4f5fa5757a --- /dev/null +++ b/book/sandbox/reading_test.txt @@ -0,0 +1 @@ +Hopefully we can read this! diff --git a/book/thebe_lite/sphinx-thebe.js b/book/thebe_lite/sphinx-thebe.js index 1d1754626c06ef161e39f8a13f2cfb8b47886a06..a0ca901ba944e3ca6c39787726f954877b76e61c 100644 --- a/book/thebe_lite/sphinx-thebe.js +++ b/book/thebe_lite/sphinx-thebe.js @@ -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; }; - diff --git a/build-lite.sh b/build-lite.sh index 426703f9c7afe77553dff906753cd6b2c8a86406..eeeb7be6fa3124ce1ac45e30ab9d5696553e8a78 100755 --- a/build-lite.sh +++ b/build-lite.sh @@ -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 & +