how do i make a interactive Pyscript terminal with input and output?

241 Views Asked by At

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/
1

There are 1 best solutions below

0
On
import contextlib

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

message = input('message to crypt: ')