Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Book
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
MUDE
Book
Commits
0b1e359e
Commit
0b1e359e
authored
1 year ago
by
sheepmax
Browse files
Options
Downloads
Patches
Plain Diff
Fixed file fetching and made imports no longer rely on directory listings:
parent
0089a3e5
No related branches found
Branches containing commit
No related tags found
Tags containing commit
2 merge requests
!51
Blank book
,
!49
Thebe file fetching fixes and improvements
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
thebe_lite/_static/_hook_fetch_module_finder.py
+27
-0
27 additions, 0 deletions
thebe_lite/_static/_hook_fetch_module_finder.py
thebe_lite/_static/sphinx-thebe.js
+13
-31
13 additions, 31 deletions
thebe_lite/_static/sphinx-thebe.js
with
40 additions
and
31 deletions
thebe_lite/_static/_hook_fetch_module_finder.py
0 → 100644
+
27
−
0
View file @
0b1e359e
import
importlib.abc
import
importlib.machinery
import
importlib.util
import
sys
import
os
# Finds modules by stupidly fetching from the server
class
FetchPathFinder
(
importlib
.
abc
.
MetaPathFinder
):
@classmethod
def
find_spec
(
cls
,
fullname
,
path
,
target
=
None
):
if
path
is
None
:
path
=
os
.
getcwd
()
for
suffix
in
importlib
.
machinery
.
SOURCE_SUFFIXES
:
fullpath
=
os
.
path
.
join
(
path
,
fullname
+
suffix
)
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
sys
.
meta_path
.
append
(
FetchPathFinder
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
thebe_lite/_static/sphinx-thebe.js
+
13
−
31
View file @
0b1e359e
...
...
@@ -260,8 +260,8 @@ function override_pyodide_lookup(fs, server_path) {
// 1. The default /drive home is a mount which does not play well with certain file operations
// 2. I thought it would help fetching py files from root, but that returns index.html instead of the directory listing
// This means py files inside the root folder of the server cannot be loaded implcitly, but you can still use open etc. to fetch them
// NOTE: The slash at the end is important, keep it if you change anything.
const
home
=
"
/home/pyodide/book
/
"
;
// NOTE: The
LACK of
slash at the end is important, keep it
that way
if you change anything.
const
home
=
"
/home/pyodide/book
"
;
// Create a file from the string without using the writeFile method
// writeFile calls lookupPath, which may cause infinite recursion?? (haven't tested)
...
...
@@ -297,35 +297,6 @@ function override_pyodide_lookup(fs, server_path) {
currentDirectory
+=
directory
+
"
/
"
;
try
{
fs
.
mkdir
(
currentDirectory
);
// Fetching the directory needs to return a directory listing
// This feature may not be enabled on some servers and will fail
let
request
=
new
XMLHttpRequest
();
const
fullPath
=
currentDirectory
;
const
path
=
currentDirectory
.
slice
(
home
.
length
);
request
.
open
(
"
GET
"
,
path
,
false
);
request
.
send
();
if
(
request
.
status
!==
200
)
{
continue
;
}
// Find alls all the links with .py files in the directory listing
// We cannot use DOMParser here since it's running inside a web worker: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API
const
regex
=
/
\<
a href=
\"(
.*
?\.
py
)\"\>(
.*
?\.
py
)\<
.*
?\>
/g
;
regex
.
lastIndex
=
0
;
for
(
const
match
of
request
.
response
.
matchAll
(
regex
))
{
// match[1] contains the name of the python file
request
.
open
(
"
GET
"
,
path
+
match
[
1
],
false
);
request
.
send
();
if
(
request
.
status
!==
200
)
{
continue
;
}
createFileFromString
(
fullPath
,
match
[
1
],
request
.
response
);
}
}
catch
{}
}
}
...
...
@@ -461,6 +432,17 @@ var initThebe = async () => {
}
')")`
,
}).
done
;
const
request
=
new
XMLHttpRequest
();
request
.
open
(
"
GET
"
,
"
/_static/_hook_fetch_module_finder.py
"
,
false
);
request
.
send
();
const
fetchImportHookCode
=
request
.
response
;
// Enable importing of modules on server
await
thebelab
.
session
.
kernel
.
requestExecute
({
code
:
fetchImportHookCode
,
}).
done
;
// Fix for issues with ipywidgets in Thebe
await
thebelab
.
session
.
kernel
.
requestExecute
({
code
:
`import ipykernel; ipykernel.version_info = (0,0)`
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment