Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
hoxbro committed Feb 22, 2024
0 parents commit 2ff12a0
Show file tree
Hide file tree
Showing 102 changed files with 35,357 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .buildinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Sphinx build info version 1
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
config: 5f2d4d17b6edc435204c5787124fca34
tags: 645f666f9bcd5a90fca523b33c5a78b7
Empty file added .nojekyll
Empty file.
57 changes: 57 additions & 0 deletions PyodideServiceWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const appName = 'nbsite'
const appCacheName = 'nbsite-0.8.5a2';

const preCacheFiles = [];

const cachePatterns = ['https://cdn.holoviz.org/panel/1.3.8/dist/', 'https://cdn.bokeh.org/bokeh/', 'https://cdn.jsdelivr.net/pyodide/', 'https://files.pythonhosted.org/packages/', 'https://pypi.org/pypi/'];

self.addEventListener('install', (e) => {
console.log('[Service Worker] Install');
self.skipWaiting();
e.waitUntil((async () => {
const cacheNames = await caches.keys();
for (const cacheName of cacheNames) {
if (cacheName.startsWith(appName) && cacheName !== appCacheName) {
console.log(`[Service Worker] Delete old cache ${cacheName}`);
caches.delete(cacheName);
}
}
const cache = await caches.open(appCacheName);
if (preCacheFiles.length) {
console.log('[Service Worker] Precaching ');
}
preCacheFiles.forEach(async (cacheFile) => {
const request = new Request(cacheFile);
const response = await fetch(request);
if (response.ok || response.type == 'opaque') {
cache.put(request, response);
}
})
})());
});

self.addEventListener('activate', (event) => {
console.log('[Service Worker] Activating');
return self.clients.claim();
});

self.addEventListener('fetch', (e) => {
if (e.request.method !== 'GET') {
return
}
e.respondWith((async () => {
const cache = await caches.open(appCacheName);
let response = await cache.match(e.request);
console.log(`[Service Worker] Fetching resource: ${e.request.url}`);
if (response) {
return response;
}
response = await fetch(e.request);
if (!response.ok && !(response.type == 'opaque')) {
throw Error(`[Service Worker] Fetching resource ${e.request.url} failed with response: ${response.status}`);
}
console.log(`[Service Worker] Caching new resource: ${e.request.url}`);
cache.put(e.request, response.clone());
return response;
})());
});
Binary file added _images/thing1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _images/thing2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions _sphinx_design_static/design-tabs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var sd_labels_by_text = {};

function ready() {
const li = document.getElementsByClassName("sd-tab-label");
for (const label of li) {
syncId = label.getAttribute("data-sync-id");
if (syncId) {
label.onclick = onLabelClick;
if (!sd_labels_by_text[syncId]) {
sd_labels_by_text[syncId] = [];
}
sd_labels_by_text[syncId].push(label);
}
}
}

function onLabelClick() {
// Activate other inputs with the same sync id.
syncId = this.getAttribute("data-sync-id");
for (label of sd_labels_by_text[syncId]) {
if (label === this) continue;
label.previousElementSibling.checked = true;
}
window.localStorage.setItem("sphinx-design-last-tab", syncId);
}

document.addEventListener("DOMContentLoaded", ready, false);
221 changes: 221 additions & 0 deletions _static/PyodideWebWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
importScripts("https://cdn.jsdelivr.net/pyodide/v0.23.1/full/pyodide.js");

const QUEUE = [];

const REQUIRES = {}

function sendPatch(patch, buffers, cell_id) {
self.postMessage({
type: 'patch',
patch: patch,
buffers: buffers,
id: cell_id
})
}

function sendStdout(cell_id, stdout) {
self.postMessage({
type: 'stdout',
content: stdout,
id: cell_id
})
}
function sendStderr(cell_id, stderr) {
self.postMessage({
type: 'stderr',
content: stderr,
id: cell_id
})
}

async function loadApplication(cell_id, path) {
console.log("Loading pyodide!");
self.pyodide = await loadPyodide();
self.pyodide.globals.set("sendPatch", sendPatch);
self.pyodide.globals.set("sendStdout", sendStdout);
self.pyodide.globals.set("sendStderr", sendStderr);
console.log("Loaded!");
await self.pyodide.loadPackage("micropip");
const packages = ['panel', 'pandas'];
if (path != null) {
for (const key of Object.keys(REQUIRES)) {
if (path.replace('.html', '').endsWith(key.replace('.md', ''))) {
for (const req of REQUIRES[key]) {
packages.push(req)
}
}
}
}

await self.pyodide.runPythonAsync("")
self.pyodide.runPython("import micropip")
for (const pkg of packages) {
self.postMessage({
type: 'loading',
msg: `Loading ${pkg}`,
id: cell_id
});
await self.pyodide.runPythonAsync(`
await micropip.install('${pkg}', keep_going=True);
`);
}
console.log("Packages loaded!");
}

