Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
# Pytml 2.5.0
# Pytml 2.5.1

- Supports inline Pytml from `file://` pages.
- Keeps HTTPS runtime loading through the public `pytml.js` URL.
- Supports inline Python on `file://` pages while keeping Pyodide on HTTPS.
- Loads Pyodide only when a page contains Python blocks.
- Gives a clear error for relative local Python `src` files under `file://`.
- Improves async callbacks, timer handling, and HTML escaping.

Changelog

## 2.5.1 — 2026-10-19

- Added inline Python support for `file://` pages and clearer errors for local Python `src` files.
- Avoided loading Pyodide on pages without Python blocks.
- Improved async callbacks, timer handling, and HTML escaping.

## 2.5.0 — 2026-10-02

- Kept Pyodide 314.0.7 as the runtime.
Expand Down
38 changes: 19 additions & 19 deletions dist/pytml.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Pytml 2.5.1
* Python + HTML, powered by Pyodide.
*
*
* Pytml 2.5 includes a lightweight inline UI preprocessor for <pyN> blocks.
* The full document/language compiler remains planned for v3.0.
*/
Expand Down Expand Up @@ -333,10 +333,10 @@ def query(selector): return element(selector)
def query_all(selector): return [Element(x) for x in js.document.querySelectorAll(str(selector))]
def joystick(target): return Joystick(js.pytmlInstance.makeJoystick(target).element)
async def sleep(ms): await js.pytmlInstance.sleep(int(ms))
def timeout(callback, ms):
def timeout(callback, ms):
from pyodide.ffi import create_proxy
return js.pytmlInstance.setTimeout(create_proxy(callback), int(ms))
def interval(callback, ms):
def interval(callback, ms):
from pyodide.ffi import create_proxy
return js.pytmlInstance.setInterval(create_proxy(callback), int(ms))
def clear_timeout(timer_id): return js.pytmlInstance.clearTimeout(int(timer_id))
Expand Down Expand Up @@ -814,35 +814,35 @@ async def _pytml_run_source(source):
hideStatus() { if (this.statusElement) { this.statusElement.remove(); this.statusElement=null; } }

