Demo of a Random Number Generator Python Flask Application

Posted by

Random Number Generator Demo

Welcome to the Random Number Generator Demo

In this demo, we will create a simple web application using Python’s Flask framework to generate random numbers.

Setting up the Flask application

First, make sure you have Python and Flask installed on your machine. If not, you can install Flask using the following command:

pip install Flask

Next, create a new file called app.py and add the following code:

from flask import Flask
import random

app = Flask(__name__)

@app.route('/')
def generate_random_number():
    return str(random.randint(1, 100))

if __name__ == '__main__':
    app.run(debug=True)
    

Save the file and run the Flask application using the following command:

python app.py

Accessing the random number generator

Once the Flask application is running, open your web browser and go to http://127.0.0.1:5000/. You should see a random number generated on the page each time you refresh it.

Adding some styling

To make the demo look more appealing, let’s add some basic styling using CSS. Create a new file called style.css and add the following code:

body {
    font-family: Arial, sans-serif;
    text-align: center;
}

h1 {
    color: #3399ff;
}

p {
    margin-bottom: 20px;
}

code {
    background-color: #f2f2f2;
    padding: 3px 5px;
}

pre {
    background-color: #f2f2f2;
    padding: 10px;
}
    

Link the CSS file to the HTML by adding the following line to the head section of the HTML file:

<link rel="stylesheet" type="text/css" href="style.css">

Conclusion

Congratulations! You have successfully created a simple Random Number Generator using Python’s Flask framework. You can now further enhance this demo by adding more features and functionality as per your requirements.

Happy coding!