How to create button that runs python code?

Viewed 116

I'm learning python currently and I've made a password generator. I would like to make it so the script runs when you press a button, so you don't have to refresh the page to get a new password. I'm not sure where to start so any help would be appreciated. Below is my python script. I'm using flask.

 <py-script> 
    num = "0123456789"
    lowercase = "abcdefghijklmnopqrstuvwxyz"
    uppercase = lowercase.upper()
    special = "!@#$%^&*()_+~`|}{[]:;?><,. /-="

    all = num+lowercase+uppercase+special

    from random import choice
    password = "".join(
    choice(all) for i in range(16)
      )
    print (password)
 </py-script>
1 Answers
*Press html Button more than once to work effectively*

    <html>
        <head>
          <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
          <link rel = 'stylesheet' href = "https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css">
          <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
        </head>
    
      <body>
        <button id = "show_password" class = "btn btn-primary" type = "submit" pys-onClick="show_password">Password Generator</button>
        <div id="outputDiv" style="margin-top: 10px;margin-bottom: 10px;"></div>
       
        <py-script> 
                 from random import choice
                 def show_password(*args, **kwargs):
                    output = Element("outputDiv")
                    num = "0123456789"
                    lowercase = "abcdefghijklmnopqrstuvwxyz"
                    uppercase = lowercase.upper()
                    special = "!@#$%^&*()_+~`|}{[]:;?><,. /-="
    
                    all = num+lowercase+uppercase+special
                    password = "".join(choice(all) for i in range(16))
                    output.write(password)
        </py-script>
      </body>
    </html>

enter image description here

Related