Convert Python Console App to Web App With Flask

In this blog post, I will show you how to convert a Python console app to a Web App using Flask.

About Flask

Flask is a web development framework for Python that gives the capabilities to convert a console app into a Web App.

Using Flask, we can take a Python app running in a console and run in a browser.

Install Flask

To install flask, we use the following command.

pip install flask

Run flask

Below is a basic Flask application that runs a simple Python code in the browser.

from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()

When we run the code, we will use the flask webserver to view the app using our browser.

In our case, the webserver URL is.

http://127.0.0.1:5000

When I click on the URL, I can see my program is running.

I can also use a more sophisticated example of a console app that generates a number.

Below is the code I am using.

import random
import string
from flask import Flask
app = Flask(__name__)
@app.route('/')
def rannum():
x =random.randint(500000,9000000)
y = str(x)
return y
if __name__ == '__main__':
app.run()

The result is shown below.

Processing…
Success! You're on the list.

Posted

in

by