Photo by Markus Spiske on Unsplash
Create and Publish a Python Random Password Generator Web App for Free
At my company, we’re required to change our passwords every month, so I thought, why not create my own password generator?
Initially, I used Flask to build a simple password generator. However, I found it a bit complex since it required creating separate HTML files for the website.
Then I discovered Streamlit, which made the process much easier. With Streamlit, you can focus on your Python code and let it handle the web interface for you.
In this post, I'll first walk you through the code, and then I'll show you how to create a free web app using it.
At first, will show the algorithm for password generator. Its pretty straight forward.
def generate_random_password(length=12):
# string.ascii_letters is for lowercase and uppercase letters
# string.digits is for numbers
# string.punctuation is special characters
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for index in range(length))
return password
For the function, It starts with an empty string (''
) and adds characters to it using the join()
function.
for index in range(length))
used to pick up random characters till the length is achieved.
That’s the basic idea. Now, let’s move on to the fun part: making a web app.
import random
import string
import time
import streamlit as st
funny_quotes = [
"This password is so strong, it does push-ups!",
"Even your mom can't guess this one!",
"MCMC is watching",
"Guard this password like it's your last slice of pizza!",
"Better write this one down—it's practically a masterpiece!",
"Your password is more complicated than assembling IKEA furniture.",
"Your password is now as secretive as a magician’s tricks.",
"This password is so good, even the MCMC would be impressed.",
"This password is so secure, even Sherlock Holmes couldn’t deduce it.",
"This password is more complicated than your love life.",
"This password is stronger than your morning coffee.",
"This password is more mysterious than the Bermuda Triangle.",
"This password is harder to guess than the plot twist in a Momento.",
"This password is harder to figure out than the meaning of life.",
"This password is more confusing than your partner.",
"This password is more secure than your emotional walls.",
]
def generate_random_password(length=12):
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for _ in range(length))
return password
st.title("Random Password Generator")
length = st.slider("Select password length", min_value=8, max_value=30, value=12)
if st.button("Generate Password"):
password = generate_random_password(length)
st.write("Generated password:")
# Display password in a text input for easy copying
st.header(password)
# Temporary pop-up message for success
success_placeholder = st.empty()
success_placeholder.success("Password generated successfully.")
st.balloons()
# Wait for a few seconds
time.sleep(3)
st.write(random.choice(funny_quotes))
# Clear the success message
success_placeholder.empty()
# Simple tagline below the page
st.write("\n" * 40) # Adding some space before the footer
st.write("ahmadafif.com", align="center", font_size=17)
For the library we will import few:
import random
import string
import time
import streamlit as st
random
and string
: to generate random characters and string for password
time:
for brief delay before display quote
streamlit
: library that create interactive web app
you can install the library by using this command:
pip install streamlit
At first, I hard-coded the funny quotes like below
funny_quotes = [
"This password is so strong, it does push-ups!",
...
]
password generator is the same as explained earlier
for web interface:
st.title("Random Password Generator")
length = st.slider("Select password length", min_value=8, max_value=30, value=12)
Title: The app is titled "Random Password Generator".
Password Length Slider: You can choose the length of the password using a slider, with options ranging from 8 to 30 characters.
Generate Button and Display:
if st.button("Generate Password"):
password = generate_random_password(length)
st.write("Generated password:")
st.header(password)
Button: When you click the "Generate Password" button, the app creates a new password.
Display: The generated password is shown on the screen, ready for you to copy.
Fun Extras:
success_placeholder = st.empty()
success_placeholder.success("Password generated successfully.")
st.balloons()
time.sleep(3)
st.write(random.choice(funny_quotes))
success_placeholder.empty()
Success Message and Balloons: After generating the password, a success message appears, and fun balloons fill the screen.
Funny Quote: After a brief delay, a random funny quote from the list is displayed, adding a bit of humor to the experience.
How to upload to web apps
Run in local
Before deploying, you should test your app locally to ensure everything works. In the terminal or command prompt, navigate to the directory where your
[filename]
.py
file is located and run:
streamlit run [filename].py
This command will open a new tab in your web browser where you can see your app in action.
Create a GitHub Repository
Go to your GitHub profile and click on "New" to create a new repository.
Name your repository (e.g.,
PasswordGeneratorStreamlite
) and choose to make it public.Upload your code.
It will be something like this:
Deploy app to Streamlit
visit website and login using Github account (prefferable)
Click on "New app" and select the GitHub repository you just created.
Choose the branch (usually
main
ormaster
) and the Python file (e.gBasePassGen.py
).Click "Deploy".
Congratulations! Your app is ready.