数字を入力して、その二乗を計算します

def calculate_square(event): number = int(Element("number").value) result = number ** 2 Element("result").element.innerText = f"結果: {result}" Element("calculate").element.onclick = calculate_square

HTMLコードの表示例

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PyScriptの入力例</title>
    <!-- PyScriptのライブラリURLを正しく指定 -->
    <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css">
    <script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>

<body>

<h2>数字を入力して、その二乗を計算します</h2>

<input id="number" type="number" placeholder="数字を入力">
<button id="calculate">計算する</button>
<p id="result"></p>

<py-script>
    def calculate_square(event):
        number = int(Element("number").value)
        result = number ** 2
        Element("result").element.innerText = f"結果: {result}"

    Element("calculate").element.onclick = calculate_square
</py-script>

</body>