pyodide: Prompt of `input(prompt="..")` is not shown in the browser input dialog

Expected behaviour Browser input prompt with message provided in input(prompt="..") is shown.

Actual behaviour Prompt with message “Input” appears, instead of prompt value.

Versions Firefox 79.0 pyodide 0.15.0

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 3
  • Comments: 15 (10 by maintainers)

Most upvoted comments

Managed to fix this behavior by adding this code:

<script>
function input_fixed(text) {
    return prompt(text);
};

languagePluginLoader.then(() => {
  pyodide.runPythonAsync(`
from js import input_fixed
input = input_fixed
__builtins__.input = input_fixed
`);
});
</script>

By the way, I have a demo with a synchronous IO implementation here. I think input, pdb, etc work with it. https://spectralsequences.com/worker-pyodide-console/ Repo: https://github.com/hoodmane/worker-pyodide-console/

<pyscript>
from js import prompt
def input(p):
    return prompt(p)
__builtins__.input = input
</pyscript>

This worked great. Shouldn’t this functionality be the default for Pyodide? At least after calling pyodide.setStdin(). I was surprised I had to do this workaround as a JavaScript pop-up prompt without a message is pretty useless.

I mentioned this on Gitter, but I suppose noting it down here would be useful. I took a stab at this, including synchronous I/O and think I figured out how to do so properly

First I edited pyodide.js to support redirecting stdin. While I was at it, I also decided to support redirecting the print and printErr functions

// Inject custom stdin/stdout/stderr
if(config.stdin) {
  Module.preRun = [function() {
    Module.FS.init(config.stdin, null, null);
  }];
}
if(config.print) {
  Module.print = config.print;
}
if(config.printErr) {
  Module.printErr = config.printErr;
}

https://github.com/stefnotch/starboard-python/blob/20c76fb95a97a3065308ccf99828efedc5badf7d/src/pyodide/pyodide.ts#L49-L60

With that set up, I could now write my own stdin. Ignoring the synchronous IO part, it would look like this

let input: number[] = [];
let inputIndex = -1; // -1 means that we just returned null
function stdin() {
  if (inputIndex === -1) {
    input = intArrayFromString((prompt() || "") + "\n", true, 0);
    inputIndex = 0;
  }

  if (inputIndex < input.length) {
    let character = input[inputIndex];
    inputIndex++;
    return character;
  } else {
    inputIndex = -1;
    return null;
  }
}

https://github.com/stefnotch/starboard-python/blob/20c76fb95a97a3065308ccf99828efedc5badf7d/src/worker/pyodide-worker.ts#L237-L253

The intArrayFromString is copied from emscripten, because it’s not exposed by default.

I think it would be really useful to expose the input functionality, preferably with the whole wrapping and converting already being done. So, maybe something like this

self.loadPyodide({
  indexURL: artifactsURL,
  input: () => prompt(),
  print: (text) => console.log(text),
  printErr: (text) => console.error(text),
});

Indeed has not been fixed. Hopefully we’ll add support for synchronous IO in v0.18.