8000 Allow pyscript package to contain multiple files by hoodmane · Pull Request #1262 · pyscript/pyscript · GitHub
[go: up one dir, main page]

Skip to content

Allow pyscript package to contain multiple files #1262

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

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
1e34b4b
Allow pyscript package to contain multiple files
hoodmane Mar 8, 2023
b8d0835
Remove unused import
hoodmane Mar 8, 2023
564e1e1
Unvendor toml package
hoodmane Mar 8, 2023
19bfa96
Remove pyscript import
hoodmane Mar 8, 2023
04ae065
Fix many ESlint errors
hoodmane Mar 8, 2023
00f2d64
Merge branch 'fix-lints' into pyscript-package-2
hoodmane Mar 8, 2023
6ae4d7f
Put back Record
hoodmane Mar 8, 2023
21ea205
Merge branch 'fix-lints' into pyscript-package-2
hoodmane Mar 8, 2023
ea45633
Fix typescript compilation
hoodmane Mar 8, 2023
144efac
Fix typescript compilation
hoodmane Mar 8, 2023
09be677
Merge branch 'unvendor-toml' into fix-lints
hoodmane Mar 8, 2023
ad00491
Fix lints
hoodmane Mar 8, 2023
2e158d4
Try @iarna/toml instead
hoodmane Mar 8, 2023
ed61b1e
Fix import
hoodmane Mar 8, 2023
81b4c35
Use @ltd/j-toml
hoodmane Mar 8, 2023
81878c8
Update test
hoodmane Mar 8, 2023
dcdfc93
Use toml-j0.4
hoodmane Mar 8, 2023
6154b5c
Merge branch 'unvendor-toml' into fix-lints
hoodmane Mar 8, 2023
3c6fe2d
Some changes
hoodmane Mar 8, 2023
54ab4b3
Fix toml import
hoodmane Mar 9, 2023
8bb9ec4
Merge branch 'unvendor-toml' into fix-lints
hoodmane Mar 9, 2023
40324ab
Try adding eslint gha job
hoodmane Mar 9, 2023
d68c6e7
Add forgotten checkout action
hoodmane Mar 9, 2023
bcb9d92
Force CI to run
hoodmane Mar 9, 2023
336782a
Blah
hoodmane Mar 9, 2023
054ada0
Fix
hoodmane Mar 9, 2023
859f127
Revert changes to github workflow
hoodmane Mar 9, 2023
281328b
Merge branch 'main' into fix-lints
hoodmane Mar 9, 2023
06ea704
Fix lints
hoodmane Mar 9, 2023
d0c6815
Merge branch 'main' into pyscript-package-2
hoodmane Mar 9, 2023
7957814
Merge branch 'fix-lints' into pyscript-package-2
hoodmane Mar 9, 2023
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
22 changes: 11 additions & 11 deletions pyscriptjs/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ module.exports = {
plugins: ['@typescript-eslint'],
ignorePatterns: ['node_modules'],
rules: {
'no-prototype-builtins': 'warn',
'@typescript-eslint/no-unused-vars': 'warn',
'@typescript-eslint/no-explicit-any': 'warn',
'@typescript-eslint/no-unsafe-assignment': 'warn',
'@typescript-eslint/no-unsafe-argument': 'warn',
'@typescript-eslint/no-unsafe-member-access': 'warn',
'@typescript-eslint/no-unsafe-call': 'warn',
'@typescript-eslint/no-unsafe-return': 'warn',
'@typescript-eslint/no-floating-promises': 'warn',
'@typescript-eslint/restrict-plus-operands': 'warn',
'@typescript-eslint/no-empty-function': 'warn',
'no-prototype-builtins': 'error',
'@typescript-eslint/no-unused-vars': ['error', { args: 'none' }],
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-unsafe-assignment': 'error',
'@typescript-eslint/no-unsafe-argument': 'error',
'@typescript-eslint/no-unsafe-member-access': 'error',
'@typescript-eslint/no-unsafe-call': 'error',
'@typescript-eslint/no-unsafe-return': 'error',
'@typescript-eslint/no-floating-promises': 'error',
'@typescript-eslint/restrict-plus-operands': 'error',
'@typescript-eslint/no-empty-function': 'error',
},
};
70 changes: 47 additions & 23 deletions pyscriptjs/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,32 @@ import { terser } from 'rollup-plugin-terser';
import typescript from '@rollup/plugin-typescript';
import css from 'rollup-plugin-css-only';
import serve from 'rollup-plugin-serve';
import { string } from 'rollup-plugin-string';
import copy from 'rollup-plugin-copy';
import * as fs from 'fs';
import * as path from 'path';

function directoryManifest(root, dir = '.', result = { dirs: [], files: [] }) {
const curdir = path.join(root, dir);
const dirObj = fs.opendirSync(curdir);
try {
let d;
while ((d = dirObj.readSync())) {
const entry = path.join(dir, d.name);
if (d.isDirectory()) {
if (d.name === '__pycache__') {
continue;
}
result.dirs.push(entry);
directoryManifest(root, entry, result);
} else if (d.isFile()) {
result.files.push([entry, fs.readFileSync(path.join(root, entry), { encoding: 'utf-8' })]);
}
}
return result;
} finally {
dirObj.close();
}
}

