Repository: pyscript/pyscript
File: pyscriptjs/src/components/pyscript.ts (TypeScript)

Code before:
import { htmlDecode, ensureUniqueId, showWarning } from '../utils';
import type { Runtime } from '../runtime';
import { getLogger } from '../logger';
import { pyExec } from '../pyexec';
import { FetchError, _createAlertBanner } from '../exceptions';

const logger = getLogger('py-script');

export function make_PyScript(runtime: Runtime) {
    class PyScript extends HTMLElement {
        async connectedCallback() {
            if (this.hasAttribute('output')) {
                const deprecationMessage = (
                    "The 'output' attribute is deprecated. You should use the " +
                    "'display' API to output the content to a specific element. " +
                    'For example display(myElement, target="divID").'
                )
                showWarning(deprecationMessage)
            }
            ensureUniqueId(this);
            const pySrc = await this.getPySrc();
            this.innerHTML = '';
            pyExec(runtime, pySrc, this);
        }

        async getPySrc(): Promise<string> {
            if (this.hasAttribute('src')) {
                const url = this.getAttribute('src');
                const response = await fetch(url);
                if (response.status !== 200) {
                    const errorMessage = (
                        `Failed to fetch '${url}' - Reason: ` +
                        `${response.status} ${response.statusText}`
                    );
                    _createAlertBanner(errorMessage);
                    this.innerHTML = '';
                    throw new FetchError(errorMessage);
                }
                return await response.text();
            } else {
                return htmlDecode(this.innerHTML);

Review comment (suggestion):
```suggestion
                    "The 'output' attribute is deprecated and ignored. You should use " +
                    "'display()' to output the content to a specific element. " +
                    'For example display(myElement, target="divID").'
```

Code after:
import { htmlDecode, ensureUniqueId, showWarning } from '../utils';
import type { Runtime } from '../runtime';
import { getLogger } from '../logger';
import { pyExec } from '../pyexec';
import { _createAlertBanner } from '../exceptions';
import { robustFetch } from '../fetch';

const logger = getLogger('py-script');

export function make_PyScript(runtime: Runtime) {
    class PyScript extends HTMLElement {
        async connectedCallback() {
            if (this.hasAttribute('output')) {
                const deprecationMessage = (
                    "The 'output' attribute is deprecated and ignored. You should use " +
                    "'display()' to output the content to a specific element. " +
                    'For example display(myElement, target="divID").'
                )
                showWarning(deprecationMessage)
            }
            ensureUniqueId(this);
            const pySrc = await this.getPySrc();
            this.innerHTML = '';
            pyExec(runtime, pySrc, this);
        }

        async getPySrc(): Promise<string> {
            if (this.hasAttribute('src')) {
                const url = this.getAttribute('src');
                try {
                    const response = await robustFetch(url);
                    return await response.text();
                } catch(e) {
                    _createAlertBanner(e.message);
                    this.innerHTML = '';
                    throw e
                }
            } else {
                return htmlDecode(this.innerHTML);
            }
        }