Run python from HTML

210 views Asked by At

I need to run some python code when HTML button click, I try this code but when I clicked the button nothing happened. This is my code: ''' <!doctype html>

<html>

<head>
    <meta charset="utf-8">
    <script type="text/javascript"
        src="https://cdn.jsdelivr.net/npm/[email protected]/brython.min.js">
    </script>
</head>

<body onload="brython()">

    <script type="text/python">
        def change_image():
            from browser import document, html
            document["image"].clear()
            document["image"] <= html.IMG(src="flowers.jpg")

        document["next"].bind("click", change_image)

    </script>
    <img id="image" src="image.png">
    <input id="next" type="button" value="next state"/>


</body>

</html>

''' What is my problem??

1

There are 1 answers

5
Nima On

Your code has a problem.Change it as follows

<script type="text/python">

from browser import document, html #Use the form outside the function
def change_image(item):
    document["image"].clear()
    document["image"].src="flowers.jpg" #change attributes

document["next"].bind("click", change_image)

</script>

Full code

<head>
    <meta charset="utf-8">
    <script type="text/javascript"
        src="https://cdn.jsdelivr.net/npm/[email protected]/brython.min.js">
    </script>
</head>

<body onload="brython()">
    <script type="text/python">

from browser import document, html
def change_image(item):
    document["image"].clear()
    document["image"].src="https://www.w3schools.com/html/img_chania.jpg"

document["next"].bind("click", change_image)

    </script>

    <img width=200 id="image" src="https://www.w3schools.com/html/pic_trulli.jpg">
    <input id="next" type="button" value="next state"/>
</body>