Skip to content

Commit 101ec74

Browse files
Add support for snoop debugging in Python
1 parent 812393d commit 101ec74

8 files changed

Lines changed: 22 additions & 10 deletions

File tree

src/BackendEvent.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ export enum BackendEventType {
88
Output = "output",
99
Sleep = "sleep",
1010
Error = "error",
11-
Interrupt = "interrupt"
11+
Interrupt = "interrupt",
12+
Debug = "debug"
1213
}
1314
/**
1415
* All possible types for ease of iteration
@@ -17,7 +18,7 @@ export const BACKEND_EVENT_TYPES = [
1718
BackendEventType.Start, BackendEventType.End,
1819
BackendEventType.Input, BackendEventType.Output,
1920
BackendEventType.Sleep, BackendEventType.Error,
20-
BackendEventType.Interrupt
21+
BackendEventType.Interrupt, BackendEventType.Debug
2122
];
2223
/**
2324
* Interface for events used for communication between threads

src/OutputManager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export class OutputManager {
4848
constructor() {
4949
BackendManager.subscribe(BackendEventType.Start, () => this.reset());
5050
BackendManager.subscribe(BackendEventType.Output, e => this.showOutput(e));
51+
BackendManager.subscribe(BackendEventType.Debug, e => this.showOutput(e));
5152
BackendManager.subscribe(BackendEventType.Error, e => this.showError(e));
5253
this.options = { parentElementId: "" };
5354
}

src/util/Util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export interface ButtonOptions {
7878
*/
7979
buttonText: string;
8080
/**
81-
* Optional classes to apply to the button
81+
* Optional Tailwind classes to apply to the button
8282
*/
8383
extraClasses?: string;
8484
}

src/workers/python/PythonWorker.worker.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,20 @@ class PythonWorker extends Backend<PyodideExtras> {
5555
);
5656
}
5757

58-
async runCode(extras: PyodideExtras, code: string): Promise<any> {
58+
override runModes(): string[] {
59+
return [...super.runModes(), "snoop"];
60+
}
61+
62+
async runCode(extras: PyodideExtras, code: string, mode = "exec"): Promise<any> {
5963
this.extras = extras;
6064
if (extras.interruptBuffer) {
6165
this.pyodide.setInterruptBuffer(extras.interruptBuffer);
6266
}
63-
await this.papyros.run_async(code);
67+
await this.papyros.run_async.callKwargs({
68+
source_code: code,
69+
mode: mode,
70+
snoop_config: { color: false }
71+
});
6472
}
6573

6674
override async autocomplete(context: WorkerAutocompleteContext):

src/workers/python/build_package.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ def check_tar(tarname):
4646

4747

4848
if __name__ == "__main__":
49-
create_package("python_package", "python-runner friendly_traceback jedi", extra_deps="papyros")
49+
create_package("python_package", "python-runner friendly_traceback jedi snoop", extra_deps="papyros")

src/workers/python/papyros/papyros.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ def cb(typ, dat, contentType=None, **kwargs):
4747
# Do not display values entered by user for input
4848
continue
4949
else:
50-
cb("output", part["text"], contentType=part.get("contentType"))
50+
ev_type = "debug" if typ in ["snoop"] else "output"
51+
cb(ev_type, part["text"], contentType=part.get("contentType"))
5152
elif event_type == "input":
5253
return cb("input", data["prompt"])
5354
elif event_type == "sleep":
@@ -94,17 +95,17 @@ async def install_imports(self, source_code, ignore_missing=True):
9495
if not ignore_missing:
9596
raise
9697

97-
def pre_run(self, source_code, mode="exec", top_level_await=False):
98+
def pre_run(self, source_code, mode="exec", top_level_await=True):
9899
self.override_globals()
99100
return super().pre_run(source_code, mode=mode, top_level_await=top_level_await)
100101

101-
async def run_async(self, source_code, mode="exec", top_level_await=True):
102+
async def run_async(self, source_code, mode="exec", top_level_await=True, snoop_config=None):
102103
with self._execute_context():
103104
try:
104105
await self.install_imports(source_code, ignore_missing=False)
105106
code_obj = self.pre_run(source_code, mode=mode, top_level_await=top_level_await)
106107
if code_obj:
107-
result = self.execute(code_obj, mode)
108+
result = self.execute(code_obj, mode=mode, snoop_config=to_py(snoop_config))
108109
while isinstance(result, Awaitable):
109110
result = await result
110111
return result
918 KB
Binary file not shown.

webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ module.exports = function (webpackEnv, argv) {
8686
devServer: {
8787
static: path.join(__dirname, PUBLIC_DIR),
8888
port: DEVELOPMENT_PORT,
89+
hot: false // Many things in the workers cannot be replaced in a hot fashion
8990
},
9091
}
9192
};

0 commit comments

Comments
 (0)