8000 Refactor py-config and the general initialization logic of the page by antocuni · Pull Request #806 · pyscript/pyscript · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
77be53a
start to remove module-level statements, and put all the logic in its…
antocuni Sep 29, 2022
a1124da
add a JS API to get the pyscript config and two integration tests to …
antocuni Sep 29, 2022
894a1c7
rename this file
antocuni Sep 29, 2022
4bc4029
introduce the class PyScriptApp, so have a global singleton where to …
antocuni Sep 29, 2022
5798923
small refactoring of pyconfig.ts, in preparation to make py-config NO…
antocuni Sep 29, 2022
1faeb3b
improve logging
antocuni Sep 29, 2022
6479860
start moving all the config logic to config.ts
antocuni Sep 29, 2022
540b129
move all the config logic from utils.ts into config.ts
antocuni Sep 29, 2022
62ddb90
WIP: start to refactor pyconfig.test.ts to test directly loadConfigFr…
antocuni Sep 29, 2022
634be61
WIP: allow to call loadConfigFromElement(null)
antocuni Sep 30, 2022
6ca9be5
WIP: more progress in porting tests, create a brand new element inste…
antocuni Sep 30, 2022
798c436
fix typos :)
antocuni Sep 30, 2022
ef79c64
WIP: one more test
antocuni Sep 30, 2022
257c1e9
WIP: more tests
antocuni Sep 30, 2022
07a4548
WIP: more tests
antocuni Sep 30, 2022
4789705
move the last tests to PyConfig
antocuni Sep 30, 2022
46c8aab
another small step: PyConfig is no longer a web component but it's ma…
antocuni Sep 30, 2022
c6123f3
Merge branch 'main' of github.com:pyscript/pyscript into main
antocuni Sep 30, 2022
2a185af
fix tests which were broken after the merge
antocuni Sep 30, 2022
c51621a
use JSON.stringify to get a human-readable version of the config
antocuni Sep 30, 2022
2964124
add an integration test for <py-config>paths=...
antocuni Sep 30, 2022
fe702ce
add a test for <py-config>packages...
antocuni Sep 30, 2022
35a8201
move the loadPackages logic from pyconfig into main.ts
antocuni Sep 30, 2022
d4a3138
move the loadPaths logic from pyconfig into main.ts
antocuni Sep 30, 2022
6ee7794
move the loadRuntimes() logic into main.ts
antocuni Sep 30, 2022
e2ae490
kill all the remaining references to PyConfig
antocuni Sep 30, 2022
b9e77ae
finally kill pyconfig.ts
antocuni Sep 30, 2022
8f8b4b1
now that the old pyconfig.ts has gone, we can reuse its name
antocuni Sep 30, 2022
4b37b8b
wooo, kill the appConfig store: instead of using a global variable to…
antocuni Sep 30, 2022
c0577d6
Update pyscriptjs/src/pyconfig.ts
antocuni Oct 3, 2022
c65a8ba
improve this comment
antocuni Oct 3, 2022
0dca198
try to use an older version of eslint
antocuni Oct 3, 2022
b7d4b04
Merge remote-tracking branch 'origin/main' into antocuni/refactor-py-…
antocuni Oct 4, 2022
7239e2d
use expect().toThrow instead of it.failing()
antocuni Oct 4, 2022
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ repos:
- --py310-plus

- repo: https://github.com/pre-commit/mirrors-eslint
rev: v8.24.0
rev: v8.23.1
hooks:
- id: eslint
files: pyscriptjs/src/.*\.[jt]sx?$ # *.js, *.jsx, *.ts and *.tsx
Expand Down
130 changes: 0 additions & 130 deletions pyscriptjs/src/components/pyconfig.ts

This file was deleted.

113 changes: 93 additions & 20 deletions pyscriptjs/src/main.ts
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);
}

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,
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
But as I said above, in this PR I tried to move around the logic without actually trying to change it too much.

Copy link
Member
@ntoll ntoll Oct 4, 2022

Choose a reason for hiding this comment

The 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();
Loading
0