27
loading...
This website collects cookies to deliver better user experience
$ mkdir flask-app
flask-app
folder.$ cd flask-app
python3
for the app. To set up a virtual environment, let us run the below command.$ python3 -m venv env
$ source env/bin/activate
$ pip install Flask
app.py
in the root directory of our project. You can use the terminal or code editor of your choice.$ touch app.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello world</p>"
python -m flask
. Before you can do that, you need to tell your terminal the application to work with by exporting the FLASK_APP
environment variable:export FLASK_APP=app
$ flask run
* Running on http://127.0.0.1:5000/
Port 5000
. Go to your browser and open localhost:5000. You should see Hello World in the browser. How cool is that? Amazing right?@app.route("/")
def hello_world():
return "<p>Hello world</p>"
/
in the browser. Then the flask app will run the hello_word
function. Since we were returning <p>Hello world</p>
, this gets rendered in the browser./
in the browser. To do that, we need to create two folders named static
and templates
$ mkdir static
$ mkdir templates
index.html
file inside the template
folder and add the snippet.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Document</title>
<link rel="stylesheet" type="text/css" href="static/index.css" />
<link
rel="shortcut icon"
href="{{ url_for('static', filename='favicon.ico') }}"
/>
</head>
<body>
<div>
<p>Pratap</p>
</div>
<script src="static/index.js"></script>
</body>
</html>
css
and js
file inside static
folder.body {
background-color: red;
}
console.log("I am running");
index.html
file<!-- Loading the css file using this line -->
<link rel="stylesheet" type="text/css" href="static/index.css" />
<!-- Linking the javascript file using this line -->
<script src="static/index.js"></script>
app.py
file and some more code there.from flask import Flask, render_template
app = Flask(
__name__,
template_folder="./templates",
static_folder="./static",
)
@app.route("/")
def hello_world():
return render_template('index.html')
Flask
function. We let the flask know where it should load the templates
and static
files.app = Flask(
__name__,
template_folder="./templates",
static_folder="./static",
)
By default Flask will look for template
and static
folders.
/
route we return render_template(index.html)
. So, the flask app will load the index.html
file and pass it to the render_template
function.@app.route("/")
def hello_world():
return render_template('index.html')
from flask import Flask, render_template, jsonify
app = Flask(
__name__,
template_folder="./templates",
static_folder="./static",
)
@app.route("/")
def hello_world():
return render_template('index.html')
@app.route("/json")
def json_response():
response = {"name": "Pratap", "age": 24}
return jsonify([response])
json_response()
function carefully we are now returning a dictionary
. We than pass it to jsonify()
function which we imported from Flask
@app.route("/json")
def json_response():
response = {"name": "Pratap", "age": 24}
return jsonify([response])
flask
app to restart after we make any changes. We need to update the app.py
and add the following snippet at the bottom of the file. We have added a condition that if we are running the file directly, add debug=True
. This will make sure to run the app in debug modeif __name__ == "__main__":
app.run(debug=True)
$ python app.py
if __name__ == "__main__":
app.run(port=8000, debug=True)
Things to remember
route()
function of the class defines the URL mapping of the associated function.app.run()
method is used to run our Flask Application.