-1

I have this def on my flask python:

def passGen():
# Definição de caracteres #
letras = string.ascii_letters
numeros = string.digits
especiais = string.punctuation

# Definição de tamanho #

tam = random.randrange(7,13)

# Construção da senha #

Pass = letras + numeros + especiais

PassRandom = "".join(random.sample(Pass,tam))

print(PassRandom)```

I want to show that "PassRandom" on my html.

My html:

<html>
<head>
    <meta charset='utf-8'>
    <title>Meu Site</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <link rel="stylesheet" href="{{ url_for('static', filename= 'CSS/style.css') }}">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src='main.js'></script>
</head>
<body>
    <div>
        <input type="button" value="gerarSenha" class="btn btn-primary btn-lg btn-block"></input>
    </div>
    
    <h1>
        <p></p>
    </h1>
</body>
</html>

The trigger to this def should be my html button, is there any way to do this?

2 Answers2

0

You can do following

from flask import Flask, render_template, Response, request, redirect, url_for
app = Flask(__name__)

@app.route("/")
def index():
    return render_template('index.html')

@app.route("/generate_random/", methods=['POST'])
def get_random():
    # Definição de caracteres #
    letras = string.ascii_letters
    numeros = string.digits
    especiais = string.punctuation

    # Definição de tamanho #

    tam = random.randrange(7,13)

    # Construção da senha #

    Pass = letras + numeros + especiais

    PassRandom = "".join(random.sample(Pass,tam))

    print(PassRandom)```
    return render_template('index.html', random_num=PassRandom);

index.html

<html>
<head>
    <meta charset='utf-8'>
    <title>Meu Site</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <link rel="stylesheet" href="{{ url_for('static', filename= 'CSS/style.css') }}">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src='main.js'></script>
</head>
<body>
    <div>
        <form action="/generate_random/" method="post">
            <button name="gerarSenha" type="submit">GenerateRandom</button>
        </form>
    </div>
    
    <h1>
        <p></p>
    </h1>
</body>
</html>
0

On you flask code you can create a route to execute any python code you need. For example:


@app.route('/passGen')
def passGen():
    ...code
    return "PASSWORD"


Now all you have to do is call that route from your javascript code, for example, using the fetch api: Just don't forget to add an ID to your button to better identify it.

const button = document.getElementById('mybutton');
button.addEventListener("click", async ()=>{
  button.value = `api was called`;
  await fetch("/passGen")
})

Hope it helps!

Marcos
  • 11
  • 3