setTimeout(fn, ms) {
const id = window.setTimeout(() => {
try { fn(); } catch (e) { this.addError(`Timer error: ${e.message}`); }
const id = window.setTimeout(() => {
try { fn(); } catch (e) { this.addError(`Timer error: ${e.message}`); }
}, Number(ms)||0);
this._timeouts.set(id, fn);
this._timeouts.set(id, fn);
return id;
}

setInterval(fn, ms) {
const id = window.setInterval(() => {
try { fn(); } catch (e) { this.addError(`Timer error: ${e.message}`); }
const id = window.setInterval(() => {
try { fn(); } catch (e) { this.addError(`Timer error: ${e.message}`); }
}, Number(ms)||0);
this._intervals.set(id, fn);
this._intervals.set(id, fn);
return id;
}
clearTimeout(id) {
window.clearTimeout(id);
clearTimeout(id) {
window.clearTimeout(id);
const fn = this._timeouts.get(id);
if (fn) {
try { fn.destroy(); } catch (_) {}
this._timeouts.delete(id);
if (fn) {
try { fn.destroy(); } catch (_) {}
this._timeouts.delete(id);
}
}

clearInterval(id) {
window.clearInterval(id);
clearInterval(id) {
window.clearInterval(id);
const fn = this._intervals.get(id);
if (fn) {
try { fn.destroy(); } catch (_) {}
this._intervals.delete(id);
if (fn) {
try { fn.destroy(); } catch (_) {}
this._intervals.delete(id);
}
}
clipboard_write(text) {
Expand Down
119 changes: 84 additions & 35 deletions dist/pytml.min.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
(function (global) {
'use strict';
const VERSION = '2.5.1';
const PYODIDE_VERSION = '0.314.0.7';
const PYODIDE_VERSION = '314.0.7';
const PYODIDE_INDEX = `https://cdn.jsdelivr.net/pyodide/v${PYODIDE_VERSION}/full/`;
const PYODIDE_SCRIPT = `${PYODIDE_INDEX}pyodide.js`;
// file:// is supported for inline Python. Pyodide itself stays on the HTTPS CDN.
const IS_FILE_PROTOCOL = typeof location !== 'undefined' && location.protocol === 'file:';
const PY_BLOCK = /^py(?:\d+)?$/i;
// --- Speed: start the CDN connection and script download immediately, before DOMContentLoaded ---
// --- Network helpers. Pyodide is loaded only when a Python block is present. ---
let _pyodideScriptPromise = null;
function preconnect() {
for (const [rel, href] of [
Expand All @@ -18,13 +20,6 @@ link.href = href;
if (rel === 'preconnect') link.crossOrigin = 'anonymous';
document.head.appendChild(link);
}
// Preload the 6MB wasm so it downloads in parallel with the JS bootstrap.
const wasm = document.createElement('link');
wasm.rel = 'preload';
wasm.href = `${PYODIDE_INDEX}pyodide.asm.wasm`;
wasm.as = 'fetch';
wasm.crossOrigin = 'anonymous';
document.head.appendChild(wasm);
}
function preloadPyodideScript() {
if (_pyodideScriptPromise) return _pyodideScriptPromise;
Expand Down Expand Up @@ -113,10 +108,13 @@ el.classList.add('pytml-boot-error');
el.innerHTML = `<span>${message || 'Python failed to load.'}</span>`;
});
}
// Fire immediately — these run before DOMContentLoaded.
preconnect();
installBootState();
preloadPyodideScript();
function pageHasPythonBlocks() {
if (document.querySelector('script[type="text/python"]')) return true;
return Array.from(document.querySelectorAll('*')).some((el) => {
const tag = el.tagName?.toLowerCase?.() || '';
return PY_BLOCK.test(tag);
});
}
const DEFAULT_STYLE = `
.pytml-output{box-sizing:border-box;background:#0a0e27;color:#e7e9ee;border:1px solid rgba(102,126,234,.28);border-radius:14px;padding:16px;margin:16px 0;font:14px/1.5 ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;max-height:420px;overflow:auto}
.pytml-line{margin:4px 0;white-space:pre-wrap;overflow-wrap:anywhere}
Expand All @@ -132,7 +130,9 @@ const DEFAULT_STYLE = `
// This is the Python-side DOM/runtime module injected into Pyodide at startup.
const PYTHON_MODULE = String.raw`
import js
import asyncio
from pyodide.ffi import create_proxy
import html as _html_escape
_callbacks = []
class Event(dict):
"""Dictionary-like browser event with default-control helpers."""
Expand Down Expand Up @@ -241,7 +241,10 @@ def on(self, event, callback, options=None):
def handler(raw):
try:
normalized = js.pytmlInstance.api_event(raw).to_py()
return callback(Event(normalized, raw))
result = callback(Event(normalized, raw))
if asyncio.iscoroutine(result):
asyncio.ensure_future(result)
return None
except Exception:
import traceback
js.pytmlInstance.addError(traceback.format_exc())
Expand Down Expand Up @@ -296,8 +299,12 @@ def query(selector): return element(selector)
def query_all(selector): return [Element(x) for x in js.document.querySelectorAll(str(selector))]
def joystick(target): return Joystick(js.pytmlInstance.makeJoystick(target).element)
async def sleep(ms): await js.pytmlInstance.sleep(int(ms))
def timeout(callback, ms): return js.pytmlInstance.setTimeout(callback, int(ms))
def interval(callback, ms): return js.pytmlInstance.setInterval(callback, int(ms))
def timeout(callback, ms):
from pyodide.ffi import create_proxy
return js.pytmlInstance.setTimeout(create_proxy(callback), int(ms))
def interval(callback, ms):
from pyodide.ffi import create_proxy
return js.pytmlInstance.setInterval(create_proxy(callback), int(ms))
def clear_timeout(timer_id): return js.pytmlInstance.clearTimeout(int(timer_id))
def clear_interval(timer_id): return js.pytmlInstance.clearInterval(int(timer_id))
async def clipboard_copy(text): await js.pytmlInstance.clipboard_write(str(text))
Expand All @@ -309,11 +316,14 @@ def gamepads(): return js.pytmlInstance.gamepads()
def storage_get(key, fallback=None): return js.pytmlInstance.storage_get(key, fallback)
def storage_set(key, value): return js.pytmlInstance.storage_set(key, value)
def storage_remove(key): return js.pytmlInstance.storage_remove(key)
def escape(text):
"""Safely escape HTML characters (<, >, &) from user input."""
return _html_escape.escape(str(text))
def install(requirements, **kwargs):
import micropip
return micropip.install(requirements, **kwargs)
version = '2.5.0'
pyodide_version = '0.314.0.7'
version = '2.5.1'
pyodide_version = '314.0.7'
def _bind_all_dom(namespace):
elements = {}
for obj in js.document.querySelectorAll('[id]'):
Expand Down Expand Up @@ -412,24 +422,30 @@ this.ready = false;
this.readyPromise = null;
this.programs = new Map();
this._callbackRegistry = new Map();
this._intervals = new Set();
this._timeouts = new Set();
this._intervals = new Map();
this._timeouts = new Map();
this._joystickState = new WeakMap();
}
async start() {
if (!this.readyPromise) this.readyPromise = this._start();
return this.readyPromise;
}
async _start() {
preconnect();
installBootState();
this.installStyles();
// Always use the remote Pyodide CDN from file:// pages. This avoids
// trying to resolve Pyodide assets relative to a local file origin.
const indexURL = IS_FILE_PROTOCOL
? PYODIDE_INDEX
: this.options.pyodideIndexURL;
await this.loadPyodideScript();
this.pyodide = await global.loadPyodide({ indexURL: this.options.pyodideIndexURL });
this.pyodide = await global.loadPyodide({ indexURL });
global.pyodide = this.pyodide;
global.pytmlInstance = this;
global.pytml = this;
await this.installPythonModule();
await this.setupPythonEnvironment();
await this.pyodide.loadPackage('micropip');
this.ready = true;
await this.runAllPythonScripts();
clearBootState();
Expand Down Expand Up @@ -594,8 +610,15 @@ return result;
async runAllPythonScripts() {
for (const block of this.findPythonBlocks()) {
if (block.src) {
const response = await fetch(block.src, { credentials: 'same-origin' });
if (!response.ok) throw new Error(`HTTP ${response.status} while loading ${block.src}`);
const resolved = new URL(block.src, document.baseURI);
if (location.protocol === 'file:' && resolved.protocol === 'file:') {
throw new Error(`Pytml cannot read a separate local Python file ("${block.src}") from file:// because browsers block local-file fetches. Use inline <py> / <pyN> code or an HTTPS src.`);
}
const response = await fetch(resolved.href, {
credentials: IS_FILE_PROTOCOL ? 'omit' : 'same-origin',
mode: 'cors'
});
if (!response.ok) throw new Error(`HTTP ${response.status} while loading ${resolved.href}`);
await this.executePythonCode(await response.text(), block.id);
} else if (block.code.trim()) {
await this.executePythonCode(block.code, block.id);
Expand Down Expand Up @@ -701,7 +724,7 @@ line.textContent = String(text);
this.outputContainer.appendChild(line);
this.outputContainer.scrollTop = this.outputContainer.scrollHeight;
}
addError(text) { this.addOutput(`❌ ${text}`, true); }
addError(text) { this.addOutput(`X ${text}`, true); }
showStatus(message, isError=false) {
if (!this.statusElement) { this.statusElement=document.createElement('div'); document.body.appendChild(this.statusElement); }
this.statusElement.className = `pytml-status${isError ? ' error' : ''}`;
Expand All @@ -710,17 +733,35 @@ this.statusElement.style.display = 'block';
}
hideStatus() { if (this.statusElement) { this.statusElement.remove(); this.statusElement=null; } }
setTimeout(fn, ms) {
const proxy = this.pyodide.ffi.create_proxy(() => { try { fn(); } catch (e) { this.addError(`Timer error: ${e.message}`); } });
const id = window.setTimeout(() => { try { proxy(); } finally { try { proxy.destroy(); } catch (_) {} } }, Number(ms)||0);
this._timeouts.add(id); return id;
const id = window.setTimeout(() => {
try { fn(); } catch (e) { this.addError(`Timer error: ${e.message}`); }
}, Number(ms)||0);
this._timeouts.set(id, fn);
return id;
}
setInterval(fn, ms) {
const proxy = this.pyodide.ffi.create_proxy(() => { try { fn(); } catch (e) { this.addError(`Timer error: ${e.message}`); } });
const id = window.setInterval(proxy, Number(ms)||0);
this._intervals.add({ id, proxy }); return id;
const id = window.setInterval(() => {
try { fn(); } catch (e) { this.addError(`Timer error: ${e.message}`); }
}, Number(ms)||0);
this._intervals.set(id, fn);
return id;
}
clearTimeout(id) {
window.clearTimeout(id);
const fn = this._timeouts.get(id);
if (fn) {
try { fn.destroy(); } catch (_) {}
this._timeouts.delete(id);
}
}
clearInterval(id) {
window.clearInterval(id);
const fn = this._intervals.get(id);
if (fn) {
try { fn.destroy(); } catch (_) {}
this._intervals.delete(id);
}
}
clearTimeout(id) { window.clearTimeout(id); this._timeouts.delete(id); }
clearInterval(id) { window.clearInterval(id); for (const item of [...this._intervals]) if (item.id === id) { try { item.proxy.destroy(); } catch (_) {} this._intervals.delete(item); } }
clipboard_write(text) {
if (!navigator.clipboard?.writeText) return Promise.reject(new Error('Clipboard API unavailable'));
return navigator.clipboard.writeText(String(text));
Expand Down Expand Up @@ -775,16 +816,24 @@ gamepads() { return [...(navigator.getGamepads?.()||[])].filter(Boolean).map(g=>
let singleton=null;
function start(options={}) { if(!singleton) singleton=new Pytml(options); return singleton.start(); }
async function boot() {
const run = async () => {
if (!pageHasPythonBlocks()) return;
try {
await start();
} catch (err) {
console.error('[PYTML] boot failed:', err);
failBootState('Python failed to load. Refresh to retry.');
failBootState(err?.message || 'Python failed to load. Refresh to retry.');
}
};
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', run, { once: true });
} else {
run();
}
}
installEarlyPythonHide();
global.Pytml=Pytml;
global.PytmlStart=start;
global.pytml=global.pytml||{start};
boot();
})(typeof window!=='undefined'?window:globalThis);
})(typeof window!=='undefined'?window:globalThis);
Loading
Loading