const autodetect_deps_code = `
import json
try:
from panel.io.mime_render import find_requirements
except Exception:
from panel.io.mime_render import find_imports as find_requirements
json.dumps(find_requirements(msg.to_py()['code']))`

const exec_code = `
from functools import partial
from panel.io.pyodide import pyrender
from js import console
msg = msg.to_py()
code = msg['code']
stdout_cb = partial(sendStdout, msg['id'])
stderr_cb = partial(sendStderr, msg['id'])
target = f"output-{msg['id']}"
pyrender(code, stdout_cb, stderr_cb, target)`

const onload_code = `
msg = msg.to_py()
if msg['mime'] == 'application/bokeh':
from panel.io.pyodide import _link_docs_worker
from panel.io.state import state
doc = state.cache[f"output-{msg['id']}"]
_link_docs_worker(doc, sendPatch, msg['id'], 'js')`

const patch_code = `
import json
from panel import state
msg = msg.to_py()
doc = state.cache[f"output-{msg['id']}"]
doc.apply_json_patch(msg['patch'], setter='js')`

const MESSAGES = {
patch: patch_code,
execute: exec_code,
rendered: onload_code
}

self.onmessage = async (event) => {
let resolveExecution, rejectExecution;
const executing = new Promise(function(resolve, reject){
resolveExecution = resolve;
rejectExecution = reject;
});

const prev_msg = QUEUE[0]
const msg = {...event.data, executing}
QUEUE.unshift(msg)

if (prev_msg) {
self.postMessage({
type: 'loading',
msg: 'Awaiting previous cells',
id: msg.id
});
await prev_msg.executing
}

// Init pyodide
if (self.pyodide == null) {
self.postMessage({
type: 'loading',
msg: 'Loading pyodide',
id: msg.id
});
await loadApplication(msg.id, msg.path)
self.postMessage({
type: 'loaded',
id: msg.id
});
}

// Handle message
if (!MESSAGES.hasOwnProperty(msg.type)) {
console.warn(`Service worker received unknown message type '${msg.type}'.`)
resolveExecution()
self.postMessage({
type: 'idle',
id: msg.id
});
return
}

if (msg.type === 'execute') {
let deps
try {
self.pyodide.globals.set('msg', msg)
deps = self.pyodide.runPython(autodetect_deps_code)
} catch(e) {
deps = '[]'
console.warn(`Auto-detection of dependencies failed with error: ${e}`)
}
for (const pkg of JSON.parse(deps)) {
self.postMessage({
type: 'loading',
msg: `Loading ${pkg}`,
id: msg.id
});
try {
await self.pyodide.runPythonAsync(`await micropip.install('${pkg}', keep_going=True)`)
} catch(e) {
console.log(`Auto-detected dependency ${pkg} could not be installed.`)
}
}
}

try {
self.pyodide.globals.set('msg', msg)
let out = await self.pyodide.runPythonAsync(MESSAGES[msg.type])
resolveExecution()
if (out == null) {
out = new Map()
}
if (out.has('content')) {
self.postMessage({
type: 'render',
id: msg.id,
content: out.get('content'),
mime: out.get('mime_type')
});
}
if (out.has('stdout') && out.get('stdout').length) {
self.postMessage({
type: 'stdout',
content: out.get('stdout'),
id: msg.id
});
}
if (out.has('stderr') && out.get('stderr').length) {
self.postMessage({
type: 'stderr',
content: out.get('stderr'),
id: msg.id
});
}
self.postMessage({
type: 'idle',
id: msg.id,
uuid: msg.uuid
});
} catch (e) {
const traceback = `${e}`
const tblines = traceback.split('\n')
self.postMessage({
type: 'error',
traceback: traceback,
msg: tblines[tblines.length-2],
id: msg.id
});
resolveExecution()
throw(e)
}
}
14 changes: 14 additions & 0 deletions _static/ServiceHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register(DOCUMENTATION_OPTIONS.URL_ROOT + 'PyodideServiceWorker.js').then(reg => {
reg.onupdatefound = () => {
const installingWorker = reg.installing;
installingWorker.onstatechange = () => {
if (installingWorker.state === 'installed' &&
navigator.serviceWorker.controller) {
// Reload page if service worker is replaced
location.reload();
}
}
}
})
}
Loading

0 comments on commit 2ff12a0

Please sign in to comment.