Button Click function Call in PyScript

Button Click function Call in PyScript

In this Blog Post, I have covered a scenario where it shows the Current Date Time when page loads and when the Current Date Time button is clicked.

index.html:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Demo</title>
    
        <!-- Recommended meta tags -->
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width,initial-scale=1.0">
    
        <!-- PyScript CSS -->
        <link rel="stylesheet" href="https://pyscript.net/releases/2025.2.1/core.css">
    
        <!-- This script tag bootstraps PyScript -->
        <script type="module" src="https://pyscript.net/releases/2025.2.1/core.js"></script>
    </head>
    <body>
        <div id="demo"></div><br/>
        <button py-click="show">Current Date Time</button>
        <script type="py" src="./main.py"></script>
    </body>
</html>

main.py:

import js
import datetime

def show( event ):
    now = str( datetime.datetime.now() )
    js.document.getElementById( "demo" ).innerHTML = now

show( None )

Output:

Leave a Reply