Web Framework using Python Flask
We wil require two package to setup your environment.
To Install Flask & virtualenv open CMD and type the following commands:
pip install virtualenv
Once installed, new virtual environment is created in a folder.
mkdir newproj
cd newproj
virtualenv venv
To activate corresponding environment, on Windows, use the following
venv\scripts\activate
We are now ready to install Flask in this environment.
pip install Flask
To start, open myflask1.py and do the following:
from flask import Flask
app = Flask(__name__)
@app.route('/hello/<name>')
def hello_name(name):
return 'Hello %s!' % name
if __name__ == '__main__':
app.run()
- copy/download myflask1.py
- open CMD and type the myflask1.py file directory
It should be like the following:
C:\Users\Desktop>python C:\Users\Desktop\myflask1.py
The Output would be:
* Serving Flask app 'myflask1'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
- copy the URL http://127.0.0.1:5000/ and paste it in your preferred browser tab
Now try this
Enter this URL http://127.0.0.1:5000/hello/<name>
replace <name> with your name.
Example:
http://127.0.0.1:5000/hello/John
Output would display like this:
Hello John!
Good job!👍
Step 1. Download the pythontest folder or copy files located inside it.
Step 2. Repeat the procedure same as the above run the myflask2.py on cmd after that;
Step 3. Open login.html file in your selected browser
Step 4. Enter name
Step 5. Click Submit
Output will show:
Welcome John