8000 Minor refactoring (#437) · yannickfunk/pyscript@8cd5ba6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8cd5ba6

Browse files
authored
Minor refactoring (pyscript#437)
1 parent c951961 commit 8cd5ba6

File tree

3 files changed

+22
-37
lines changed

3 files changed

+22
-37
lines changed

pyscriptjs/src/components/base.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ export class BaseEvalElement extends HTMLElement {
6262
}
6363

6464
async getSourceFromFile(s: string): Promise<string> {
65-
const pyodide = runtime;
6665
const response = await fetch(s);
6766
this.code = await response.text();
6867
return this.code;
@@ -104,11 +103,8 @@ export class BaseEvalElement extends HTMLElement {
104103
let source: string;
105104
let output;
106105
try {
107-
if (this.source) {
108-
source = await this.getSourceFromFile(this.source);
109-
} else {
110-
source = this.getSourceFromElement();
111-
}
106+
source = this.source ? await this.getSourceFromFile(this.source)
107+
: this.getSourceFromElement();
112108

113109
await this._register_esm(pyodide);
114110

@@ -142,7 +138,7 @@ export class BaseEvalElement extends HTMLElement {
142138
if (errorElements.length > 0) {
143139
for (const errorElement of errorElements) {
144140
errorElement.classList.add('hidden');
145-
if(this.hasAttribute('std-err')) {
141+
if (this.hasAttribute('std-err')) {
146142
this.errorElement.hidden = true;
147143
this.errorElement.style.removeProperty('display');
148144
}
@@ -160,7 +156,7 @@ export class BaseEvalElement extends HTMLElement {
160156
addClasses(this.errorElement, ['bg-red-200', 'p-2']);
161157
out.write.callKwargs(err, { append: true });
162158

163-
this.errorElement.children[this.errorElement.children.length - 1].setAttribute('error', '')
159+
this.errorElement.children[this.errorElement.children.length - 1].setAttribute('error', '');
164160
this.errorElement.hidden = false;
165161
this.errorElement.style.display = 'block';
166162
}
@@ -249,7 +245,6 @@ function createWidget(name: string, code: string, klass: string) {
249245
}
250246
}
251247
}
252-
const xPyWidget = customElements.define(name, CustomWidget);
253248
}
254249

255250
export class PyWidget extends HTMLElement {
@@ -290,7 +285,6 @@ export class PyWidget extends HTMLElement {
290285
throw new ReferenceError(
291286
`No id specified for component. Components must have an explicit id. Please use id="" to specify your component id.`,
292287
);
293-
return;
294288
}
295289

296290
const mainDiv = document.createElement('div');

pyscriptjs/src/components/pyrepl.ts

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ function createCmdHandler(el: PyRepl): StateCommand {
4343

4444
let initialTheme: string;
4545
function getEditorTheme(el: BaseEvalElement): string {
46-
if (initialTheme) {
47-
return initialTheme;
48-
}
49-
50-
return (initialTheme = el.getAttribute('theme'));
46+
return initialTheme || (initialTheme = el.getAttribute('theme'));
5147
}
5248

5349
export class PyRepl extends BaseEvalElement {
@@ -165,11 +161,9 @@ export class PyRepl extends BaseEvalElement {
165161
mainDiv.appendChild(this.outputElement);
166162
}
167163

168-
if (this.hasAttribute('std-err')) {
169-
this.errorElement = document.getElementById(this.getAttribute('std-err'));
170-
} else {
171-
this.errorElement = this.outputElement;
172-
}
164+
this.errorElement = this.hasAttribute('std-err')
165+
? document.getElementById(this.getAttribute('std-err'))
166+
: this.outputElement;
173167
}
174168

175169
this.appendChild(mainDiv);
@@ -188,7 +182,7 @@ export class PyRepl extends BaseEvalElement {
188182

189183
if (this.hasAttribute('auto-generate')) {
190184
const allPyRepls = document.querySelectorAll(`py-repl[root='${this.getAttribute('root')}'][exec-id]`);
191-
const lastRepl = allPyRepls[allPyRepls.length -1 ];
185+
const lastRepl = allPyRepls[allPyRepls.length - 1];
192186
const lastExecId = lastRepl.getAttribute('exec-id');
193187
const nextExecId = parseInt(lastExecId) + 1;
194188

@@ -198,17 +192,15 @@ export class PyRepl extends BaseEvalElement {
198192
newPyRepl.setAttribute('auto-generate', '');
199193
this.removeAttribute('auto-generate');
200194

201-
if (this.hasAttribute('output')) {
202-
newPyRepl.setAttribute('output', this.getAttribute('output'));
203-
}
195+
const addReplAttribute = (attribute: string) => {
196+
if (this.hasAttribute(attribute)) {
197+
newPyRepl.setAttribute(attribute, this.getAttribute(attribute));
198+
}
199+
};
204200

205-
if (this.hasAttribute('std-out')) {
206-
newPyRepl.setAttribute('std-out', this.getAttribute('std-out'));
207-
}
208-
209-
if (this.hasAttribute('std-err')) {
210-
newPyRepl.setAttribute('std-err', this.getAttribute('std-err'));
211-
}
201+
addReplAttribute('output');
202+
addReplAttribute('std-out');
203+
addReplAttribute('std-err');
212204

213205
newPyRepl.setAttribute('exec-id', nextExecId.toString());
214206
this.parentElement.appendChild(newPyRepl);

pyscriptjs/src/utils.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ function ltrim(code: string): string {
3232

3333
const k = Math.min(...lengths);
3434

35-
if (k != 0) return lines.map(line => line.substring(k)).join('\n');
36-
else return code;
35+
return k != 0 ? lines.map(line => line.substring(k)).join('\n')
36+
: code;
3737
}
3838

3939
function guidGenerator(): string {
@@ -58,10 +58,10 @@ function showError(msg: string): void {
5858
document.body.prepend(warning);
5959
}
6060

61-
function handleFetchError(e: Error, singleFile: string){
61+
function handleFetchError(e: Error, singleFile: string) {
6262
//Should we still export full error contents to console?
6363
console.warn('Caught an error in loadPaths:\r\n' + e);
64-
let errorContent;
64+
let errorContent: string;
6565
if (e.message.includes('TypeError: Failed to fetch')) {
6666
errorContent = `<p>PyScript: Access to local files
6767
(using "Paths:" in &lt;py-env&gt;)
@@ -75,8 +75,7 @@ function handleFetchError(e: Error, singleFile: string){
7575
singleFile +
7676
`</u> failed with error 404 (File not Found). Are your filename and path are correct?</p>`;
7777
} else {
78-
errorContent =
79-
'<p>PyScript encountered an error while loading from file: ' + e.message + '</p>';
78+
errorContent = '<p>PyScript encountered an error while loading from file: ' + e.message + '</p>';
8079
}
8180
showError(errorContent);
8281
}

0 commit comments

Comments
 (0)
0