8000 minor changes to make linting happier (#598) · gytdev/pyscript@8e1cd0b · GitHub
[go: up one dir, main page]

Skip to content

Commit 8e1cd0b

Browse files
authored
minor changes to make linting happier (pyscript#598)
* minor changes to make linting happy * remove cast since linter wasn't happy * address PR comments
1 parent 9102768 commit 8e1cd0b

File tree

9 files changed

+35
-71
lines changed

9 files changed

+35
-71
lines changed

examples/repl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<title>REPL</title>
88

99
<link rel="icon" type="image/png" href="favicon.png" />
10-
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript_base.css" />
10+
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
1111
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
1212
</head>
1313

pyscriptjs/src/components/base.ts

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
1-
import { loadedEnvironments, mode, pyodideLoaded, type Environment } from '../stores';
1+
import { pyodideLoaded } from '../stores';
22
import { guidGenerator, addClasses, removeClasses } from '../utils';
33
import type { PyodideInterface } from '../pyodide';
44
// Premise used to connect to the first available pyodide interpreter
5-
let runtime;
6-
let environments: Record<Environment['id'], Environment> = {};
7-
let currentMode;
5+
let runtime: PyodideInterface;
86
let Element;
97

108
pyodideLoaded.subscribe(value => {
119
runtime = value;
1210
});
13-
loadedEnvironments.subscribe(value => {
14-
environments = value;
15-
});
16-
17-
mode.subscribe(value => {
18-
currentMode = value;
19-
});
2011

2112
export class BaseEvalElement extends HTMLElement {
2213
shadow: ShadowRoot;
@@ -120,30 +111,29 @@ export class BaseEvalElement extends HTMLElement {
120111
console.log('evaluate');
121112
this.preEvaluate();
122113

123-
const pyodide = runtime;
124114
let source: string;
125-
let output;
115+
let output: string;
126116
try {
127117
source = this.source ? await this.getSourceFromFile(this.source)
128118
: this.getSourceFromElement();
129119
const is_async = source.includes('asyncio')
130120

131-
await this._register_esm(pyodide);
121+
await this._register_esm(runtime);
132122
if (is_async) {
133-
await pyodide.runPythonAsync(
123+
<string>await runtime.runPythonAsync(
134124
`output_manager.change(out="${this.outputElement.id}", err="${this.errorElement.id}", append=${this.appendOutput ? 'True' : 'False'})`,
135125
);
136-
output = await pyodide.runPythonAsync(source);
126+
output = <string>await runtime.runPythonAsync(source);
137127
} else {
138-
output = pyodide.runPython(
128+
output = <string>runtime.runPython(
139129
`output_manager.change(out="${this.outputElement.id}", err="${this.errorElement.id}", append=${this.appendOutput ? 'True' : 'False'})`,
140130
);
141-
output = pyodide.runPython(source);
131+
output = <string>runtime.runPython(source);
142132
}
143133

144134
if (output !== undefined) {
145135
if (Element === undefined) {
146-
Element = pyodide.globals.get('Element');
136+
Element = <Element>runtime.globals.get('Element');
147137
}
148138
const out = Element(this.outputElement.id);
149139
out.write.callKwargs(output, { append: this.appendOutput });
@@ -152,8 +142,8 @@ export class BaseEvalElement extends HTMLElement {
152142
this.outputElement.style.display = 'block';
153143
}
154144

155-
is_async ? await pyodide.runPythonAsync(`output_manager.revert()`)
156-
: await pyodide.runPython(`output_manager.revert()`);
145+
is_async ? await runtime.runPythonAsync(`output_manager.revert()`)
146+
: await runtime.runPython(`output_manager.revert()`);
157147

158148
// check if this REPL contains errors, delete them and remove error classes
159149
const errorElements = document.querySelectorAll(`div[id^='${this.errorElement.id}'][error]`);
@@ -171,7 +161,7 @@ export class BaseEvalElement extends HTMLElement {
171161
this.postEvaluate();
172162
} catch (err) {
173163
if (Element === undefined) {
174-
Element = pyodide.globals.get('Element');
164+
Element = <Element>runtime.globals.get('Element');
175165
}
176166
const out = Element(this.errorElement.id);
177167

pyscriptjs/src/components/pyconfig.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
import { loadInterpreter } from '../interpreter';
1515
import type { PyLoader } from './pyloader';
1616
import type { PyScript } from './pyscript';
17+
import type { PyodideInterface } from '../pyodide';
1718

1819
const DEFAULT_RUNTIME = {
1920
src: 'https://cdn.jsdelivr.net/pyodide/v0.20.0/full/pyodide.js',
@@ -69,8 +70,6 @@ mode.subscribe((value: string) => {
6970
console.log('post initializers set');
7071
});
7172

72-
let pyodideReadyPromise;
73-
7473
let loader: PyLoader | undefined;
7574
globalLoader.subscribe(value => {
7675
loader = value;
@@ -86,11 +85,9 @@ export class PyodideRuntime extends Object {
8685

8786
async initialize() {
8887
loader?.log('Loading runtime...');
89-
pyodideReadyPromise = loadInterpreter(this.src);
90-
const pyodide = await pyodideReadyPromise;
88+
const pyodide: PyodideInterface = await loadInterpreter(this.src);
9189
const newEnv = {
9290
id: 'a',
93-
promise: pyodideReadyPromise,
9491
runtime: pyodide,
9592
state: 'loading',
9693
};
@@ -116,7 +113,7 @@ export class PyodideRuntime extends Object {
116113
loader?.log('Initializing scripts...');
117114
if (mode_ == 'play') {
118115
for (const script of scriptsQueue_) {
119-
script.evaluate();
116+
await script.evaluate();
120117
}
121118
scriptsQueue.set([]);
122119
}
@@ -129,11 +126,9 @@ export class PyodideRuntime extends Object {
129126
console.log('------ loader closed ------');
130127
}
131128

132-
setTimeout(() => {
133-
for (const initializer of postInitializers_) {
134-
initializer();
135-
}
136-
}, 3000);
129+
for (const initializer of postInitializers_) {
130+
await initializer();
131+
}
137132
}
138133
}
139134

pyscriptjs/src/components/pyenv.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import * as jsyaml from 'js-yaml';
33
import { pyodideLoaded, addInitializer } from '../stores';
44
import { loadPackage, loadFromFile } from '../interpreter';
55
import { handleFetchError } from '../utils';
6+
import type { PyodideInterface } from '../pyodide';
67

78
// Premise used to connect to the first available pyodide interpreter
8-
let pyodideReadyPromise;
9-
let runtime;
9+
let runtime: PyodideInterface;
1010

1111
pyodideLoaded.subscribe(value => {
1212
runtime = value;
@@ -18,7 +18,7 @@ export class PyEnv extends HTMLElement {
1818
wrapper: HTMLElement;
1919
code: string;
2020
environment: unknown;
21-
runtime: any;
21+
runtime: PyodideInterface;
2222
env: string[];
2323
paths: string[];
2424

@@ -67,7 +67,7 @@ export class PyEnv extends HTMLElement {
6767
await loadFromFile(singleFile, runtime);
6868
} catch (e) {
6969
//Should we still export full error contents to console?
70-
handleFetchError(e, singleFile);
70+
handleFetchError(<Error>e, singleFile);
7171
}
7272
}
7373
console.log('paths loaded');

pyscriptjs/src/components/pyrepl.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,9 @@ import { Compartment, StateCommand } from '@codemirror/state';
44
import { keymap } from '@codemirror/view';
55
import { defaultKeymap } from '@codemirror/commands';
66
import { oneDarkTheme } from '@codemirror/theme-one-dark';
7-
8-
import { componentDetailsNavOpen, loadedEnvironments, mode, pyodideLoaded, type Environment } from '../stores';
97
import { addClasses, htmlDecode } from '../utils';
108
import { BaseEvalElement } from './base';
119

12-
// Premise used to connect to the first available pyodide interpreter
13-
14-
let pyodideReadyPromise;
15-
let environments: Record<Environment['id'], Environment> = {};
16-
let currentMode;
17-
18-
pyodideLoaded.subscribe(value => {
19-
pyodideReadyPromise = value;
20-
});
21-
22-
loadedEnvironments.subscribe(value => {
23-
environments = value;
24-
});
25-
26-
let propertiesNavOpen;
27-
componentDetailsNavOpen.subscribe(value => {
28-
propertiesNavOpen = value;
29-
});
30-
31-
mode.subscribe(value => {
32-
currentMode = value;
33-
});
3410

3511
function createCmdHandler(el: PyRepl): StateCommand {
3612
// Creates a codemirror cmd handler that calls the el.evaluate when an event

pyscriptjs/src/interpreter.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { getLastPath } from './utils';
22
import type { PyodideInterface } from './pyodide';
3+
34
// eslint-disable-next-line
45
// @ts-ignore
56
import pyscript from './pyscript.py';
67

78
let pyodideReadyPromise;
8-
let pyodide;
9+
let pyodide: PyodideInterface;
910

1011
const loadInterpreter = async function (indexUrl: string): Promise<PyodideInterface> {
1112
console.log('creating pyodide runtime');
@@ -23,15 +24,19 @@ const loadInterpreter = async function (indexUrl: string): Promise<PyodideInterf
2324
await pyodide.loadPackage('micropip');
2425

2526
console.log('loading pyscript...');
26-
await pyodide.runPythonAsync(pyscript);
27+
28+
const output = await pyodide.runPythonAsync(pyscript);
29+
if (output !== undefined) {
30+
console.log(output);
31+
}
2732

2833
console.log('done setting up environment');
2934
return pyodide;
3035
};
3136

3237
const loadPackage = async function (package_name: string[] | string, runtime: PyodideInterface): Promise<void> {
3338
if (package_name.length > 0){
34-
const micropip = pyodide.globals.get('micropip');
39+
const micropip = runtime.globals.get('micropip');
3540
await micropip.install(package_name);
3641
micropip.destroy();
3742
}

pyscriptjs/src/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { PyLoader } from './components/pyloader';
1313
import { globalLoader } from './stores';
1414
import { PyConfig } from './components/pyconfig';
1515

16+
/* eslint-disable @typescript-eslint/no-unused-vars */
1617
const xPyScript = customElements.define('py-script', PyScript);
1718
const xPyRepl = customElements.define('py-repl', PyRepl);
1819
const xPyEnv = customElements.define('py-env', PyEnv);
@@ -23,6 +24,7 @@ const xPyInputBox = customElements.define('py-inputbox', PyInputBox);
2324
const xPyWidget = customElements.define('py-register-widget', PyWidget);
2425
const xPyLoader = customElements.define('py-loader', PyLoader);
2526
const xPyConfig = customElements.define('py-config', PyConfig);
27+
/* eslint-enable @typescript-eslint/no-unused-vars */
2628

2729
// As first thing, loop for application configs
2830
const config: PyConfig = document.querySelector('py-config');

pyscriptjs/src/stores.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,11 @@ export type Initializer = () => Promise<void>;
77

88
export type Environment = {
99
id: string;
10-
promise: Promise<PyodideInterface>;
1110
runtime: PyodideInterface;
1211
state: string;
1312
};
1413

15-
export const pyodideLoaded = writable({
16-
loaded: false,
17-
premise: null,
18-
});
14+
export const pyodideLoaded = writable();
1915

2016
export const loadedEnvironments = writable<Record<Environment['id'], Environment>>({});
2117
export const DEFAULT_MODE = 'play';

pyscriptjs/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function showError(msg: string): void {
6060

6161
function handleFetchError(e: Error, singleFile: string) {
6262
//Should we still export full error contents to console?
63-
console.warn('Caught an error in loadPaths:\r\n' + e);
63+
console.warn(`Caught an error in loadPaths:\r\n ${e.toString()}`);
6464
let errorContent: string;
6565
if (e.message.includes('TypeError: Failed to fetch')) {
6666
errorContent = `<p>PyScript: Access to local files

0 commit comments

Comments
 (0)
0