Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
E
erp5-bin
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
erp5-bin
Commits
3873f331
Commit
3873f331
authored
Jul 29, 2019
by
Boxiang Sun
Committed by
Romain Courteaud
Aug 08, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
erp5_notebook: Rearrange the pyodide initialization code
Prepare to initialize pyodide when executing JS code block
parent
da117d95
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
176 additions
and
176 deletions
+176
-176
bt5/erp5_notebook/SkinTemplateItem/portal_skins/erp5_notebook/gadget_jsmd_eval.js.js
...ateItem/portal_skins/erp5_notebook/gadget_jsmd_eval.js.js
+176
-176
No files found.
bt5/erp5_notebook/SkinTemplateItem/portal_skins/erp5_notebook/gadget_jsmd_eval.js.js
View file @
3873f331
...
...
@@ -65,6 +65,182 @@
});
}
function
loadJSResource
(
url
)
{
// Copied from renderJS
return
new
RSVP
.
Promise
(
function
waitForJSLoadEvent
(
resolve
,
reject
)
{
var
newScript
;
newScript
=
document
.
createElement
(
'
script
'
);
newScript
.
async
=
false
;
newScript
.
type
=
'
text/javascript
'
;
newScript
.
onload
=
function
(
evt
)
{
resolve
(
evt
.
target
.
value
);
};
newScript
.
onerror
=
function
(
error
)
{
console
.
warn
(
error
);
reject
(
error
);
};
newScript
.
src
=
url
;
document
.
head
.
appendChild
(
newScript
);
}
);
}
function
loadPyodide
(
imports
,
success_callback
)
{
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
return
ajax
({
url
:
"
pyodide.asm.wasm
"
,
dataType
:
"
arraybuffer
"
});
})
.
push
(
function
(
evt
)
{
// Compile and instantiate WebAssembly code.
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiate
return
WebAssembly
.
instantiate
(
evt
.
target
.
response
,
imports
);
})
.
push
(
function
(
result
)
{
// This function act as instantiateWasm
// And in instantiateWasm, we have to call the second parameter
// See https://emscripten.org/docs/api_reference/module.html#Module.instantiateWasm
return
success_callback
(
result
.
instance
);
});
}
function
checkABI
(
ABI_number
)
{
// This was needed by pyodide wasm module
if
(
ABI_number
!==
1
)
{
throw
new
Error
(
"
Expect ABI number 1, got
"
+
ABI_number
);
}
return
true
;
}
// Pyodide package loading
function
pyodideLoadPackage
(
names
)
{
var
queue
,
toLoad
,
package_name
,
k
,
subpackage
,
packages
,
loadedPackages
,
defer
;
packages
=
pyodide_instance
.
packages
.
dependencies
;
loadedPackages
=
pyodide_instance
.
loadedPackages
;
// packages is dependency list of each package
// e.g.: [pandas: ['numpy', 'python-dateutil', 'pytz'], numpy: []]
queue
=
new
Array
(
names
);
toLoad
=
[];
defer
=
RSVP
.
defer
();
// Find all packages that need to load, store in the "toLoad" list
while
(
queue
.
length
)
{
package_name
=
queue
.
pop
();
if
(
loadedPackages
.
indexOf
(
package_name
)
===
-
1
)
{
toLoad
.
push
(
package_name
);
// packages is the dependency list
if
(
packages
.
hasOwnProperty
(
package_name
))
{
for
(
k
=
0
;
k
<
packages
[
package_name
].
length
;
k
+=
1
)
{
subpackage
=
packages
[
package_name
][
k
];
if
(
loadedPackages
.
indexOf
(
subpackage
)
===
-
1
&&
toLoad
.
indexOf
(
subpackage
)
===
-
1
)
{
queue
.
push
(
subpackage
);
}
}
}
else
{
throw
new
Error
(
"
Unknown package
"
+
package_name
);
}
}
}
if
(
toLoad
.
length
===
0
)
{
// No new packages to load
return
;
}
// pyodide built with option "--use-preload-plugins"
// So emscripten load the pyodide itself and its extension using createPreloadedFile.
// See createPreloadedFile section in:
// https://github.com/emscripten-core/emscripten/blob/master/site/source/docs/api_reference/Filesystem-API.rst
// And createPreloadedFile use addRunDependency or removeRunDependency to handle the dependencies,
// it will call monitorRunDependencies.
// https://github.com/emscripten-core/emscripten/blob/master/src/preamble.js#L1842
// The logic in here is if we loaded all wasm module, aka the "left" is 0.
// We load all shared lib to the wasm file system through loadSharedlib.
pyodide_instance
.
monitorRunDependencies
=
function
(
left
)
{
if
(
left
===
0
)
{
for
(
k
=
0
;
k
<
toLoad
.
length
;
k
+=
1
)
{
package_name
=
toLoad
[
k
];
loadedPackages
.
push
(
package_name
);
}
delete
pyodide_instance
.
monitorRunDependencies
;
return
loadSharedlib
(
pyodide_instance
.
_module
.
FS
)
.
push
(
function
()
{
defer
.
resolve
();
return
;
});
}
};
for
(
k
=
0
;
k
<
toLoad
.
length
;
k
+=
1
)
{
package_name
=
toLoad
[
k
];
loadJSResource
(
package_name
+
"
.js
"
);
}
pyodide_instance
.
runPython
(
'
import importlib as _importlib
\n
'
+
'
_importlib.invalidate_caches()
\n
'
);
return
defer
.
promise
;
}
function
initPyodide
()
{
var
Module
=
{};
// perform the WebAssembly instantiation action
// See https://emscripten.org/docs/api_reference/module.html#Module.instantiateWasm
Module
.
instantiateWasm
=
loadPyodide
;
Module
.
checkABI
=
checkABI
;
window
.
Module
=
Module
;
return
RSVP
.
Queue
()
.
push
(
function
()
{
return
loadJSResource
(
'
pyodide.asm.data.js
'
);
})
.
push
(
function
()
{
return
loadJSResource
(
'
pyodide.asm.js
'
);
})
.
push
(
function
()
{
// pyodide is a function defined in pyodide.asm.js
// which set the functions under Module
pyodide_instance
=
window
.
pyodide
(
Module
);
var
defer
=
RSVP
.
defer
();
// define postRun to resolve the promise
// When pyodide loading completed, it will call Module.postRun
// to signal the module loaded and ready for next step
// https://github.com/emscripten-core/emscripten/blob/1.38.10/src/preamble.js#L1602
// https://emscripten.org/docs/api_reference/preamble.js.html
Module
.
postRun
=
defer
.
resolve
;
return
defer
.
promise
;
})
.
push
(
function
()
{
return
ajax
({
url
:
'
packages.json
'
});
})
.
push
(
function
(
evt
)
{
return
JSON
.
parse
(
evt
.
target
.
response
);
})
.
push
(
function
(
json
)
{
pyodide_instance
.
_module
=
Module
;
pyodide_instance
.
loadedPackages
=
[];
pyodide_instance
.
_module
.
packages
=
json
;
// Make pyodide happy...
// pyodide.asm.js need window.pyodide:
// https://github.com/iodide-project/pyodide/blob/master/src/runpython.c#L91
// var packageNames = self.pyodide._module.packages.import_name_to_package_name;
// import_name_to_package_name was defined when reading package.json
// https://github.com/iodide-project/pyodide/blob/master/pyodide_build/buildall.py#L58
window
.
pyodide
=
pyodide_instance
;
return
;
});
}
function
parseJSMDCellList
(
jsmd
)
{
// Split the text into a list of Iodide cells
var
line_list
=
jsmd
.
split
(
split_line_regex
),
...
...
@@ -143,27 +319,6 @@
document
.
head
.
appendChild
(
style
);
}
function
loadJSResource
(
url
)
{
// Copied from renderJS
return
new
RSVP
.
Promise
(
function
waitForJSLoadEvent
(
resolve
,
reject
)
{
var
newScript
;
newScript
=
document
.
createElement
(
'
script
'
);
newScript
.
async
=
false
;
newScript
.
type
=
'
text/javascript
'
;
newScript
.
onload
=
function
(
evt
)
{
resolve
(
evt
.
target
.
value
);
};
newScript
.
onerror
=
function
(
error
)
{
console
.
warn
(
error
);
reject
(
error
);
};
newScript
.
src
=
url
;
document
.
head
.
appendChild
(
newScript
);
}
);
}
function
deferJSResourceLoading
(
url
)
{
return
function
()
{
return
loadJSResource
(
url
);
...
...
@@ -265,161 +420,6 @@
});
}
function
loadPyodide
(
imports
,
success_callback
)
{
return
new
RSVP
.
Queue
()
.
push
(
function
()
{
return
ajax
({
url
:
"
pyodide.asm.wasm
"
,
dataType
:
"
arraybuffer
"
});
})
.
push
(
function
(
evt
)
{
// Compile and instantiate WebAssembly code.
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiate
return
WebAssembly
.
instantiate
(
evt
.
target
.
response
,
imports
);
})
.
push
(
function
(
result
)
{
// This function act as instantiateWasm
// And in instantiateWasm, we have to call the second parameter
// See https://emscripten.org/docs/api_reference/module.html#Module.instantiateWasm
return
success_callback
(
result
.
instance
);
});
}
function
checkABI
(
ABI_number
)
{
// This was needed by pyodide wasm module
if
(
ABI_number
!==
1
)
{
throw
new
Error
(
"
Expect ABI number 1, got
"
+
ABI_number
);
}
return
true
;
}
// Pyodide package loading
function
pyodideLoadPackage
(
names
)
{
var
queue
,
toLoad
,
package_name
,
k
,
subpackage
,
packages
,
loadedPackages
,
defer
;
packages
=
pyodide_instance
.
packages
.
dependencies
;
loadedPackages
=
pyodide_instance
.
loadedPackages
;
// packages is dependency list of each package
// e.g.: [pandas: ['numpy', 'python-dateutil', 'pytz'], numpy: []]
queue
=
new
Array
(
names
);
toLoad
=
[];
defer
=
RSVP
.
defer
();
// Find all packages that need to load, store in the "toLoad" list
while
(
queue
.
length
)
{
package_name
=
queue
.
pop
();
if
(
loadedPackages
.
indexOf
(
package_name
)
===
-
1
)
{
toLoad
.
push
(
package_name
);
// packages is the dependency list
if
(
packages
.
hasOwnProperty
(
package_name
))
{
for
(
k
=
0
;
k
<
packages
[
package_name
].
length
;
k
+=
1
)
{
subpackage
=
packages
[
package_name
][
k
];
if
(
loadedPackages
.
indexOf
(
subpackage
)
===
-
1
&&
toLoad
.
indexOf
(
subpackage
)
===
-
1
)
{
queue
.
push
(
subpackage
);
}
}
}
else
{
throw
new
Error
(
"
Unknown package
"
+
package_name
);
}
}
}
if
(
toLoad
.
length
===
0
)
{
// No new packages to load
return
;
}
// pyodide built with option "--use-preload-plugins"
// So emscripten load the pyodide itself and its extension using createPreloadedFile.
// See createPreloadedFile section in:
// https://github.com/emscripten-core/emscripten/blob/master/site/source/docs/api_reference/Filesystem-API.rst
// And createPreloadedFile use addRunDependency or removeRunDependency to handle the dependencies,
// it will call monitorRunDependencies.
// https://github.com/emscripten-core/emscripten/blob/master/src/preamble.js#L1842
// The logic in here is if we loaded all wasm module, aka the "left" is 0.
// We load all shared lib to the wasm file system through loadSharedlib.
pyodide_instance
.
monitorRunDependencies
=
function
(
left
)
{
if
(
left
===
0
)
{
for
(
k
=
0
;
k
<
toLoad
.
length
;
k
+=
1
)
{
package_name
=
toLoad
[
k
];
loadedPackages
.
push
(
package_name
);
}
delete
pyodide_instance
.
monitorRunDependencies
;
return
loadSharedlib
(
pyodide_instance
.
_module
.
FS
)
.
push
(
function
()
{
defer
.
resolve
();
return
;
});
}
};
for
(
k
=
0
;
k
<
toLoad
.
length
;
k
+=
1
)
{
package_name
=
toLoad
[
k
];
loadJSResource
(
package_name
+
"
.js
"
);
}
pyodide_instance
.
runPython
(
'
import importlib as _importlib
\n
'
+
'
_importlib.invalidate_caches()
\n
'
);
return
defer
.
promise
;
}
function
initPyodide
()
{
var
Module
=
{};
// perform the WebAssembly instantiation action
// See https://emscripten.org/docs/api_reference/module.html#Module.instantiateWasm
Module
.
instantiateWasm
=
loadPyodide
;
Module
.
checkABI
=
checkABI
;
window
.
Module
=
Module
;
return
RSVP
.
Queue
()
.
push
(
function
()
{
return
loadJSResource
(
'
pyodide.asm.data.js
'
);
})
.
push
(
function
()
{
return
loadJSResource
(
'
pyodide.asm.js
'
);
})
.
push
(
function
()
{
// pyodide is a function defined in pyodide.asm.js
// which set the functions under Module
pyodide_instance
=
window
.
pyodide
(
Module
);
var
defer
=
RSVP
.
defer
();
// define postRun to resolve the promise
// When pyodide loading completed, it will call Module.postRun
// to signal the module loaded and ready for next step
// https://github.com/emscripten-core/emscripten/blob/1.38.10/src/preamble.js#L1602
// https://emscripten.org/docs/api_reference/preamble.js.html
Module
.
postRun
=
defer
.
resolve
;
return
defer
.
promise
;
})
.
push
(
function
()
{
return
ajax
({
url
:
'
packages.json
'
});
})
.
push
(
function
(
evt
)
{
return
JSON
.
parse
(
evt
.
target
.
response
);
})
.
push
(
function
(
json
)
{
pyodide_instance
.
_module
=
Module
;
pyodide_instance
.
loadedPackages
=
[];
pyodide_instance
.
_module
.
packages
=
json
;
// Make pyodide happy...
// pyodide.asm.js need window.pyodide:
// https://github.com/iodide-project/pyodide/blob/master/src/runpython.c#L91
// var packageNames = self.pyodide._module.packages.import_name_to_package_name;
// import_name_to_package_name was defined when reading package.json
// https://github.com/iodide-project/pyodide/blob/master/pyodide_build/buildall.py#L58
window
.
pyodide
=
pyodide_instance
;
return
;
});
}
function
renderCodeblock
(
result_text
)
{
var
div
=
document
.
createElement
(
'
div
'
),
pre
=
document
.
createElement
(
'
pre
'
),
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment