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:
Madhur Tandon
2022-10-04 19:00:39 +05:30
committed by GitHub
parent a4f97e6e46
commit c70e121078
27 changed files with 193 additions and 387 deletions

View File

@@ -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);
}
}

View File

@@ -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

View File

@@ -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:

View File

@@ -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 &lt;py-env&gt;)
(using "Paths:" in &lt;py-config&gt;)
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>