Francesco Erichsen
Francesco Erichsen

Reputation: 23

How to implement a python script into an HTML site, so the user can type in input using the python script

I have a python script ( a random number generator ) and I want to create a site using HTML that uses the python script to give an input to the user which produces the random password. I am fairly new to programming and am not sure how to do this.

This is my code here

import random

chars = 'abcdefghijklmnopqrstuwxyz !@#$'

number = input("Number of passwords? - ")
number = int(number)

length = input("Password length - ")
length = int(length)

for f in range(number):
  password = ''
  for c in range(length):
    password += random.choice(chars)
  print(password)

Upvotes: 2

Views: 105

Answers (2)

Syed H
Syed H

Reputation: 270

A purely Pythonic way, using Flask. Execute the code, open your browser, head to localhost:8080, enter inputs and press Go.

from flask import Flask, jsonify, request
import random
app = Flask(__name__)

@app.route('/')
def index():
    print('Verified Request made to Base Domain')
    resp = jsonify(status=200)
    return "\
    <form type='GET' action='/result'>\
    <label for='number'>Number</label>\
    <input type='text' name='number' />\
    <label for='length'>Length</label>\
    <input type='text' name='length' />\
    <input type='submit' value='Go' />\
    </form>\
    "
@app.route('/result', methods=['GET'])
def run():
    number = int(request.args['number'])
    length = int(request.args['length'])
    passwords = []
    chars = 'abcdefghijklmnopqrstuwxyz !@#$'
    for f in range(number):
        password = ''
        for c in range(length):
            password += random.choice(chars)
        passwords.append(password)
    return jsonify(passwords= passwords)

app.run('localhost', 8080, app)


Upvotes: 0

Abhishek Bhagate
Abhishek Bhagate

Reputation: 5766

I am not exactly sure if you can do it on a website with python. There might be libraries that allow you to use python for the same but I don't think you can directly do it. However, you can do it using javascript as follows -

const chars = 'abcdefghijklmnopqrstuwxyz !@#$'
let numberOfPasswords = 0;

function passNum(value) {
  numberOfPasswords = value;
}

let lengthOfPassword = 0

function passLen(value) {
  lengthOfPassword = value;
}

let res = '';

function displayResult() {

  for (let i = 0; i < numberOfPasswords; i++) {
    for (let j = 0; j < lengthOfPassword; j++) {
      res += chars[Math.floor(Math.random() * Math.floor(chars.length))]
    }
    res+='\n'
  }
  document.getElementById('result').innerHTML = res;
}

document.getElementById("getPass").addEventListener("click", displayResult);
input,textarea{
  width:80%
}
<label for="fname">Number of passwords? - </label><br>
<input type="text" id="passnum" name="passnum" placeholder='Enter numer  of Passwords to be generated' onChange='passNum(this.value)'><br><br>
<label for="lname">Password length - </label><br>
<input type="text" id="passlen" name="passlen" placeholder='Enter desired length of Password' onChange='passLen(this.value)'><br><br>
<b>GENERATED PASSWORDS =></b><br>
<textarea rows="4" cols="50" id='result' placeholder='Generated passwords will be displayed here one on each line'>
</textarea><br>
<button id='getPass'>Generate password</button>

Upvotes: 1

Related Questions