-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a way to register JS modules directly
- Loading branch information
1 parent
7f84887
commit 061ef5f
Showing
9 changed files
with
125 additions
and
10 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/*! coi-serviceworker v0.1.7 - Guido Zuidhof and contributors, licensed under MIT */ | ||
/*! mini-coi - Andrea Giammarchi and contributors, licensed under MIT */ | ||
(({ document: d, navigator: { serviceWorker: s } }) => { | ||
if (d) { | ||
const { currentScript: c } = d; | ||
s.register(c.src, { scope: c.getAttribute('scope') || '.' }).then(r => { | ||
r.addEventListener('updatefound', () => location.reload()); | ||
if (r.active && !s.controller) location.reload(); | ||
}); | ||
} | ||
else { | ||
addEventListener('install', () => skipWaiting()); | ||
addEventListener('activate', e => e.waitUntil(clients.claim())); | ||
addEventListener('fetch', e => { | ||
const { request: r } = e; | ||
if (r.cache === 'only-if-cached' && r.mode !== 'same-origin') return; | ||
e.respondWith(fetch(r).then(r => { | ||
const { body, status, statusText } = r; | ||
if (!status || status > 399) return r; | ||
const h = new Headers(r.headers); | ||
h.set('Cross-Origin-Opener-Policy', 'same-origin'); | ||
h.set('Cross-Origin-Embedder-Policy', 'require-corp'); | ||
h.set('Cross-Origin-Resource-Policy', 'cross-origin'); | ||
return new Response(body, { status, statusText, headers: h }); | ||
})); | ||
}); | ||
} | ||
})(self); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width,initial-scale=1.0" /> | ||
<title>python</title> | ||
<style>#main-map, #worker-map { height: 320px; } h3 { margin-bottom: 0; }</style> | ||
<link rel="stylesheet" href="style.css" /> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet.css" /> | ||
<script type="module" src="../core.js"></script> | ||
</head> | ||
<body> | ||
<!-- | ||
main only: npx static-handler . | ||
main/worker: npx static-handler --coop --coep . | ||
Note: --corp breaks the tiles server | ||
--> | ||
|
||
<!-- Use js_modules via config --> | ||
<h3>Main</h3> | ||
<div id="main-map"></div> | ||
<script type="pyodide" config="./modules.toml"> | ||
# needed to fix pyodide proxies once passed along | ||
from pyodide.ffi import to_js | ||
|
||
# registered as Python module | ||
import leaflet as L | ||
|
||
center = to_js([51.505, -0.09]) | ||
mark = to_js([51.5, -0.09]) | ||
map = L.map('main-map').setView(center, 13); | ||
|
||
L.tileLayer( | ||
'https://tile.openstreetmap.org/{z}/{x}/{y}.png', | ||
to_js({"attribution": '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'}) | ||
).addTo(map); | ||
|
||
L.marker(mark).addTo(map).bindPopup('A pretty CSS popup.<br> Easily customizable.').openPopup(); | ||
</script> | ||
|
||
<hr> | ||
|
||
<!-- Land js modules on main --> | ||
<h3>Worker</h3> | ||
<div id="worker-map"></div> | ||
<script type="module"> | ||
import * as leaflet from "https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet-src.esm.js"; | ||
globalThis.leaflet = leaflet; | ||
</script> | ||
<script type="pyodide" worker> | ||
# used to reach the main thread window proxy | ||
from polyscript import xworker | ||
|
||
# reach directly the module from main | ||
L = xworker.window.leaflet | ||
|
||
center = [51.505, -0.09] | ||
mark = [51.5, -0.09] | ||
map = L.map('worker-map').setView(center, 13); | ||
|
||
L.tileLayer( | ||
'https://tile.openstreetmap.org/{z}/{x}/{y}.png', | ||
{"attribution": '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'} | ||
).addTo(map); | ||
|
||
L.marker(mark).addTo(map).bindPopup('A pretty CSS popup.<br> Easily customizable.').openPopup(); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[js_modules] | ||
"https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet-src.esm.js" = "leaflet" |