🍶 Flask is a framework to build websites. Here, we will make a simple blogging website. Below are the steps to start.
exit() #TOEXIT FLASK
cd inqzit #TO GO TO THE FOLDER
Create a python file and name it app_001001_start.py. Add the following code in it.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
set FLASK_DEBUG = 1 #TO AUTO RUN SERVER WHEN FILES UPDATED
flask run #TO RUN THE SERVER
Go to http://127.0.0.1:5000/ or http://localhost:5000/ in browser.
Instead of set FLASK_DEBUG = 1 line, we can add a few lines of code in the python file itself for auto run of server when a file is updated. Add the following code in the file.
if __name__=='__main__':
app.run(debug=True)
@app.route("/about")
def about():
return "<h1>About Us</h1>"
To add another URL that calls the same function or page, simply add @app.route("/anything") above it. See in below example:
@app.route("/home")
@app.route("/about")
def about():
return "<h1>About Us</h1>"
- Install Python
- Install Pip
- Install Flask
Run the following code in Command Line:
import flask #TO CHECK IF FLASK IS INSTALLEDexit() #TOEXIT FLASK
cd inqzit #TO GO TO THE FOLDER
Create a python file and name it app_001001_start.py. Add the following code in it.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
Run the following code in Command Line:
set FLASK_APP=app_001001_start.py #TO START RUNNING THE APPLICATION SERVERset FLASK_DEBUG = 1 #TO AUTO RUN SERVER WHEN FILES UPDATED
flask run #TO RUN THE SERVER
Go to http://127.0.0.1:5000/ or http://localhost:5000/ in browser.
Instead of set FLASK_DEBUG = 1 line, we can add a few lines of code in the python file itself for auto run of server when a file is updated. Add the following code in the file.
if __name__=='__main__':
app.run(debug=True)
Add another page to website
Simply add the following lines of code again. This time with a different url:@app.route("/about")
def about():
return "<h1>About Us</h1>"
To add another URL that calls the same function or page, simply add @app.route("/anything") above it. See in below example:
@app.route("/home")
@app.route("/about")
def about():
return "<h1>About Us</h1>"
Comments
Post a Comment