-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Refactor py-config and the general initialization logic of the page #806
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
77be53a
a1124da
894a1c7
4bc4029
5798923
1faeb3b
6479860
540b129
62ddb90
634be61
6ca9be5
798c436
ef79c64
257c1e9
07a4548
4789705
46c8aab
c6123f3
2a185af
c51621a
2964124
fe702ce
35a8201
d4a3138
6ee7794
e2ae490
b9e77ae
8f8b4b1
4b37b8b
c0577d6
c65a8ba
0dca198
b7d4b04
7239e2d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,104 @@ | ||
| import './styles/pyscript_base.css'; | ||
|
|
||
| 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 { PyConfig } from './components/pyconfig'; | ||
| import { PyodideRuntime } from './pyodide'; | ||
| import { getLogger } from './logger'; | ||
| import { globalLoader } from './stores'; | ||
| import { globalLoader, runtimeLoaded, addInitializer } from './stores'; | ||
| import { handleFetchError, globalExport } from './utils' | ||
|
|
||
| const logger = getLogger('pyscript/main'); | ||
|
|
||
| /* eslint-disable @typescript-eslint/no-unused-vars */ | ||
| const xPyScript = customElements.define('py-script', PyScript); | ||
| const xPyLoader = customElements.define('py-loader', PyLoader); | ||
| const xPyConfig = customElements.define('py-config', PyConfig); | ||
| const xPyEnv = customElements.define('py-env', PyEnv); | ||
| /* eslint-disable @typescript-eslint/no-unused-vars */ | ||
|
|
||
| // As first thing, loop for application configs | ||
| logger.info('checking for py-config'); | ||
| const config: PyConfig = document.querySelector('py-config'); | ||
| if (!config) { | ||
| const loader = document.createElement('py-config'); | ||
| document.body.append(loader); | ||
| // XXX this should be killed eventually | ||
| let runtimeSpec: Runtime; | ||
| runtimeLoaded.subscribe(value => { | ||
| runtimeSpec = value; | ||
| }); | ||
|
|
||
|
|
||
| class PyScriptApp { | ||
|
|
||
| config: AppConfig; | ||
|
|
||
| main() { | ||
| this.loadConfig(); | ||
| this.initialize(); | ||
|
|
||
| /* 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 | ||
| logger.info('add py-loader'); | ||
| const loader = <PyLoader>document.createElement('py-loader'); | ||
| document.body.append(loader); | ||
| globalLoader.set(loader); | ||
| } | ||
|
|
||
| loadConfig() { | ||
| // find the <py-config> tag. If not found, we get null which means | ||
| // "use the default config" | ||
| // XXX: we should actively complain if there are multiple <py-config> | ||
| // and show a big error. PRs welcome :) | ||
| logger.info('searching for <py-config>'); | ||
| const el = document.querySelector('py-config'); | ||
| this.config = loadConfigFromElement(el); | ||
| logger.info('config loaded:\n' + JSON.stringify(this.config, null, 2)); | ||
| } | ||
|
|
||
| initialize() { | ||
| addInitializer(this.loadPackages); | ||
| addInitializer(this.loadPaths); | ||
| this.loadRuntimes(); | ||
| } | ||
|
|
||
| loadPackages = async () => { | ||
| logger.info("Packages to install: ", this.config.packages); | ||
| await runtimeSpec.installPackage(this.config.packages); | ||
antocuni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| loadPaths = async () => { | ||
| const paths = this.config.paths; | ||
| logger.info("Paths to load: ", paths) | ||
| for (const singleFile of paths) { | ||
| logger.info(` loading path: ${singleFile}`); | ||
| try { | ||
| await runtimeSpec.loadFromFile(singleFile); | ||
| } catch (e) { | ||
| //Should we still export full error contents to console? | ||
| handleFetchError(<Error>e, singleFile); | ||
| } | ||
| } | ||
| logger.info("All paths loaded"); | ||
| } | ||
|
|
||
| loadRuntimes() { | ||
| logger.info('Initializing runtimes'); | ||
| for (const runtime of this.config.runtimes) { | ||
| const runtimeObj: Runtime = new PyodideRuntime(this.config, runtime.src, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll need to figure out the multiple-interpreters story for this... e.g. defining what interpreter to use and then how to instantiate it.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. Personally, I would like to change the config to allow only one runtime instead of having a list-which-is-not-really-a-list-because-it-doesnt-work-if-you-have-more-than-one.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. KISS (keep it simple, stupid)... totally agree. Also, if you need more than one runtime, you're probably doing it wrong. |
||
| runtime.name, runtime.lang); | ||
| const script = document.createElement('script'); // create a script DOM node | ||
| script.src = runtimeObj.src; // set its src to the provided URL | ||
| script.addEventListener('load', () => { | ||
| void runtimeObj.initialize(); | ||
| }); | ||
| document.head.appendChild(script); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| function pyscript_get_config() { | ||
| return globalApp.config; | ||
| } | ||
| globalExport('pyscript_get_config', pyscript_get_config); | ||
|
|
||
| // add loader to the page body | ||
| logger.info('add py-loader'); | ||
| const loader = <PyLoader>document.createElement('py-loader'); | ||
| document.body.append(loader); | ||
| globalLoader.set(loader); | ||
| // main entry point of execution | ||
| const globalApp = new PyScriptApp(); | ||
| globalApp.main(); | ||
Uh oh!
There was an error while loading. Please reload this page.