Skip to content
Snippets Groups Projects
Commit 9030246f authored by sheepmax's avatar sheepmax
Browse files

Fixed aggressive directory creation, added support for some nested libraries

parent 0b1e359e
No related branches found
No related tags found
2 merge requests!51Blank book,!49Thebe file fetching fixes and improvements
Pipeline #206251 passed
%% Cell type:markdown id: tags:
# Benchmarks: auxiliary files
This page includes benchmarks for auxiliary files included in the book build. We can use these to give instant feedback to the readers of the book when running live code. Examples of this usage will be provided elsewhere in the book.
%% Cell type:code id: tags:
``` python
# Reading from the same directory
with open("reading_test.txt") as f:
print(f.read())
```
%% Cell type:code id: tags:
``` python
# Reading from nested directory
with open("nested/nested_test.txt") as f:
print(f.read())
```
%% Cell type:code id: tags:
``` python
# Reading from relative directory
with open("../taxi_duration.txt") as f:
print(f.read())
```
%% Cell type:code id: tags:
``` python
# Local library test
import my_library
my_library.hello()
```
%% Cell type:code id: tags:
``` python
# Local library in a folder
import nested.nested.nested_library
nested.nested.nested_library.hello()
```
......
# This can be empty
# This can be empty
def hello():
print("Hello world!")
......@@ -9,19 +9,28 @@ import os
class FetchPathFinder(importlib.abc.MetaPathFinder):
@classmethod
def find_spec(cls, fullname, path, target=None):
if path is None:
if path is None or len(path) == 0:
path = os.getcwd()
for suffix in importlib.machinery.SOURCE_SUFFIXES:
fullpath = os.path.join(path, fullname + suffix)
else:
path = path[0]
module_name = fullname.split(".")[-1]
suffixed = [module_name + suffix for suffix in importlib.machinery.SOURCE_SUFFIXES]
# Due to the lack of directory listings, we'll just force nested modules to have an __init__.py
for relpath in [*suffixed, os.path.join(module_name, "__init__.py")]:
fullpath = os.path.join(path, relpath)
try:
# This will cause a lazy load from the server
open(fullpath, "r").close()
except:
continue
loader = importlib.machinery.SourceFileLoader(fullname, fullpath)
return importlib.util.spec_from_loader(fullname, loader=loader)
return None
@classmethod
def invalidate_caches(cls):
return
sys.meta_path.append(FetchPathFinder)
\ No newline at end of file
sys.meta_path.append(FetchPathFinder)
......@@ -325,8 +325,6 @@ function override_pyodide_lookup(fs, server_path) {
const name = pathList[pathList.length - 1]; // Name of file
const parentPathList = pathList.slice(0, -1); // List of all parent directories
createDirectoryStructure(parentPathList);
let request = new XMLHttpRequest();
request.open("GET", path, false);
request.send();
......@@ -334,6 +332,7 @@ function override_pyodide_lookup(fs, server_path) {
if (request.status !== 200) {
throw exception;
}
createDirectoryStructure(parentPathList);
createFileFromString(parentPathList.join("/"), name, request.response);
return old_lookup(fullPath, opts);
}
......@@ -426,11 +425,11 @@ var initThebe = async () => {
// 3. Eval the string og the override_pyodide_lookup function in JS, this brings it into scope
// 4. Execute the override_pyodide_lookup function in JS, and bake in the relative path from root in the book (the home)
// NOTE: All functions used in override_pyodide_lookup should be nested inside it, since the web worker cannot access functions in this script
await thebelab.session.kernel.requestExecute({
thebelab.session.kernel.requestExecute({
code: `import js; import pyodide_js; js.fs = pyodide_js.FS; js.eval("""${override_pyodide_lookup.toString()}"""); js.eval(f"override_pyodide_lookup(fs, '${
location.pathname.split("/").slice(0, -1).join("/") + "/"
}')")`,
}).done;
});
const request = new XMLHttpRequest();
request.open("GET", "/_static/_hook_fetch_module_finder.py", false);
......@@ -439,9 +438,9 @@ var initThebe = async () => {
const fetchImportHookCode = request.response;
// Enable importing of modules on server
await thebelab.session.kernel.requestExecute({
thebelab.session.kernel.requestExecute({
code: fetchImportHookCode,
}).done;
});
// Fix for issues with ipywidgets in Thebe
await thebelab.session.kernel.requestExecute({
......
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