0

The basic python inputs code doesn't work right when I open them on google chrome. is there a different was to write the code in PyScript, that's still as simple as the normal inputs? I really don't want to have to convert everything to JavaScript, mostly because I would need to learn how JavaScript works. (also sorry about the formatting I literally just use notepad)

<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
<script defer src="https://pyscript.net/latest/pyscript.js"></script>

<body>

<py-script>

print("welcome to madlibs!")
print('please do not put any spaces in your answers')
print("")
print("")
print("")
name1 = input('name?')

this is what happens when I open it on google chrome. it doesn't even let the page load until it has input.

I wanted it to run how it would in python, but upon further google searches I learned that will never happen. I thought at least it would put the question in the input box, but it didn't do that either.

2 Answers 2

1

You need a browser-aware program.

<html>

<head>
  <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
  <script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>

<body>
  welcome to madlibs!<br>
  please do not put any spaces in your answers'<br><br><br><br>
  <button id="button">Input</button>
  name = <span id="span"></span>
  <py-script>
    import js
    from pyodide.ffi import create_proxy

    def input_name(event):
      name = input()
      js.document.getElementById("span").innerText = name

    proxy = create_proxy(input_name)
    js.document.getElementById("button").addEventListener("click", proxy)
  </py-script>
</body>

</html>

Sign up to request clarification or add additional context in comments.

Comments

0

For simple use cases you could prompt() the user for input()...

import contextlib

with contextlib.suppress(ImportError):
    from pyscript import window
    input = window.prompt

name1 = input('name?')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.