It needs to use JavaScript to send AJAX request to server. Server can return only password (without HTML) and JavaScript has to put it in textbox.
But page from your link uses JavaScript to generate password directly in browser
Minimal working code which uses JavaScript to get values from FORM, send to server, get result from server and put password in FORM.
from flask import Flask, request, render_template_string
import random
import string
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == "POST":
data = request.json
lower_number = int(data.get('lower', 0))
upper_number = int(data.get('upper', 0))
password_lower = random.choices(string.ascii_lowercase, k=lower_number)
password_upper = random.choices(string.ascii_uppercase, k=upper_number)
password = password_lower + password_upper
random.shuffle(password) # change order of chars (work in-place so doesn't need to assign to variable)
return "".join(password)
return render_template_string('''<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form method="POST">
Lower: <input type="text" id="lower_id" value="10" /><br/>
Upper: <input type="text" id="upper_id" value="10" /><br/>
<button type="submit" name="btn" onclick="generate();return false">GENERATE</button></br>
<input type="text" id="password_id" /><br/>
</form>
<script>
var password = document.getElementById("password_id");
var lower_input = document.getElementById("lower_id");
var upper_input = document.getElementById("upper_id");
function generate() {
fetch("/", {
method: "POST",
body: JSON.stringify({
lower: lower_input.value,
upper: upper_input.value,
}),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
})
.then(response => response.text())
.then(text => {password.value = text;})
}
</script>
</body>
</html>''')
if __name__ == '__main__':
#app.debug = True
app.run()
EDIT:
In fetch I set
- url
"/",
- method
POST,
- data converted to JSON,
content-type as JSON - so Flask can get it with request.json,
accept as JSON - to inform Flask that I expect response as JSON (but I don't respect it and in Flask I send normal text instead of JSON - so I could skip this header)
Because JavaScript uses asynchronous functions so
fetch uses .then() to execute next function when it gets response from server. This function gets text/body from response. It also has to wait for result and it uses .then() to execute next function when it finally get text from response. And this function puts this text in <input> for password.
Mozilla: Using Fetch and Fetch API