0

I want to host a python script on a website with pyscript, which has both console input and console output. how exactly do i do that?

i used https://pyscript.com/ to make the website and github pages to deploy it. i couldn't see the preview on pyscript.com.

this is most of the python code. the only things that i left out here are the encrypt and decrypt keys:

def encrypt(message):
    encrypted_message = ""
    for letter in message:
        if letter in encrypt_key:
            encrypted_message += encrypt_key[letter]
        else:
            encrypted_message += letter
    return encrypted_message

def decrypt(encrypted_message):
    decrypted_message = ""
    for letter in encrypted_message:
        if letter in decrypt_key:
            decrypted_message += decrypt_key[letter]
        else:
            decrypted_message += letter
    return decrypted_message

while True:
    message = input('message to crypt: ')
    task = input('encrypt or decrypt? ')
    if task == 'encrypt':
        encrypted_message = encrypt(message)
        print(encrypted_message)
    
    elif task == 'decrypt':
        decrypted_message = decrypt(message)
        print(decrypted_message)
    else:
        break

on the website, i get this error:

Traceback (most recent call last):
File "/lib/python311.zip/\_pyodide/\_base.py", line 499, in eval_code
.run(globals, locals)
^^^^^^^^^^^^^^^^^^^^
File "/lib/python311.zip/\_pyodide/\_base.py", line 340, in run
coroutine = eval(self.code, globals, locals)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "\<exec\>", line 68, in \<module\>
File "\<exec\>", line 32, in input
Exception: input() doesn't work when PyScript runs in the main thread.
Consider using the worker attribute: https://pyscript.github.io/docs/2023.11.2/user-guide/workers/
4
  • There's a lot on using input() in conjunction with pyscript in StackOverflow, such as here and others that should come up with a search, & so it is hard to follow what you missed about the exception ' input() doesn't work when PyScript runs in the main thread'? There's even a direct link to pyscript.github.io/docs/2023.11.2/user-guide/workers that further discusses how you don't want to block things in your main thread. There's also this reddit post asking much the same as you ... Commented Dec 15, 2023 at 17:14
  • <comment> here that discusses some of the challenges you are facing. And as that latter post references, there is already a console option for pyodide, which underlies the kernel involved for pyscript. You can use Pyodide in conjunction with JupyterLite to do something like you describe separate from pyscript. Example of a working version is here as part of the sympy documentation site. .... Commented Dec 15, 2023 at 17:21
  • <continued> (A simpler version separated from the sympy documentation site is here.) The console allows you to try sympy examples right there using pyodide as the Python-enabling kernel. More on how they did it is covered here and here and at links therein. Commented Dec 15, 2023 at 17:22
  • 1
    It sounds like you need to add the worker attribute to your <script type="py"> tag. Commented Dec 20, 2023 at 16:10

1 Answer 1

0
import contextlib

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

message = input('message to crypt: ')
Sign up to request clarification or add additional context in comments.

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.