Skip to content

Conversation

LamHo220
Copy link
Contributor

@LamHo220 LamHo220 commented Mar 7, 2025

About this pull request

There are several bugs or enhancement in the hook

  1. Invalid url in getInput() in next.js project for Chromium (SyntaxError: The string did not match the expected pattern for Safari)
  2. We can handle the output for users and the output stream should be stdout line 1 -> stdout line 2 -> ... -> stdout line n -> stderr.
  3. Timeout will terminate the workers.

Fixing the bug in 1.

  1. Adding workerUrl in props for the PythonProvider in next.js. This allowes the provider use public/react-py-sw.js for the service-worker;
  2. use windows.location.origin (location.origin for python-worker.ts) as the base url of the url in PythonProvider.
<PythonProvider workerUrl="react-py-sw.js">
    {children}
</PythonProvider>

Enhancing the output

For example, the python script

for i in range(10):
  print(i)
  if i == 2:
    raise ValueError()

should results in

0
1
2
Traceback (most recent call last):
  File "<console>", line 4, in <module>
ValueError

but the code in the docs will leads because the stdout is still updating the results.

Traceback (most recent call last):
  File "<console>", line 4, in <module>
ValueError
0
1
2

Due to the rendering bug in Safari. By observation, updating output array in stdout stream will end faster than updating the stdout. We can wait until the stdout stream is ended which is stdout === output.join('') and add the stderr at the end.

Fix 3.

I added the shared arrary buffer based on https://pyodide.org/en/stable/usage/keyboard-interrupts.html. However, github pages does allowed to add custom headers so i can update the docs because of that

TODO

  • fix input error for next.js
  • handle the outputs
  • show stderr after appending the previous stdout
  • fix 3.
  • update docs
  • test in other browsers (test in safari, chrome, firefox)

@LamHo220 LamHo220 closed this May 28, 2025
@LamHo220 LamHo220 reopened this Jun 2, 2025
@LamHo220 LamHo220 closed this Jun 2, 2025
@LamHo220 LamHo220 deleted the use-python-console branch June 2, 2025 03:29
@LamHo220 LamHo220 restored the use-python-console branch June 2, 2025 03:30
@LamHo220 LamHo220 reopened this Jun 2, 2025
@LamHo220 LamHo220 changed the title [Draft] Fix usePythonConsole Feature Update Jun 2, 2025
@LamHo220 LamHo220 marked this pull request as ready for review June 2, 2025 11:29
@elilambnz elilambnz self-requested a review September 28, 2025 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant