Skip to content

Commit

Permalink
No embedded Python code anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
mellelieuwes committed Nov 18, 2022
1 parent 815aff7 commit 08296b8
Show file tree
Hide file tree
Showing 35 changed files with 554 additions and 474 deletions.
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length = 120
8 changes: 8 additions & 0 deletions dist/framework/processing/py_worker.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
declare function runCycle(payload: any): void;
declare function unwrap(response: any): Promise<any>;
declare function copyFileToPyFS(file: any, resolve: any): void;
declare function initialise(): any;
declare function startPyodide(): any;
declare function loadPackages(): any;
declare function installPortPackage(): any;
declare let pyScript: any;
86 changes: 86 additions & 0 deletions dist/framework/processing/py_worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
var pyScript;
onmessage = function (event) {
var eventType = event.data.eventType;
switch (eventType) {
case 'initialise':
initialise().then(function () {
self.postMessage({ eventType: 'initialiseDone' });
});
break;
case 'firstRunCycle':
pyScript = self.pyodide.runPython("port.start(".concat(event.data.sessionId, ")"));
runCycle(null);
break;
case 'nextRunCycle':
var response = event.data.response;
unwrap(response).then(function (userInput) {
runCycle(userInput);
});
break;
default:
console.log('[ProcessingWorker] Received unsupported event: ', eventType);
}
};
function runCycle(payload) {
console.log('[ProcessingWorker] runCycle ' + JSON.stringify(payload));
scriptEvent = pyScript.send(payload);
self.postMessage({
eventType: 'runCycleDone',
scriptEvent: scriptEvent.toJs({
create_proxies: false,
dict_converter: Object.fromEntries
})
});
}
function unwrap(response) {
console.log('[ProcessingWorker] unwrap response: ' + JSON.stringify(response.payload));
return new Promise(function (resolve) {
switch (response.payload.__type__) {
case 'PayloadFile':
copyFileToPyFS(response.payload.value, resolve);
break;
default:
resolve(response.payload);
}
});
}
function copyFileToPyFS(file, resolve) {
var reader = file.stream().getReader();
var pyFile = self.pyodide.FS.open(file.name, 'w');
var writeToPyFS = function (_a) {
var done = _a.done, value = _a.value;
if (done) {
resolve({ __type__: 'PayloadString', value: file.name });
}
else {
self.pyodide.FS.write(pyFile, value, 0, value.length);
reader.read().then(writeToPyFS);
}
};
reader.read().then(writeToPyFS);
}
function initialise() {
console.log('[ProcessingWorker] initialise');
return startPyodide().then(function (pyodide) {
self.pyodide = pyodide;
return loadPackages();
})
.then(function () {
return installPortPackage();
});
}
function startPyodide() {
importScripts('https://cdn.jsdelivr.net/pyodide/v0.21.2/full/pyodide.js');
console.log('[ProcessingWorker] loading Pyodide');
return loadPyodide({
indexURL: 'https://cdn.jsdelivr.net/pyodide/v0.21.2/full/'
});
}
function loadPackages() {
console.log('[ProcessingWorker] loading packages');
return self.pyodide.loadPackage(['micropip', 'numpy', 'pandas']);
}
function installPortPackage() {
console.log('[ProcessingWorker] load port package');
return self.pyodide.runPythonAsync("\n import micropip\n await micropip.install(\"/port-0.1.0-py3-none-any.whl\", deps=False)\n import port\n ");
}
8 changes: 0 additions & 8 deletions dist/framework/processing/python/worker.d.ts

This file was deleted.

86 changes: 0 additions & 86 deletions dist/framework/processing/python/worker.js

This file was deleted.

