Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Basthon
Basthon Kernel
Commits
d87744ca
Commit
d87744ca
authored
Oct 17, 2022
by
Romain Casati
Browse files
Python 3: multiple URL lookup for Pyodide.
parent
29ccac6d
Changes
2
Hide whitespace changes
Inline
Side-by-side
packages/kernel-python3-old/src/kernel.ts
View file @
d87744ca
import
{
KernelBase
}
from
'
@basthon/kernel-base
'
;
const
PYODIDE_VERSION
=
"
0.16.1
"
;
declare
global
{
interface
Window
{
...
...
@@ -17,13 +18,15 @@ export class KernelPython3Old extends KernelBase {
/**
* Where to find pyodide.js (private).
*/
private
_pyodideU
rl
=
"
https://cdn.jsdelivr.net/pyodide/v
0.16.1
/full/pyodide.js
"
;
private
_pyodideU
RLs
=
[
`
https://cdn.jsdelivr.net/pyodide/v
{PYODIDE_VERSION}
/full/pyodide.js
`
];
;
private
__kernel__
:
any
=
null
;
public
pythonVersion
:
string
=
""
;
constructor
(
options
:
any
)
{
super
(
options
);
this
.
_pyodideUrl
=
options
?.
pyodideUrl
??
this
.
_pyodideUrl
;
// for locally installed Pyodide
this
.
_pyodideURLs
.
unshift
(
`
${
this
.
basthonRoot
()}
/pyodide/pyodide.js`
);
this
.
_pyodideURLs
=
options
?.
pyodideURLs
??
this
.
_pyodideURLs
;
}
/**
...
...
@@ -62,18 +65,23 @@ export class KernelPython3Old extends KernelBase {
* Start the Basthon kernel asynchronously.
*/
public
async
launch
()
{
/* testing if Pyodide is installed locally */
try
{
const
url
=
this
.
basthonRoot
()
+
"
/pyodide/pyodide.js
"
;
const
response
=
await
fetch
(
url
,
{
method
:
"
HEAD
"
});
if
(
response
.
ok
)
this
.
_pyodideUrl
=
url
;
}
catch
(
e
)
{
}
let
pyodideURL
:
string
=
this
.
_pyodideURLs
[
0
];
for
(
let
url
of
this
.
_pyodideURLs
)
{
url
=
url
.
replace
(
"
{PYODIDE_VERSION}
"
,
PYODIDE_VERSION
);
try
{
const
response
=
await
fetch
(
url
,
{
method
:
"
HEAD
"
});
if
(
response
.
ok
)
{
pyodideURL
=
url
;
break
;
}
}
catch
(
e
)
{
}
}
// forcing Pyodide to look at the right location for other files
window
.
languagePluginUrl
=
this
.
_
pyodideU
rl
.
substr
(
0
,
this
.
_
pyodideU
rl
.
lastIndexOf
(
'
/
'
))
+
'
/
'
;
window
.
languagePluginUrl
=
pyodideU
RL
.
substr
(
0
,
pyodideU
RL
.
lastIndexOf
(
'
/
'
))
+
'
/
'
;
try
{
await
KernelPython3Old
.
loadScript
(
this
.
_
pyodideU
rl
);
await
KernelPython3Old
.
loadScript
(
pyodideU
RL
);
}
catch
(
error
)
{
console
.
log
(
error
);
console
.
error
(
"
Can't load pyodide.js
"
);
...
...
packages/kernel-python3/src/kernel.ts
View file @
d87744ca
...
...
@@ -18,13 +18,15 @@ export class KernelPython3 extends KernelBase {
/**
* Where to find pyodide.js (private).
*/
private
_pyodideU
rl
=
`https://cdn.jsdelivr.net/pyodide/v
$
{
PYODIDE_VERSION
}
/full/pyodide.js`
;
private
_pyodideU
RLs
=
[
`https://cdn.jsdelivr.net/pyodide/v{PYODIDE_VERSION}/full/pyodide.js`
]
;
private
__kernel__
:
any
=
null
;
public
pythonVersion
:
string
=
""
;
constructor
(
options
:
any
)
{
super
(
options
);
this
.
_pyodideUrl
=
options
?.
pyodideUrl
??
this
.
_pyodideUrl
;
// for locally installed Pyodide
this
.
_pyodideURLs
.
unshift
(
`
${
this
.
basthonRoot
()}
/pyodide/pyodide.js`
);
this
.
_pyodideURLs
=
options
?.
pyodideURLs
??
this
.
_pyodideURLs
;
}
/**
...
...
@@ -60,15 +62,20 @@ export class KernelPython3 extends KernelBase {
* Start the Basthon kernel asynchronously.
*/
public
async
launch
()
{
/* testing if Pyodide is installed locally */
try
{
const
url
=
this
.
basthonRoot
()
+
"
/pyodide/pyodide.js
"
;
const
response
=
await
fetch
(
url
,
{
method
:
"
HEAD
"
});
if
(
response
.
ok
)
this
.
_pyodideUrl
=
url
;
}
catch
(
e
)
{
}
let
pyodideURL
:
string
=
this
.
_pyodideURLs
[
0
];
for
(
let
url
of
this
.
_pyodideURLs
)
{
url
=
url
.
replace
(
"
{PYODIDE_VERSION}
"
,
PYODIDE_VERSION
);
try
{
const
response
=
await
fetch
(
url
,
{
method
:
"
HEAD
"
});
if
(
response
.
ok
)
{
pyodideURL
=
url
;
break
;
}
}
catch
(
e
)
{
}
}
try
{
await
KernelPython3
.
loadScript
(
this
.
_
pyodideU
rl
);
await
KernelPython3
.
loadScript
(
pyodideU
RL
);
}
catch
(
error
)
{
console
.
log
(
error
);
console
.
error
(
"
Can't load pyodide.js
"
);
...
...
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment