Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python code inputs #239

Closed
therealOri opened this issue May 5, 2022 · 6 comments
Closed

Python code inputs #239

therealOri opened this issue May 5, 2022 · 6 comments
Labels
backlog issue has been triaged but has not been earmarked for any upcoming release lang: python Related to the Python language tag: pyodide Related to issues with pyodide type: bug Something isn't working

Comments

@therealOri
Copy link

In my python script, When i ask for user input...It is very very weird. The following are some examples of what's happening when I ask for input.

Maybe I am just doing things wrong but for someone just getting started with making web-pages and messing with html, finding documentation that is simple and has lots of examples to work off of is not easy or common. Perhaps working on ways in pyscript to allow for my issues here to be done in an easier way would be even more helpful.




Firstly;

What I ask in my input field doesn't show up in the java-script pop-up. (I do not know how to make the pop-up show what I want it to show and documentation is a bit scarce at the moment...) It would also be nice if the pop-up had the same text as the python input..somehow.

image
image



Secondly;

AFTER I enter my input into the java-script pop-up window and hit enter, What I have in the previous image as my input message "Length", Get's printed to the body of my web page and will never go away unless I refresh. The rest of my code will execute just fine and take the argument that is given to the java-script pop-up. It would be great if the python "input" didn't "print" to the page body.
image



Additionally;

Unfortunately because I lack the experience/know how to be able to print all of the output my python code would make to a window/box on my site/page...everything just gets directly printed to the page/html itself...basically I would like some additional information on how to print my python output to a box or window on my web-page and to have the text in that box be hidden, a button to copy what is there, and a "hide" toggle box....Instead of just printing everything directly to the page body.





Example Code;

Here is some code that I am using for my project. I am also using python -m http.server to test stuff locally.

  • html
    image

  • python
    image




Overwiew;

Asking for user input in a python script doesn't work to well and is a bit buggy.

That or I am completely doing it wrong and have to use stuff that isn't python to ask for input and then somehow give that input to my python code...idk big headache (Still learning).

@antocuni antocuni added the needs-triage Issue needs triage label May 6, 2022
@ckavidas
Copy link

ckavidas commented May 7, 2022

Just out of curiosity, is it advisable to use python's input type on a webpage? Shouldn't you use an HTML form for this?

@therealOri
Copy link
Author

therealOri commented May 7, 2022

Just out of curiosity, is it advisable to use python's input type on a webpage? Shouldn't you use an HTML form for this?

Well, considering It's python in the browser, I would like to think it should be a thing. Like it works... but not very well. Kinda scuffed mixed in with trying to learn html. It's probably not advisable but like I said..it is "python in the browser/html" after all.

I would do the whole html form option but I don't currently know how to get the users input from that form into my python script as an argument to then output properly.... Also asking for input in my python script seems easier to handle/deal with than setting up everything for the form and making it look good, etc.

@antocuni
Copy link
Contributor

Thank you for the report. It seems to be a pyodide issue. E.g., if you code to the pyodide online REPL:
https://pyodide.org/en/stable/console.html
and type input('hello') at the prompt, if see a similar behavior:

  • an input box popup without any text
  • hello gets printed to the console

See also pyodide/pyodide#2505

I think this is more or less what's happening:

  1. the user calls the builtin input() function
  2. input() is implemented in C
  3. the C implementation prints the message to stdout
  4. the C implementation does the equivalent of sys.stdin.readline()
  5. Pyodide implements sys.stdin.readline() by using a JS popup inputbox.

In Pyscript, when you print something to stdout it is appended to the output div. This explains why you see Length? in your HTML.

I think this should be fixed by either upstream pyodide or pyscript itself. The easiest way is probably to provide our own implementation of the builtin input function, but we need to think carefully about this.

@antocuni antocuni added type: bug Something isn't working tag: pyodide Related to issues with pyodide lang: python Related to the Python language and removed needs-triage Issue needs triage labels May 10, 2022
@marimeireles marimeireles added the backlog issue has been triaged but has not been earmarked for any upcoming release label Sep 29, 2022
@critor
Copy link

critor commented Jan 20, 2023

Here is a partial workaround.

Add this at the start of your Python scripts :
try: _input except: _input = input def input(txt): val = _input(txt) print(val) return val

The try/except is to patch input() only once in case you use a REPL.

This will make input() behave more as expected in the console (prints prompt + value + new line).
But the popup without any information is still an issue.

@marimeireles
Copy link
Member

The reason for that is the asyncness nature of the browser that doesn't translate well to the python world.
This issue will be resolved once we add support to webworkers. (We're working on this in the moment!)
I wonder if you can do everything on the JS side? As a workaround.

@WebReflection
Copy link
Contributor

We solved the input issue in current terminal when it runs in workers and from a sync perspective there's nothing else one can do on the main thread because it has to be synchronous.

The current PyScript terminal doesn't use the JS prompt and it works without surprises so I think we can close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backlog issue has been triaged but has not been earmarked for any upcoming release lang: python Related to the Python language tag: pyodide Related to issues with pyodide type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants