mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-23 12:12:59 -05:00
Refactor how py-script are executed, kill scriptQueue store, introduce pyExec (#881)
Yet another refactoring to untangle the old mess. Highlights: base.ts, pyscript.ts and pyrepl.ts were a tangled mess of code, in which each of them interacted with the others in non-obvious ways. Now PyScript is no longer a subclass of BaseEvalElement and it is much simpler. I removed code for handling the attributes std-out and std-err because they are no longer needed with the new display() logic. The logic for executing python code is now in pyexec.ts: so we are decoupling the process of "finding" the python code (handled by the py-script web component) and the logic to actually execute it. This has many advantages, including the fact that it will be more easily usable by other components (e.g. pyrepl). Also, note that it's called pyexec and not pyeval: in the vast majority of cases in Python you have statements to execute, and almost never expressions to evaluate. I killed the last remaining global store, scriptQueue tada. As a bonus effect, now we automatically do the correct thing when a <py-script> tag is dynamically added to the DOM (I added a test for it). I did not remove svelte from packages.json, because I don't fully understand the implications: there are various options which mention svelte in rollup.js and tsconfig.json, so it's probably better to kill it in its own PR. pyexec.ts is also responsible of handling the default target for display() and correct handling/visualization of exceptions. I fixed/improved/added display/output tests in the process. I also found a problem though, see issue #878, so I improved the test and marked it as xfail. I removed BaseEvalElement as the superclass of most components. Now the only class which inherits from it is PyRepl. In a follow-up PR, I plan to merge them into a single class and do more cleanup. During the refactoring, I killed guidGenerator: now instead of generating random py-* IDs which are very hard to read for humans, we generated py-internal-X IDs, where X is 0, 1, 2, 3, etc. This makes writing tests and debugging much easier. I improved a lot our test machinery: it turns out that PR #829 broke the ability to use/view sourcemaps inside the playwright browser (at least on my machine). For some reason chromium is unable to find sourcemaps if you use playwrights internal routing. So I reintroduced the http_server fixture which was removed by that PR, and added a pytest option --no-fake-server to use it instead, useful for debugging. By default we are still using the fakeserver though (which is faster and parallelizable). Similarly, I added --dev which implies --headed and also automatically open chrome dev tools.
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
import { getAttribute, guidGenerator, addClasses, removeClasses } from '../utils';
|
||||
// XXX this should be eventually killed.
|
||||
// The only remaining class which inherit from BaseEvalElement is PyRepl: we
|
||||
// should merge the two classes together, do a refactoing of how PyRepl to use
|
||||
// the new pyExec and in general clean up the unnecessary code.
|
||||
|
||||
import { ensureUniqueId, addClasses, removeClasses, getAttribute } from '../utils';
|
||||
import type { Runtime } from '../runtime';
|
||||
import { getLogger } from '../logger';
|
||||
import { pyExecDontHandleErrors } from '../pyexec';
|
||||
|
||||
const logger = getLogger('pyscript/base');
|
||||
|
||||
@@ -28,11 +34,6 @@ export class BaseEvalElement extends HTMLElement {
|
||||
this.setOutputMode("append");
|
||||
}
|
||||
|
||||
addToOutput(s: string) {
|
||||
this.outputElement.innerHTML += '<div>' + s + '</div>';
|
||||
this.outputElement.hidden = false;
|
||||
}
|
||||
|
||||
setOutputMode(defaultMode = "append") {
|
||||
const mode = getAttribute(this,'output-mode') || defaultMode;
|
||||
|
||||
@@ -61,7 +62,7 @@ export class BaseEvalElement extends HTMLElement {
|
||||
}
|
||||
|
||||
checkId() {
|
||||
if (!this.id) this.id = 'py-' + guidGenerator();
|
||||
ensureUniqueId(this);
|
||||
}
|
||||
|
||||
getSourceFromElement(): string {
|
||||
@@ -74,39 +75,6 @@ export class BaseEvalElement extends HTMLElement {
|
||||
return this.code;
|
||||
}
|
||||
|
||||
protected async _register_esm(runtime: Runtime): Promise<void> {
|
||||
const imports: { [key: string]: unknown } = {};
|
||||
const nodes = document.querySelectorAll("script[type='importmap']");
|
||||
const importmaps: any[] = [];
|
||||
nodes.forEach( node =>
|
||||
{
|
||||
let importmap;
|
||||
try {
|
||||
importmap = JSON.parse(node.textContent);
|
||||
if (importmap?.imports == null) return;
|
||||
importmaps.push(importmap);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
}
|
||||
)
|
||||
for (const importmap of importmaps){
|
||||
for (const [name, url] of Object.entries(importmap.imports)) {
|
||||
if (typeof name != 'string' || typeof url != 'string') continue;
|
||||
|
||||
try {
|
||||
// XXX: pyodide doesn't like Module(), failing with
|
||||
// "can't read 'name' of undefined" at import time
|
||||
imports[name] = { ...(await import(url)) };
|
||||
} catch {
|
||||
logger.error(`failed to fetch '${url}' for '${name}'`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
runtime.registerJsModule('esm', imports);
|
||||
}
|
||||
|
||||
async evaluate(runtime: Runtime): Promise<void> {
|
||||
this.preEvaluate();
|
||||
|
||||
@@ -114,14 +82,9 @@ export class BaseEvalElement extends HTMLElement {
|
||||
try {
|
||||
source = this.source ? await this.getSourceFromFile(this.source)
|
||||
: this.getSourceFromElement();
|
||||
this._register_esm(runtime);
|
||||
|
||||
try {
|
||||
<string>await runtime.run(`set_current_display_target(target_id="${this.id}")`);
|
||||
<string>await runtime.run(source);
|
||||
} finally {
|
||||
<string>await runtime.run(`set_current_display_target(target_id=None)`);
|
||||
}
|
||||
// XXX we should use pyExec and let it display the errors
|
||||
await pyExecDontHandleErrors(runtime, source, this);
|
||||
|
||||
removeClasses(this.errorElement, ['py-error']);
|
||||
this.postEvaluate();
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
import { BaseEvalElement } from './base';
|
||||
import { getAttribute, addClasses, htmlDecode } from '../utils';
|
||||
import { getAttribute, addClasses, htmlDecode, ensureUniqueId } from '../utils';
|
||||
import { getLogger } from '../logger'
|
||||
import type { Runtime } from '../runtime';
|
||||
|
||||
const logger = getLogger('py-button');
|
||||
|
||||
export function make_PyButton(runtime: Runtime) {
|
||||
class PyButton extends BaseEvalElement {
|
||||
class PyButton extends HTMLElement {
|
||||
widths: string[] = [];
|
||||
label: string | undefined = undefined;
|
||||
class: string[];
|
||||
defaultClass: string[];
|
||||
mount_name: string | undefined = undefined;
|
||||
code: string;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
@@ -41,7 +42,7 @@ export function make_PyButton(runtime: Runtime) {
|
||||
}
|
||||
|
||||
async connectedCallback() {
|
||||
this.checkId();
|
||||
ensureUniqueId(this);
|
||||
this.code = htmlDecode(this.innerHTML) || "";
|
||||
this.mount_name = this.id.split('-').join('_');
|
||||
this.innerHTML = '';
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
import { BaseEvalElement } from './base';
|
||||
import { getAttribute, addClasses, htmlDecode } from '../utils';
|
||||
import { getAttribute, addClasses, htmlDecode, ensureUniqueId } from '../utils';
|
||||
import { getLogger } from '../logger'
|
||||
import type { Runtime } from '../runtime';
|
||||
|
||||
const logger = getLogger('py-inputbox');
|
||||
|
||||
export function make_PyInputBox(runtime: Runtime) {
|
||||
class PyInputBox extends BaseEvalElement {
|
||||
class PyInputBox extends HTMLElement {
|
||||
widths: string[] = [];
|
||||
label: string | undefined = undefined;
|
||||
mount_name: string | undefined = undefined;
|
||||
code: string;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
@@ -20,7 +21,7 @@ export function make_PyInputBox(runtime: Runtime) {
|
||||
}
|
||||
|
||||
async connectedCallback() {
|
||||
this.checkId();
|
||||
ensureUniqueId(this);
|
||||
this.code = htmlDecode(this.innerHTML);
|
||||
this.mount_name = this.id.split('-').join('_');
|
||||
this.innerHTML = '';
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import { BaseEvalElement } from './base';
|
||||
import { getLogger } from '../logger';
|
||||
|
||||
const logger = getLogger('py-loader');
|
||||
|
||||
export class PyLoader extends BaseEvalElement {
|
||||
export class PyLoader extends HTMLElement {
|
||||
widths: string[];
|
||||
label: string;
|
||||
mount_name: string;
|
||||
|
||||
@@ -132,36 +132,16 @@ export function make_PyRepl(runtime: Runtime) {
|
||||
this.outputElement = el
|
||||
}
|
||||
} else {
|
||||
const stdOut = getAttribute(this, "std-out");
|
||||
if (stdOut) {
|
||||
const el = document.getElementById(stdOut);
|
||||
if(el){
|
||||
this.outputElement = el
|
||||
}
|
||||
} else {
|
||||
// In this case neither output or std-out have been provided so we need
|
||||
// to create a new output div to output to
|
||||
this.outputElement = document.createElement('div');
|
||||
this.outputElement.classList.add('output');
|
||||
this.outputElement.hidden = true;
|
||||
const stdOut = getAttribute(this, "exec-id") || "";
|
||||
this.outputElement.id = this.id + '-' + stdOut;
|
||||
// to create a new output div to output to
|
||||
this.outputElement = document.createElement('div');
|
||||
this.outputElement.classList.add('output');
|
||||
this.outputElement.hidden = true;
|
||||
this.outputElement.id = this.id + '-' + this.getAttribute('exec-id');
|
||||
|
||||
// add the output div id if there's not output pre-defined
|
||||
mainDiv.appendChild(this.outputElement);
|
||||
}
|
||||
// add the output div id if there's not output pre-defined
|
||||
mainDiv.appendChild(this.outputElement);
|
||||
|
||||
const stdErr = getAttribute(this, "std-err");
|
||||
if( stdErr ){
|
||||
const el = document.getElementById(stdErr);
|
||||
if(el){
|
||||
this.errorElement = el;
|
||||
}else{
|
||||
this.errorElement = this.outputElement
|
||||
}
|
||||
}else{
|
||||
this.errorElement = this.outputElement
|
||||
}
|
||||
this.errorElement = this.outputElement;
|
||||
}
|
||||
|
||||
this.appendChild(mainDiv);
|
||||
@@ -169,11 +149,6 @@ export function make_PyRepl(runtime: Runtime) {
|
||||
logger.debug(`element ${this.id} successfully connected`);
|
||||
}
|
||||
|
||||
addToOutput(s: string): void {
|
||||
this.outputElement.innerHTML += '<div>' + s + '</div>';
|
||||
this.outputElement.hidden = false;
|
||||
}
|
||||
|
||||
preEvaluate(): void {
|
||||
this.setOutputMode("replace");
|
||||
if(!this.appendOutput) {
|
||||
@@ -213,8 +188,6 @@ export function make_PyRepl(runtime: Runtime) {
|
||||
};
|
||||
|
||||
addReplAttribute('output');
|
||||
addReplAttribute('std-out');
|
||||
addReplAttribute('std-err');
|
||||
|
||||
newPyRepl.setAttribute('exec-id', nextExecId.toString());
|
||||
if( this.parentElement ){
|
||||
|
||||
@@ -1,117 +1,37 @@
|
||||
import {
|
||||
addToScriptsQueue,
|
||||
} from '../stores';
|
||||
|
||||
import { getAttribute, addClasses, htmlDecode } from '../utils';
|
||||
import { BaseEvalElement } from './base';
|
||||
import { htmlDecode, ensureUniqueId } from '../utils';
|
||||
import type { Runtime } from '../runtime';
|
||||
import { getLogger } from '../logger';
|
||||
import { pyExec } from '../pyexec';
|
||||
|
||||
const logger = getLogger('py-script');
|
||||
|
||||
export class PyScript extends BaseEvalElement {
|
||||
constructor() {
|
||||
super();
|
||||
export function make_PyScript(runtime: Runtime) {
|
||||
|
||||
// add an extra div where we can attach the codemirror editor
|
||||
this.shadow.appendChild(this.wrapper);
|
||||
}
|
||||
class PyScript extends HTMLElement {
|
||||
|
||||
connectedCallback() {
|
||||
this.checkId();
|
||||
this.code = htmlDecode(this.innerHTML);
|
||||
this.innerHTML = '';
|
||||
|
||||
const mainDiv = document.createElement('div');
|
||||
addClasses(mainDiv, ['output']);
|
||||
// add Editor to main PyScript div
|
||||
|
||||
const output = getAttribute( this, "output");
|
||||
if (output) {
|
||||
const el = document.getElementById(output);
|
||||
if( el ){
|
||||
this.errorElement = el;
|
||||
this.outputElement = el;
|
||||
}
|
||||
|
||||
// in this case, the default output-mode is append, if hasn't been specified
|
||||
if (!this.hasAttribute('output-mode')) {
|
||||
this.setAttribute('output-mode', 'append');
|
||||
}
|
||||
} else {
|
||||
const stdOut = getAttribute( this, "std-out");
|
||||
if (stdOut) {
|
||||
const el = document.getElementById( stdOut );
|
||||
if( el){
|
||||
this.outputElement = el
|
||||
}
|
||||
} else {
|
||||
// In this case neither output or std-out have been provided so we need
|
||||
// to create a new output div to output to
|
||||
|
||||
// Let's check if we have an id first and create one if not
|
||||
this.outputElement = document.createElement('div');
|
||||
const exec_id = this.getAttribute('exec-id');
|
||||
this.outputElement.id = this.id + (exec_id ? '-' + exec_id : '');
|
||||
|
||||
// add the output div id if there's not output pre-defined
|
||||
mainDiv.appendChild(this.outputElement);
|
||||
}
|
||||
|
||||
const stdErr = getAttribute( this, "std-err");
|
||||
if ( stdErr ) {
|
||||
const el = document.getElementById( stdErr );
|
||||
if( el ){
|
||||
this.errorElement = el;
|
||||
}else{
|
||||
this.errorElement = this.outputElement;
|
||||
}
|
||||
} else {
|
||||
this.errorElement = this.outputElement;
|
||||
}
|
||||
async connectedCallback() {
|
||||
ensureUniqueId(this);
|
||||
const pySrc = await this.getPySrc();
|
||||
this.innerHTML = '';
|
||||
await pyExec(runtime, pySrc, this);
|
||||
}
|
||||
|
||||
this.appendChild(mainDiv);
|
||||
addToScriptsQueue(this);
|
||||
|
||||
if (this.hasAttribute('src')) {
|
||||
this.source = this.getAttribute('src');
|
||||
}
|
||||
}
|
||||
|
||||
protected async _register_esm(runtime: Runtime): Promise<void> {
|
||||
for (const node of document.querySelectorAll("script[type='importmap']")) {
|
||||
const importmap = (() => {
|
||||
try {
|
||||
return JSON.parse(node.textContent);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
})();
|
||||
|
||||
if (importmap?.imports == null) continue;
|
||||
|
||||
for (const [name, url] of Object.entries(importmap.imports)) {
|
||||
if (typeof name != 'string' || typeof url != 'string') continue;
|
||||
|
||||
let exports: object;
|
||||
try {
|
||||
// XXX: pyodide doesn't like Module(), failing with
|
||||
// "can't read 'name' of undefined" at import time
|
||||
exports = { ...(await import(url)) };
|
||||
} catch {
|
||||
logger.warn(`failed to fetch '${url}' for '${name}'`);
|
||||
continue;
|
||||
}
|
||||
|
||||
runtime.registerJsModule(name, exports);
|
||||
async getPySrc(): Promise<string> {
|
||||
if (this.hasAttribute('src')) {
|
||||
// XXX: what happens if the fetch() fails?
|
||||
// We should handle the case correctly, but in my defense
|
||||
// this case was broken also before the refactoring. FIXME!
|
||||
const url = this.getAttribute('src');
|
||||
const response = await fetch(url);
|
||||
return await response.text();
|
||||
}
|
||||
else {
|
||||
return htmlDecode(this.innerHTML);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getSourceFromElement(): string {
|
||||
return htmlDecode(this.code);
|
||||
}
|
||||
return PyScript;
|
||||
}
|
||||
|
||||
/** Defines all possible py-on* and their corresponding event types */
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { BaseEvalElement } from './base';
|
||||
import { addClasses, htmlDecode } from '../utils';
|
||||
import { addClasses, htmlDecode, ensureUniqueId } from '../utils';
|
||||
|
||||
export class PyTitle extends BaseEvalElement {
|
||||
export class PyTitle extends HTMLElement {
|
||||
widths: string[];
|
||||
label: string;
|
||||
mount_name: string;
|
||||
|
||||
@@ -3,23 +3,16 @@ import './styles/pyscript_base.css';
|
||||
import { loadConfigFromElement } from './pyconfig';
|
||||
import type { AppConfig } from './pyconfig';
|
||||
import type { Runtime } from './runtime';
|
||||
import { PyScript, initHandlers, mountElements } from './components/pyscript';
|
||||
import { make_PyScript, initHandlers, mountElements } from './components/pyscript';
|
||||
import { PyLoader } from './components/pyloader';
|
||||
import { PyodideRuntime } from './pyodide';
|
||||
import { getLogger } from './logger';
|
||||
import { scriptsQueue } from './stores';
|
||||
import { handleFetchError, showError, globalExport } from './utils'
|
||||
import { createCustomElements } from './components/elements';
|
||||
|
||||
|
||||
const logger = getLogger('pyscript/main');
|
||||
|
||||
let scriptsQueue_: PyScript[];
|
||||
scriptsQueue.subscribe((value: PyScript[]) => {
|
||||
scriptsQueue_ = value;
|
||||
});
|
||||
|
||||
|
||||
|
||||
/* High-level overview of the lifecycle of a PyScript App:
|
||||
|
||||
@@ -37,7 +30,8 @@ scriptsQueue.subscribe((value: PyScript[]) => {
|
||||
|
||||
6. setup the environment, install packages
|
||||
|
||||
7. run user scripts
|
||||
7. connect the py-script web component. This causes the execution of all the
|
||||
user scripts
|
||||
|
||||
8. initialize the rest of web components such as py-button, py-repl, etc.
|
||||
|
||||
@@ -58,11 +52,11 @@ class PyScriptApp {
|
||||
config: AppConfig;
|
||||
loader: PyLoader;
|
||||
runtime: Runtime;
|
||||
PyScript: any; // XXX would be nice to have a more precise type for the class itself
|
||||
|
||||
// lifecycle (1)
|
||||
main() {
|
||||
this.loadConfig();
|
||||
customElements.define('py-script', PyScript);
|
||||
this.showLoader();
|
||||
this.loadRuntime();
|
||||
}
|
||||
@@ -129,7 +123,6 @@ class PyScriptApp {
|
||||
//
|
||||
// Invariant: this.config and this.loader are set and available.
|
||||
async afterRuntimeLoad(runtime: Runtime): Promise<void> {
|
||||
// XXX what is the JS/TS standard way of doing asserts?
|
||||
console.assert(this.config !== undefined);
|
||||
console.assert(this.loader !== undefined);
|
||||
|
||||
@@ -195,11 +188,53 @@ class PyScriptApp {
|
||||
|
||||
// lifecycle (7)
|
||||
executeScripts(runtime: Runtime) {
|
||||
for (const script of scriptsQueue_) {
|
||||
void script.evaluate(runtime);
|
||||
}
|
||||
scriptsQueue.set([]);
|
||||
this.register_importmap(runtime);
|
||||
this.PyScript = make_PyScript(runtime);
|
||||
customElements.define('py-script', this.PyScript);
|
||||
}
|
||||
|
||||
async register_importmap(runtime: Runtime) {
|
||||
// make importmap ES modules available from python using 'import'.
|
||||
//
|
||||
// XXX: this code can probably be improved because errors are silently
|
||||
// ignored. Moreover at the time of writing we don't really have a test
|
||||
// for it and this functionality is used only by the d3 example. We
|
||||
// might want to rethink the whole approach at some point. E.g., maybe
|
||||
// we should move it to py-config?
|
||||
//
|
||||
// Moreover, it's also wrong because it's async and currently we don't
|
||||
// await the module to be fully registered before executing the code
|
||||
// inside py-script. It's also unclear whether we want to wait or not
|
||||
// (or maybe only wait only if we do an actual 'import'?)
|
||||
for (const node of document.querySelectorAll("script[type='importmap']")) {
|
||||
const importmap = (() => {
|
||||
try {
|
||||
return JSON.parse(node.textContent);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
})();
|
||||
|
||||
if (importmap?.imports == null) continue;
|
||||
|
||||
for (const [name, url] of Object.entries(importmap.imports)) {
|
||||
if (typeof name != 'string' || typeof url != 'string') continue;
|
||||
|
||||
let exports: object;
|
||||
try {
|
||||
// XXX: pyodide doesn't like Module(), failing with
|
||||
// "can't read 'name' of undefined" at import time
|
||||
exports = { ...(await import(url)) };
|
||||
} catch {
|
||||
logger.warn(`failed to fetch '${url}' for '${name}'`);
|
||||
continue;
|
||||
}
|
||||
|
||||
runtime.registerJsModule(name, exports);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function pyscript_get_config() {
|
||||
|
||||
65
pyscriptjs/src/pyexec.ts
Normal file
65
pyscriptjs/src/pyexec.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { getLogger } from './logger';
|
||||
import { ensureUniqueId, addClasses } from './utils';
|
||||
import type { Runtime } from './runtime';
|
||||
|
||||
const logger = getLogger('pyexec');
|
||||
|
||||
export async function pyExec(runtime: Runtime, pysrc: string, outElem: HTMLElement)
|
||||
{
|
||||
// this is the python function defined in pyscript.py
|
||||
const set_current_display_target = runtime.globals.get('set_current_display_target');
|
||||
ensureUniqueId(outElem);
|
||||
set_current_display_target(outElem.id);
|
||||
try {
|
||||
try {
|
||||
await runtime.run(pysrc);
|
||||
}
|
||||
catch (err) {
|
||||
// XXX: currently we display exceptions in the same position as
|
||||
// the output. But we probably need a better way to do that,
|
||||
// e.g. allowing plugins to intercept exceptions and display them
|
||||
// in a configurable way.
|
||||
displayPyException(err, outElem);
|
||||
}
|
||||
}
|
||||
finally {
|
||||
set_current_display_target(undefined);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function displayPyException(err: any, errElem: HTMLElement) {
|
||||
//addClasses(errElem, ['py-error'])
|
||||
const pre = document.createElement('pre');
|
||||
pre.className = "py-error";
|
||||
|
||||
if (err.name === "PythonError") {
|
||||
// err.message contains the python-level traceback (i.e. a string
|
||||
// starting with: "Traceback (most recent call last) ..."
|
||||
logger.error("Python exception:\n" + err.message);
|
||||
pre.innerText = err.message;
|
||||
}
|
||||
else {
|
||||
// this is very likely a normal JS exception. The best we can do is to
|
||||
// display it as is.
|
||||
logger.error("Non-python exception:\n" + err);
|
||||
pre.innerText = err;
|
||||
}
|
||||
errElem.appendChild(pre);
|
||||
}
|
||||
|
||||
|
||||
// XXX this is used by base.ts but should be removed once we complete the refactoring
|
||||
export async function pyExecDontHandleErrors(runtime: Runtime, pysrc: string, out: HTMLElement)
|
||||
{
|
||||
// this is the python function defined in pyscript.py
|
||||
const set_current_display_target = runtime.globals.get('set_current_display_target');
|
||||
ensureUniqueId(out);
|
||||
set_current_display_target(out.id);
|
||||
try {
|
||||
await runtime.run(pysrc);
|
||||
}
|
||||
finally {
|
||||
set_current_display_target(undefined);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
import { writable } from 'svelte/store';
|
||||
import type { PyScript } from './components/pyscript';
|
||||
|
||||
export const scriptsQueue = writable<PyScript[]>([]);
|
||||
|
||||
export const addToScriptsQueue = (script: PyScript) => {
|
||||
scriptsQueue.update(scriptsQueue => [...scriptsQueue, script]);
|
||||
};
|
||||
@@ -38,11 +38,10 @@ export function ltrim(code: string): string {
|
||||
return k != 0 ? lines.map(line => line.substring(k)).join('\n') : code;
|
||||
}
|
||||
|
||||
export function guidGenerator(): string {
|
||||
const S4 = function (): string {
|
||||
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
|
||||
};
|
||||
return S4() + S4() + '-' + S4() + '-' + S4() + '-' + S4() + '-' + S4() + S4() + S4();
|
||||
let _uniqueIdCounter = 0;
|
||||
export function ensureUniqueId(el: HTMLElement) {
|
||||
if (el.id === "")
|
||||
el.id = "py-internal-" + _uniqueIdCounter++;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user