6 changes: 2 additions & 4 deletions dist/framework/processing/worker_engine.d.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { CommandHandler, ProcessingEngine } from '../types/modules';
import { Response, Script } from '../types/commands';
import { Response } from '../types/commands';
export default class WorkerProcessingEngine implements ProcessingEngine {
sessionId: String;
worker: Worker;
commandHandler: CommandHandler;
script: Script;
resolveInitialized: () => void;
resolveContinue: () => void;
constructor(sessionId: string, worker: Worker, commandHandler: CommandHandler);
trackUserStart(sessionId: string): void;
handleEvent(event: any): void;
start(script: Script): void;
start(): void;
waitForInitialization(): Promise<void>;
waitForSplashScreen(): Promise<void>;
renderSplashScreen(): void;
loadScript(script: any): void;
firstRunCycle(): void;
nextRunCycle(response: Response): void;
terminate(): void;
Expand Down
13 changes: 1 addition & 12 deletions dist/framework/processing/worker_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ var WorkerProcessingEngine = /** @class */ (function () {
switch (eventType) {
case 'initialiseDone':
console.log('[ReactEngine] received: initialiseDone');
this.loadScript(this.script);
break;
case 'loadScriptDone':
console.log('[ReactEngine] Received: loadScriptDone');
this.resolveInitialized();
break;
case 'runCycleDone':
Expand All @@ -74,10 +70,9 @@ var WorkerProcessingEngine = /** @class */ (function () {
console.log('[ReactEngine] received unsupported flow event: ', eventType);
}
};
WorkerProcessingEngine.prototype.start = function (script) {
WorkerProcessingEngine.prototype.start = function () {
var _this = this;
console.log('[WorkerProcessingEngine] started');
this.script = script;
var waitForInitialization = this.waitForInitialization();
var waitForSplashScreen = this.waitForSplashScreen();
Promise.all([waitForInitialization, waitForSplashScreen]).then(function () { _this.firstRunCycle(); }, function () { });
Expand Down Expand Up @@ -116,12 +111,6 @@ var WorkerProcessingEngine = /** @class */ (function () {
if (isCommand(command)) {
this.commandHandler.onCommand(command).then(function (_response) { return _this.resolveContinue(); }, function () { });
}
else {
console.log('HUH?!');
}
};
WorkerProcessingEngine.prototype.loadScript = function (script) {
this.worker.postMessage({ eventType: 'loadScript', script: script });
};
WorkerProcessingEngine.prototype.firstRunCycle = function () {
this.worker.postMessage({ eventType: 'firstRunCycle', sessionId: this.sessionId });
Expand Down
4 changes: 0 additions & 4 deletions dist/framework/types/commands.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { PropsUIPage } from './pages';
export declare type Script = string | File | URL;
export declare function isScript(arg: any): arg is Script;
export declare function isFile(arg: unknown): arg is File;
export declare function isURL(arg: any): arg is URL;
export interface Table {
__type__: 'Table';
id: string;
Expand Down
11 changes: 1 addition & 10 deletions dist/framework/types/commands.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import { isInstanceOf, isLike } from '../helpers';
import { isInstanceOf } from '../helpers';
import { isPropsUIPage } from './pages';
export function isScript(arg) {
return typeof arg === 'string' || isFile(arg) || isURL(arg);
}
export function isFile(arg) {
return isLike(arg, ['arrayBuffer', 'lastModified', 'name', 'size', 'slice', 'stream', 'text', 'type', 'webkitRelativePath']);
}
export function isURL(arg) {
return isLike(arg, ['hash', 'host', 'hostname', 'href', 'origin', 'toString', 'password', 'pathname', 'port', 'protocol', 'search', 'searchParams', 'username', 'toJSON']);
}
export function isTable(arg) {
return isInstanceOf(arg, 'Table', ['id', 'title', 'data']);
}
Expand Down
4 changes: 2 additions & 2 deletions dist/framework/types/modules.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Script, Command, Response, CommandSystem, CommandUI } from './commands';
import { Command, Response, CommandSystem, CommandUI } from './commands';
export interface ProcessingEngine {
start: (script: Script) => void;
start: () => void;
commandHandler: CommandHandler;
terminate: () => void;
}
Expand Down
Binary file added dist/port-0.0.0-py3-none-any.whl
Binary file not shown.
Binary file added dist/port-0.0.0.tar.gz
Binary file not shown.
Loading

0 comments on commit 08296b8

Please sign in to comment.