const production = !process.env.ROLLUP_WATCH || process.env.NODE_ENV === 'production';

Expand All @@ -22,29 +46,30 @@ if (!production) {

export default {
input: 'src/main.ts',
output: [
{
file: 'build/pyscript.js',
format: 'iife',
sourcemap: true,
inlineDynamicImports: true,
name: 'pyscript',
},
{
file: 'build/pyscript.min.js',
format: 'iife',
sourcemap: true,
inlineDynamicImports: true,
name: 'pyscript',
plugins: [terser()],
},
],
output: [{ minify: true }, { minify: false }].map(({ minify }) => ({
Copy link
Contributor
@marimeireles marimeireles Mar 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do I pass the minify argument to this? (in general, just because I don't really know what's calling this code)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to change because of #1298.

file: `build/pyscript${minify ? '.min' : ''}.js`,
format: 'iife',
sourcemap: !production,
inlineDynamicImports: true,
name: 'pyscript',
plugins: [
terser({
compress: {
defaults: minify,
dead_code: true,
global_defs: {
pyscript_package: directoryManifest('./src/python'),
},
},
mangle: minify,
format: {
beautify: !minify,
},
}),
],
})),
plugins: [
css({ output: 'pyscript.css' }),
// Bundle all the Python files into the output file
string({
include: './src/**/*.py',
}),
resolve({
browser: true,
}),
Expand All @@ -55,7 +80,6 @@ export default {
}),
// This will make sure that examples will always get the latest build folder
copy(copy_targets),
// production && terser(),
!production &&
serve({
port: 8080,
Expand Down
9 changes: 5 additions & 4 deletions pyscriptjs/src/components/pyrepl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { basicSetup, EditorView } from 'codemirror';
import { python } from '@codemirror/lang-python';
import { indentUnit } from '@codemirror/language';
import { Compartment } from '@codemirror/state';
import { keymap } from '@codemirror/view';
import { keymap, Command } from '@codemirror/view';
import { defaultKeymap } from '@codemirror/commands';
import { oneDarkTheme } from '@codemirror/theme-one-dark';

Expand Down Expand Up @@ -68,8 +68,8 @@ export function make_PyRepl(interpreter: InterpreterClient) {
languageConf.of(python()),
keymap.of([
...defaultKeymap,
{ key: 'Ctrl-Enter', run: this.execute.bind(this), preventDefault: true },
{ key: 'Shift-Enter', run: this.execute.bind(this), preventDefault: true },
{ key: 'Ctrl-Enter', run: this.execute.bind(this) as Command, preventDefault: true },
{ key: 'Shift-Enter', run: this.execute.bind(this) as Command, preventDefault: true },
]),
];

Expand Down Expand Up @@ -134,7 +134,7 @@ export function make_PyRepl(interpreter: InterpreterClient) {
runButton.id = 'runButton';
runButton.className = 'absolute py-repl-run-button';
runButton.innerHTML = RUNBUTTON;
runButton.addEventListener('click', this.execute.bind(this));
runButton.addEventListener('click', this.execute.bind(this) as (e: MouseEvent) => void);
return runButton;
}

Expand Down Expand Up @@ -166,6 +166,7 @@ export function make_PyRepl(interpreter: InterpreterClient) {
outEl.innerHTML = '';

// execute the python code
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const pyResult = (await pyExec(interpreter, pySrc, outEl)).result;

// display the value of the last evaluated expression (REPL-style)
Expand Down
18 changes: 11 additions & 7 deletions pyscriptjs/src/components/pyscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function make_PyScript(interpreter: InterpreterClient, app: PyScriptApp)
*
* Concurrent access to the multiple py-script tags is thus avoided.
*/
let releaseLock: any;
let releaseLock: () => void;
try {
releaseLock = await app.tagExecutionLock();
ensureUniqueId(this);
Expand All @@ -35,13 +35,15 @@ export function make_PyScript(interpreter: InterpreterClient, app: PyScriptApp)
this.innerHTML = '';

app.plugins.beforePyScriptExec({ interpreter: interpreter, src: pySrc, pyScriptTag: this });
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
const result = (await pyExec(interpreter, pySrc, this)).result;
app.plugins.afterPyScriptExec({
interpreter: interpreter,
src: pySrc,
pyScriptTag: this,
result: result,
});
/* eslint-enable @typescript-eslint/no-unsafe-assignment */
} finally {
releaseLock();
}
Expand All @@ -53,7 +55,8 @@ export function make_PyScript(interpreter: InterpreterClient, app: PyScriptApp)
try {
const response = await robustFetch(url);
return await response.text();
} catch (e) {
} catch (err) {
const e = err as Error;
_createAlertBanner(e.message);
this.innerHTML = '';
throw e;
Expand Down Expand Up @@ -163,15 +166,15 @@ const pyAttributeToEvent: Map<string, string> = new Map<string, string>([
]);

/** Initialize all elements with py-* handlers attributes */
export function initHandlers(interpreter: InterpreterClient) {
export async function initHandlers(interpreter: InterpreterClient) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these now async?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry these changes are unrelated...

logger.debug('Initializing py-* event handlers...');
for (const pyAttribute of pyAttributeToEvent.keys()) {
createElementsWithEventListeners(interpreter, pyAttribute);
await createElementsWithEventListeners(interpreter, pyAttribute);
}
}

/** Initializes an element with the given py-on* attribute and its handler */
function createElementsWithEventListeners(interpreter: InterpreterClient, pyAttribute: string) {
async function createElementsWithEventListeners(interpreter: InterpreterClient, pyAttribute: string) {
const matches: NodeListOf<HTMLElement> = document.querySelectorAll(`[${pyAttribute}]`);
for (const el of matches) {
// If the element doesn't have an id, let's add one automatically!
Expand All @@ -195,7 +198,7 @@ function createElementsWithEventListeners(interpreter: InterpreterClient, pyAttr
// the source code may contain a syntax error, which will cause
// the splashscreen to not be removed.
try {
interpreter.run(source);
await interpreter.run(source);
} catch (e) {
logger.error((e as Error).message);
}
Expand All @@ -204,7 +207,8 @@ function createElementsWithEventListeners(interpreter: InterpreterClient, pyAttr
void (async () => {
try {
await interpreter.run(handlerCode);
} catch (err) {
} catch (e) {
const err = e as Error;
displayPyException(err, el.parentElement);
}
})();
Expand Down
13 changes: 6 additions & 7 deletions pyscriptjs/src/components/pywidget.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PyProxy } from 'pyodide';
import type { PyProxy, PyProxyCallable } from 'pyodide';
import { getLogger } from '../logger';
import { robustFetch } from '../fetch';
import { InterpreterClient } from '../interpreter_client';
Expand All @@ -13,8 +13,8 @@ function createWidget(interpreter: InterpreterClient, name: string, code: string
name: string = name;
klass: string = klass;
code: string = code;
proxy: PyProxy;
proxyClass: any;
proxy: PyProxy & { connect(): void };
proxyClass: PyProxyCallable;

constructor() {
super();
Expand All @@ -28,8 +28,8 @@ function createWidget(interpreter: InterpreterClient, name: string, code: string

async connectedCallback() {
await interpreter.runButDontRaise(this.code);
this.proxyClass = interpreter.globals.get(this.klass);
this.proxy = this.proxyClass(this);
this.proxyClass = interpreter.globals.get(this.klass) as PyProxyCallable;
this.proxy = this.proxyClass(this) as PyProxy & { connect(): void };
this.proxy.connect();
this.registerWidget();
}
Expand All @@ -39,9 +39,8 @@ function createWidget(interpreter: InterpreterClient, name: string, code: string
interpreter.globals.set(this.id, this.proxy);
}
}
/* eslint-disable @typescript-eslint/no-unused-vars */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const xPyWidget = customElements.define(name, CustomWidget);
/* eslint-enable @typescript-eslint/no-unused-vars */
}

export function make_PyWidget(interpreter: InterpreterClient) {
Expand Down
7 changes: 4 additions & 3 deletions pyscriptjs/src/interpreter_client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AppConfig } from './pyconfig';
import { RemoteInterpreter } from './remote_interpreter';
import type { PyProxy } from 'pyodide';
import type { PyProxyDict } from 'pyodide';
import { getLogger } from './logger';
import type { Stdio } from './stdio';

Expand All @@ -16,7 +16,7 @@ export class InterpreterClient extends Object {
/**
* global symbols table for the underlying interface.
* */
globals: PyProxy;
globals: PyProxyDict;
stdio: Stdio;

constructor(config: AppConfig, stdio: Stdio) {
Expand All @@ -32,14 +32,15 @@ export class InterpreterClient extends Object {
* */
async initializeRemote(): Promise<void> {
await this._remote.loadInterpreter(this.config, this.stdio);
this.globals = this._remote.globals;
this.globals = this._remote.globals as PyProxyDict;
}

/**
* delegates the code to be run to the underlying interface of
* the remote interpreter.
* Python exceptions are turned into JS exceptions.
* */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async run(code: string): Promise<{ result: any }> {
return await this._remote.run(code);
}
Expand Down
6 changes: 3 additions & 3 deletions pyscriptjs/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface Logger {
debug(message: string, ...args: unknown[]): void;
info(message: string, ...args: unknown[]): void;
warn(message: string, ...args: unknown[]): void;
error(message: string, ...args: unknown[]): void;
error(message: string | Error, ...args: unknown[]): void;
}

const _cache = new Map<string, Logger>();
Expand All @@ -44,8 +44,8 @@ function getLogger(prefix: string): Logger {
function _makeLogger(prefix: string): Logger {
prefix = `[${prefix}] `;

function make(level: string) {
const out_fn = console[level].bind(console);
function make(level: 'info' | 'debug' | 'warn' | 'error') {
const out_fn = console[level].bind(console) as typeof console.log;
function fn(fmt: string, ...args: unknown[]) {
out_fn(prefix + fmt, ...args);
}
Expand Down 447D
Loading
0