mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-23 04:03:00 -05:00
eliminate py-env altogether (#775)
* examples use toml now * use 0.21.2 for now * change config in docs * fix pre-commit
This commit is contained in:
@@ -1,82 +0,0 @@
|
||||
import * as jsyaml from 'js-yaml';
|
||||
|
||||
import { runtimeLoaded, addInitializer } from '../stores';
|
||||
import { handleFetchError } from '../utils';
|
||||
import type { Runtime } from '../runtime';
|
||||
import { getLogger } from '../logger';
|
||||
|
||||
const logger = getLogger('py-env');
|
||||
|
||||
// Premise used to connect to the first available runtime (can be pyodide or others)
|
||||
let runtime: Runtime;
|
||||
|
||||
runtimeLoaded.subscribe(value => {
|
||||
runtime = value;
|
||||
});
|
||||
|
||||
export class PyEnv extends HTMLElement {
|
||||
shadow: ShadowRoot;
|
||||
wrapper: HTMLElement;
|
||||
code: string;
|
||||
environment: unknown;
|
||||
runtime: Runtime;
|
||||
env: string[];
|
||||
paths: string[];
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.shadow = this.attachShadow({ mode: 'open' });
|
||||
this.wrapper = document.createElement('slot');
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
logger.info("The <py-env> tag is deprecated, please use <py-config> instead.")
|
||||
this.code = this.innerHTML;
|
||||
this.innerHTML = '';
|
||||
|
||||
const env: string[] = [];
|
||||
const paths: string[] = [];
|
||||
|
||||
this.environment = jsyaml.load(this.code);
|
||||
if (this.environment === undefined) return;
|
||||
|
||||
for (const entry of Array.isArray(this.environment) ? this.environment : []) {
|
||||
if (typeof entry == 'string') {
|
||||
env.push(entry);
|
||||
} else if (entry && typeof entry === 'object') {
|
||||
const obj = <Record<string, unknown>>entry;
|
||||
for (const path of Array.isArray(obj.paths) ? obj.paths : []) {
|
||||
if (typeof path === 'string') {
|
||||
paths.push(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.env = env;
|
||||
this.paths = paths;
|
||||
|
||||
async function loadEnv() {
|
||||
logger.info("Loading env: ", env);
|
||||
await runtime.installPackage(env);
|
||||
}
|
||||
|
||||
async function loadPaths() {
|
||||
logger.info("Paths to load: ", paths)
|
||||
for (const singleFile of paths) {
|
||||
logger.info(` loading path: ${singleFile}`);
|
||||
try {
|
||||
await runtime.loadFromFile(singleFile);
|
||||
} catch (e) {
|
||||
//Should we still export full error contents to console?
|
||||
handleFetchError(<Error>e, singleFile);
|
||||
}
|
||||
}
|
||||
logger.info("All paths loaded");
|
||||
}
|
||||
|
||||
addInitializer(loadEnv);
|
||||
addInitializer(loadPaths);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import { loadConfigFromElement } from './pyconfig';
|
||||
import type { AppConfig } from './pyconfig';
|
||||
import type { Runtime } from './runtime';
|
||||
import { PyScript } from './components/pyscript';
|
||||
import { PyEnv } from './components/pyenv';
|
||||
import { PyLoader } from './components/pyloader';
|
||||
import { PyodideRuntime } from './pyodide';
|
||||
import { getLogger } from './logger';
|
||||
@@ -31,7 +30,6 @@ class PyScriptApp {
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
const xPyScript = customElements.define('py-script', PyScript);
|
||||
const xPyLoader = customElements.define('py-loader', PyLoader);
|
||||
const xPyEnv = customElements.define('py-env', PyEnv);
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
|
||||
// add loader to the page body
|
||||
|
||||
@@ -99,7 +99,7 @@ export class PyodideRuntime extends Runtime {
|
||||
try:
|
||||
response = await pyfetch("${path}")
|
||||
except Exception as err:
|
||||
console.warn("PyScript: Access to local files (using 'paths:' in py-env) is not available when directly opening a HTML file; you must use a webserver to serve the additional files. See https://github.com/pyscript/pyscript/issues/257#issuecomment-1119595062 on starting a simple webserver with Python.")
|
||||
console.warn("PyScript: Access to local files (using 'paths:' in py-config) is not available when directly opening a HTML file; you must use a webserver to serve the additional files. See https://github.com/pyscript/pyscript/issues/257#issuecomment-1119595062 on starting a simple webserver with Python.")
|
||||
raise(err)
|
||||
content = await response.bytes()
|
||||
with open("${filename}", "wb") as f:
|
||||
|
||||
@@ -68,7 +68,7 @@ function handleFetchError(e: Error, singleFile: string) {
|
||||
let errorContent: string;
|
||||
if (e.message.includes('TypeError: Failed to fetch')) {
|
||||
errorContent = `<p>PyScript: Access to local files
|
||||
(using "Paths:" in <py-env>)
|
||||
(using "Paths:" in <py-config>)
|
||||
is not available when directly opening a HTML file;
|
||||
you must use a webserver to serve the additional files.
|
||||
See <a style="text-decoration: underline;" href="https://github.com/pyscript/pyscript/issues/257#issuecomment-1119595062">this reference</a>
|
||||
|
||||
Reference in New Issue
Block a user