mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-22 19:53:00 -05:00
Use the type definition of pyodide (#535)
* Import the type definition * Add type annotations
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { loadedEnvironments, mode, pyodideLoaded } from '../stores';
|
||||
import { guidGenerator, addClasses, removeClasses } from '../utils';
|
||||
import type { PyodideInterface } from '../pyodide';
|
||||
// Premise used to connect to the first available pyodide interpreter
|
||||
let runtime;
|
||||
let environments;
|
||||
@@ -17,11 +18,6 @@ mode.subscribe(value => {
|
||||
currentMode = value;
|
||||
});
|
||||
|
||||
// TODO: use type declaractions
|
||||
type PyodideInterface = {
|
||||
registerJsModule(name: string, module: object): void;
|
||||
};
|
||||
|
||||
export class BaseEvalElement extends HTMLElement {
|
||||
shadow: ShadowRoot;
|
||||
wrapper: HTMLElement;
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
} from '../stores';
|
||||
import { addClasses, htmlDecode } from '../utils';
|
||||
import { BaseEvalElement } from './base';
|
||||
import type { PyodideInterface } from '../pyodide';
|
||||
|
||||
// Premise used to connect to the first available pyodide interpreter
|
||||
let pyodideReadyPromise;
|
||||
@@ -25,11 +26,6 @@ mode.subscribe(value => {
|
||||
currentMode = value;
|
||||
});
|
||||
|
||||
// TODO: use type declaractions
|
||||
type PyodideInterface = {
|
||||
registerJsModule(name: string, module: object): void;
|
||||
};
|
||||
|
||||
export class PyScript extends BaseEvalElement {
|
||||
constructor() {
|
||||
super();
|
||||
@@ -143,7 +139,7 @@ async function initHandlers() {
|
||||
}
|
||||
|
||||
/** Initializes an element with the given pys-on* attribute and its handler */
|
||||
async function createElementsWithEventListeners(pyodide: any, pysAttribute: string) {
|
||||
async function createElementsWithEventListeners(pyodide: PyodideInterface, pysAttribute: string): Promise<void> {
|
||||
const matches: NodeListOf<HTMLElement> = document.querySelectorAll(`[${pysAttribute}]`);
|
||||
for (const el of matches) {
|
||||
if (el.id.length === 0) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { getLastPath } from './utils';
|
||||
import type { PyodideInterface } from './pyodide';
|
||||
// eslint-disable-next-line
|
||||
// @ts-ignore
|
||||
import pyscript from './pyscript.py';
|
||||
@@ -6,7 +7,7 @@ import pyscript from './pyscript.py';
|
||||
let pyodideReadyPromise;
|
||||
let pyodide;
|
||||
|
||||
const loadInterpreter = async function (indexUrl: string): Promise<any> {
|
||||
const loadInterpreter = async function (indexUrl: string): Promise<PyodideInterface> {
|
||||
console.log('creating pyodide runtime');
|
||||
// eslint-disable-next-line
|
||||
// @ts-ignore
|
||||
@@ -28,7 +29,7 @@ const loadInterpreter = async function (indexUrl: string): Promise<any> {
|
||||
return pyodide;
|
||||
};
|
||||
|
||||
const loadPackage = async function (package_name: string[] | string, runtime: any): Promise<any> {
|
||||
const loadPackage = async function (package_name: string[] | string, runtime: PyodideInterface): Promise<void> {
|
||||
if (package_name.length > 0){
|
||||
const micropip = pyodide.globals.get('micropip');
|
||||
await micropip.install(package_name);
|
||||
@@ -36,7 +37,7 @@ const loadPackage = async function (package_name: string[] | string, runtime: an
|
||||
}
|
||||
};
|
||||
|
||||
const loadFromFile = async function (s: string, runtime: any): Promise<any> {
|
||||
const loadFromFile = async function (s: string, runtime: PyodideInterface): Promise<void> {
|
||||
const filename = getLastPath(s);
|
||||
await runtime.runPythonAsync(
|
||||
`
|
||||
|
||||
4
pyscriptjs/src/pyodide.ts
Normal file
4
pyscriptjs/src/pyodide.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import type { loadPyodide } from 'pyodide';
|
||||
|
||||
// The current release doesn't export `PyodideInterface` type
|
||||
export type PyodideInterface = Awaited<ReturnType<typeof loadPyodide>>;
|
||||
Reference in New Issue
Block a user