1

I'm trying to add a python terminal onto a website using pyscript to run programs I've written using

<script type="mpy" terminal worker src="subrepo/folder/program.py"></script>

The terminal does appear, and it does run the first part of the code, put after the first input, it doesn't run.

This is the code I'm trying to run in the terminal.

#importing fuctions
from functionsource1 import function1
from functionsource2 import function2
#giving user choice between which fuction to run
choice = int(input("Choose to function1 or function2. Enter 1 to function1. Enter 2 to function2. "))
if choice == 1:
    print("debug test")
    function1()
elif choice == 2:
    print("debug test")
    function2()
else:
    print("ERROR")

After the code takes the input, nothing else runs, including the debug prints. I don't know what's wrong.

For clarification, here's the full html file.

<html>
    <head> 
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width,initial-scale=1.0">
        <title>Portfolio</title>
        <link rel="stylesheet" href="style.css">
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@pyscript/core/dist/core.css">
        <script type="module" src="https://cdn.jsdelivr.net/npm/@pyscript/core/dist/core.js"></script>
        <mpy-config>
            [files]
            "./subrepo/folder/function1.py" = "function1.py"
            "./subrepo/folder/function2.py" = "function2.py"
        </mpy-config>
    </head>
    <body>
        <h1>Portfolio</h1>
        <p>
            My name is ---. I'll be putting any work I've done here for the time being. I'll update the site when I have something worthwhile to put here.
        </p>
        <script type="mpy" terminal worker src="subrepo/folder/program.py"></script>
    </body>
</html>

I'm working on a github codespace, if that affects anything.

5
  • this doc for terminal may suggests that code has to be inside <script type="mpy">...code...</script> or it needs to access myterm = document.querySelector("#my_script"); and run code as await myterm.process('print("Hello world!")'); but I don't see it in your code. Commented Aug 8 at 18:30
  • ddi you try to run it on local computer? Did you try to use DevTools in Chrome/Firefox to see if there is any JavaScript error? Commented Aug 8 at 18:37
  • 1
    after resolving problem with names of files (it had to use ... = "functionsource1.py" instead of ... = "function1.py", etc/) I can run it and in DevTools I see error File "<stdin>", line 1, in <module> AttributeError: 'NoneType' object has no attribute 'terminal'. Maybe it bug in module or maybe it needs something else in HTML Commented Aug 8 at 18:57
  • I run import code code.interact() to have interactive Python and it seems it has problem with key Enter in input(). When I press Enter then it still waits for more. When I press again Enter then it ends input() but it assign something like this <JsProxy14> to variables Commented Aug 8 at 19:49
  • code works for me when I remove worker. And then input() shows popup window. Commented Aug 8 at 20:04

2 Answers 2

2

It seems they changed something and documentation doesn't show it.

Code works for me in worker when I use await input().

program.py

result = await input(...)
choice = int(result)

index.html

<script type="mpy" terminal worker src="subrepo/folder/program.py"></script>

It needs await even in example in documentation for terminal (doc for version 2025.8.1)

<script type="py" terminal worker>
    name = await input("What is your name? ")
    print(f"Hello, {name}")
</script>

Problem exist for type="py" and type="mpy" when they use worker

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

2 Comments

Pyscript has two sources.You code supports only for one source._12.pyscriptapps.com/gentle-dust/latest. This supports both the sources.
your code doesn't use input() at all. And it doesn't use worker - so I don't know if this will useful for OP. But you could add your code as answer and describe problem with two sources. OP will have another method to choose. And it can be useful for other visitors.
0

If you are fine using type="py" the following code is going to work to display on terminal.You can even import files from external python files.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>local modules</title>
    <link rel="stylesheet" href="https://pyscript.net/releases/2023.03.1/pyscript.css" />
    <script defer src="https://pyscript.net/releases/2023.03.1/pyscript.js"></script>
  </head>
  <body>
      <py-config>
        "packages" = ["pandas",]
        [[fetch]]
        files = ["./main.py"]
      </py-config>
<div id="mydiv"></div>
     <py-script>
choice = int(input("Choose to function1 or function2. Enter 1 to function1. Enter 2 to function2. "))
if choice == 1:
    print("1")
elif choice == 2:
    print("2")
else:
    print("ERROR")
      </py-script>
  </body>
</html>

I don't understand why pyscript.com has two sources when one source is just enough to display.Even if they are two sources they as suppose to show same result but they show different.From my experience its always better to avoid terminal.Terminal is easily hackable.DOM is always safe.

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.